BeginnerSearch (Elasticsearch)
Searching for an exact SKU returns nothing
Product codes are indexed as full text, and exact matches fail.
The setup today
Products are indexed in Elasticsearch with dynamic mapping: the index was created by sending documents, without defining fields. Customers search names and descriptions; support staff look products up by SKU.
Current configuration
PUT products/_doc/1 { "sku": "AB-1234-XL", "name": "Running shoe", ... }
# dynamic mapping: string fields become text, with a .keyword sub-field
support query: { "term": { "sku": "AB-1234-XL" } }What you know
- The term query for an existing SKU returns no hits.
- A match query on sku returns products with similar but different codes.
The new requirement
Support needs exact SKU lookups and filters, and customers still need full-text search on names.
Make the call
2 questions · first-try answers earn the most XP
What a strong answer also covers
- Define mappings explicitly for important indices instead of relying on dynamic mapping.
- Use keyword fields for filters, sorting and aggregations; they are also much cheaper for those uses.
- A field can have both: text for search and a keyword sub-field for exact use.
- Normalizers can make keyword matching case-insensitive without tokenizing.
Variations to think through
Change one fact and decide again. There is no single right answer here.
- Support also wants to find SKUs by prefix, like AB-12. Which field and query fit?
- Customers type SKUs into the main search box. How do you rank an exact SKU match first?
- Some SKUs differ only by case. Should the lookup be case-sensitive?