Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

Searching for product code 'AB-1234' returns many unrelated products, and an exact match is not first. The field is mapped as text with the standard analyzer. Explain the cause and fix.

The standard analyzer splits 'AB-1234' at the hyphen into the terms 'ab' and '1234', and a match query by default matches documents containing either term, so every product with 'ab' or '1234' anywhere in the field is a hit, ranked by BM25 rather than by exactness. The fix is to map product codes as keyword, usually with a lowercase normalizer and often as a multi-field next to the text field, and query that keyword with a term query or boost it in a bool should clause. If partial matching is also wanted, a custom analyzer with word_delimiter_graph and preserve_original keeps the whole code as a token too; any analyzer change needs a reindex.

Why this question is asked

Product codes, order numbers, part numbers and SKUs look like text, so they often end up in text fields by default. Then a user searches for an exact code, the one thing that should be trivially correct, and gets a page of unrelated products. Interviewers like this debugging question because it tests whether we can reason from the analyzer to the query to the score instead of guessing. In this answer, we will reproduce the problem, see exactly how the code is tokenised and why unrelated products match, fix the mapping with a keyword field, write the query, add partial matching where needed, and roll the change out with a reindex.

1Reproducing the Problem

The field is mapped as text with the standard analyzer, and the application sends a match query, the usual full-text query.

Java
PUT /products
{ "mappings": { "properties": { "code": { "type": "text" } } } }
POST /products/_bulk
{ "index": { "_id": "1" } }
{ "code": "AB-1234" }
{ "index": { "_id": "2" } }
{ "code": "AB-9 AB-77 AB" }
{ "index": { "_id": "3" } }
{ "code": "XZ-1234" }
GET /products/_search
{ "query": { "match": { "code": "AB-1234" } } }
// hits: 2 (score 0.9), 1 (score 0.8), 3 (score 0.5) -> illustrative order

We get all three documents, and the exact match is not first. To see why, we look at the analyzer, not at the query.

2Why Unrelated Products Match

The _analyze API shows what the standard analyzer does with the code.

Java
POST /products/_analyze
{ "field": "code", "text": "AB-1234" }
// "ab" position 0
// "1234" position 1
Terms in the index after the standard analyzer
DocumentStored codeIndexed termsMatches 'ab' OR '1234'?
1AB-1234ab, 1234yes, both terms
2AB-9 AB-77 ABab, 9, ab, 77, abyes, 'ab' three times
3XZ-1234xz, 1234yes, '1234'

The standard tokenizer treats the hyphen as a word boundary and splits the code into two terms, then lowercases them. The same happens to the query string. A match query combines its terms with OR by default, so any document containing 'ab' or '1234' matches. Document 3 matches through '1234', and document 2 through 'ab'.

The ranking is the second half of the problem. BM25 scores by term frequency, rarity and field length, not by whether the whole value is identical. Document 2 contains 'ab' three times, and if 'ab' is rarer in the index than '1234', or the field lengths differ, an unrelated document can outscore the exact match. The engine has no idea that 'AB-1234' was meant as one indivisible identifier, because after analysis it no longer exists as one term.

3Fix: Map the Code as a Keyword

A keyword field is not tokenised: the whole value becomes a single term, so 'AB-1234' stays 'AB-1234'. A normalizer is like an analyzer for keyword fields, without a tokenizer; a lowercase normalizer lets 'ab-1234' match 'AB-1234'. We keep the text version too, as a multi-field, in case loose searches on the code are useful.

Java
PUT /products_v2
{
"settings": { "analysis": { "normalizer": {
"lowercase_norm": { "type": "custom", "filter": ["lowercase"] } } } },
"mappings": { "properties": {
"code": {
"type": "keyword",
"normalizer": "lowercase_norm",
"fields": { "text": { "type": "text" } }
} } }
}
POST /products_v2/_analyze
{ "field": "code", "text": "AB-1234" }
// "ab-1234" -> one term

Now the main field holds exactly one term per code, and code.text still holds the split terms for fuzzy needs. Which one is the main field and which is the subfield is a design choice; what matters is that a keyword version exists.

4Writing the Query

For an exact code lookup, a term query on the keyword field returns only the exact match. When the same search box serves both codes and product names, we combine an exact clause with a full-text clause in a bool query, giving the exact clause a large boost so an exact code always ranks first.

Java
GET /products_v2/_search
{
"query": { "bool": { "should": [
{ "term": { "code": { "value": "AB-1234", "boost": 10 } } },
{ "match": { "code.text": { "query": "AB-1234", "operator": "and" } } },
{ "match": { "name": "AB-1234" } }
] } }
}
// hits: 1 first by a wide margin (illustrative), 2 and 3 only from the name clause, if at all

Notice that the term query also goes through the normalizer, so the user's casing does not matter. The operator and on the text clause means a loose match still needs both 'ab' and '1234', which removes documents 2 and 3 from that clause. Many teams also detect code-shaped input, for example with a pattern like two letters, a hyphen and digits, and send a pure term query in that case.

5When Partial Matching Is Also Wanted

Sometimes users type 'AB1234', '1234' or 'AB 1234' and still expect the product. A custom analyzer with word_delimiter_graph can index several forms of the code at once. With preserve_original it keeps the whole code as a token, and with catenate options it adds the joined form. It should run after a whitespace tokenizer, because the standard tokenizer would already have split the code before the filter sees it.

YAML
"analyzer": { "code_parts": {
"tokenizer": "whitespace",
"filter": [ { "type": "word_delimiter_graph", "preserve_original": true,
"catenate_all": true }, "lowercase" ] } }
// "AB-1234" -> "ab-1234", "ab1234", "ab", "1234"

Whatever we choose, existing documents keep their old terms until they are indexed again, because analysis happens at write time. So we create products_v2 with the new mapping, reindex into it, and switch an alias once sample queries return the right products.

6Follow-up questions

Why not just use match_phrase on the text field?

It would require 'ab' followed by '1234', which removes documents 2 and 3, but it would also match 'AB 1234' and 'AB/1234', and scoring is still text scoring. A keyword field expresses 'this is an identifier' directly and is cheaper.

Can we change the field from text to keyword in place?

No. The type of an existing field cannot be changed. We can add a new keyword subfield to the existing mapping and repopulate it with update_by_query, or create a new index and reindex.

Should ids always be keyword?

Yes, anything compared exactly, such as ids, codes, emails, status values and tags, belongs in keyword fields. They also support sorting and aggregations through doc values.

7Conclusion

In this answer, we traced the bad results to the standard analyzer, which splits 'AB-1234' into 'ab' and '1234', so an OR match finds every document with either term and BM25 ranks them by statistics instead of exactness. We mapped the code as a keyword with a lowercase normalizer and a text multi-field, queried it with a boosted term clause inside a bool should, added a word_delimiter_graph analyzer with preserve_original for partial matching, and noted that every analyzer change needs a reindex.

What to say in the interviewAbout 80 seconds aloud

Open with

The cause is analysis. With the standard analyzer, 'AB-1234' is tokenised at the hyphen into two terms, 'ab' and '1234', and the query string is analysed the same way.

Then, if they want more

A match query ORs its terms by default, so any document containing 'ab' or '1234' matches, which is why unrelated products appear. And ranking is BM25, based on term frequency, rarity and field length, not on whether the whole code is identical, so a document that repeats 'ab' can outrank the exact one.

I would confirm it with the _analyze API on that field. The fix is to map codes as keyword, where the whole value is one term, with a lowercase normalizer so case does not matter, and usually keep a text multi-field next to it.

Then an exact lookup is a term query on the keyword field, and for a general search box I would use a bool should with the term clause heavily boosted plus the normal text clauses, so exact codes always come first.

If users also type partial forms like 'AB1234' or '1234', I would add a custom analyzer with a whitespace tokenizer and word_delimiter_graph with preserve_original and catenate options. Any of these mapping changes requires a new index and a reindex, switched in through an alias.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Elasticsearch documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Searching for product code 'AB-1234' returns many unrelated products, and an exact match...": https://scrivio-interview-library.netlify.app/databases/search-engines/fe51751c41/