Hey folks :wave: We're enhancing our part number ...
# community-help
s
Hey folks šŸ‘‹ We're enhancing our part number search experience for our parts catalog (~670k documents) and we're looking for guidance on the right approach. Our use case: Users search by partial part numbers — things like SKUs in the format
6PK2100
,
09103M08013
,
F5TZ-9C331-B
, etc. We want results ranked in this specific order: 1. Query matches the start of the part number → rank highest (e.g.
"6PK"
→
"6PK2100"
) 2. Query matches the end of the part number → rank second (e.g.
"2100"
→
"6PK2100"
) 3. Query matches somewhere in the middle → rank third (e.g.
"PK21"
→
"6PK2100"
) Our current schema (relevant fields): • partNumber → index: true, infix: false • partName → index: true, infix: false • token_separators: [] We also currently have no
token_separators
configured, so hyphenated/dotted part numbers like
F5TZ-9C331-B
are indexed as a single token. What we want guidance on: 1. Is there any native way in Typesense to rank by match position (prefix → postfix → substring) as a sort dimension? is there something we're missing? 2. If native positional ranking isn't supported, what is the recommended approach. 3. To even surface all three match types, should we enable
infix: true
on
partNumber
(and
partName
)? What are the realistic performance implications? 4. Should we add common part number separators like
-
,
.
,
/
to
token_separators
? Would this help, or would it break exact part number matches? Would really appreciate any guidance on the schema changes (if any) and whether positional ranking is on the roadmap or achievable today. Thanks! @Sahil Rally
a
Hey @Sameer Negi Partially... Typesense gives you natively two tiers, not three: prefix matches always outrank infix matches when you set
infix: true
on the field and pass
infix=always
at query time. The infix code path bucket-scores all middle and suffix matches together, so out of the box you can't sort "ends with" above "appears in the middle". Prefix matches get a real
_text_match
score, infix matches are pinned to a fixed lower score, and the official test asserts
prefix.text_match > infix.text_match
. You have two options depending on how strict you need that suffix-above-middle ordering: • Two tiers (simplest): set
infix: true
on
partNumber
, query with
infix=always
. Prefix on top, everything else below. Good enough if "appears anywhere" is acceptable as the bottom bucket. • Three tiers via reversed field + `/multi_search`: store each part number both forwards and reversed, then fire 3 searches in parallel and stitch them on the client. Reversed field is the standard trick to turn "suffix" into "prefix":
Copy code
{
  "name": "parts",
  "token_separators": [
    "-",
    ".",
    "/"
  ],
  "fields": [
    {
      "name": "partNumber",
      "type": "string",
      "infix": true
    },
    {
      "name": "partNumberReversed",
      "type": "string",
      "infix": false
    },
    {
      "name": "partName",
      "type": "string",
      "infix": true
    }
  ]
}
At index time:
Copy code
{
  partNumber: "6PK2100",
  partNumberReversed: "0012KP6"
}
At query time:
Copy code
const q  = "PK21";
const qR = q.split("").reverse().join("");

multi_search([
  { q,     query_by: "partNumber",         prefix: true, infix: "off"    },  // tier 1
  { q: qR, query_by: "partNumberReversed", prefix: true, infix: "off"    },  // tier 2
  { q,     query_by: "partNumber",         prefix: true, infix: "always" },  // tier 3
]);
Then merge client side, deduping IDs from top tier downward. That gives you exactly the prefix → suffix → middle ranking. The only gotcha is that infix is a heavy operation. For 670k SKUs of short alphanumeric tokens, p95 is typically acceptable on modern hardware, but it grows with cardinality. There's a built-in safety net (
search_cutoff_ms
, default 30s) and if a query saturates it you'll see
search_cutoff: true
in the response. I'd benchmark on a representative slice before committing and keep an eye on
search_cutoff
in the response. About Adding
-
,
.
,
/
to `token_separators`: Yes, generally a net win for SKU data. The tokenizer treats those characters as splits, so
F5TZ-9C331-B
becomes 3 tokens:
["f5tz", "9c331", "b"]
. That buys you: • A query for
9C331
becomes a fast exact token match instead of a slow infix scan. • The full
F5TZ-9C331-B
query still works, because it's also tokenized into the same 3 tokens and Typesense AND-merges them by default. •
F5TZ
matches as a clean prefix of the first token. The trade-off: infix can no longer match across a separator, so
Z-9C
won't match anymore because
-
is no longer part of any token. For your mixed dataset (some hyphenated SKUs, some not), the segmented tokens become first-class prefix-matchable, which is the cheaper path. Solid recommendation. Positional ranking as a native sort dimension beyond the prefix vs infix split isn't something I can see on the public roadmap right now. Let us know how the testing goes, happy to look at specific queries once you have a config in place!
šŸ‘€ 1
s
Thanks again, one more follow-up that came up as we thought through the token separators + infix interaction more carefully. You mentioned earlier that prefix matches always outrank infix matches natively. That makes sense without token separators. But we think token separators change this in a way we want to confirm. Without token separators, for query `"9C331"`: -
9C331-XYZ
→ true string prefix match → ranks high -
F5TZ-9C331-B
→ infix match → ranks below The prefix-beats-infix guarantee works perfectly here. But after adding
-
to
token_separators
, both part numbers get tokenized: -
9C331-XYZ
→ tokens
["9c331", "xyz"]
→ query matches
9c331
as a prefix of a token -
F5TZ-9C331-B
→ tokens
["f5tz", "9c331", "b"]
→ query also matches
9c331
as a prefix of a token Both are now token-level prefix matches of identical quality. So
F5TZ-9C331-B
— which is a mid-string match on the full part number string, now gets the same
_text_match
score as
9C331-XYZ
which is a true string prefix match. The prefix-beats-infix tier that helps us no longer applies because the mid-string match is no longer going through the infix code path at all. Is this correct? If so, does Typesense have any awareness of which token position matched e.g. would a match on the first token score higher than a match on the second or third token for the same query? Or are all token-level prefix matches scored equally regardless of where in the document field the token appears? Thanks!
a
Yes, but only when you opt in. The search parameter
prioritize_token_position=true
does exactly this. A match on the first token scores higher than a match on the second or third. By default it is off, so your analysis is correct: without it, both matches get the same
_text_match
score.