Sub topic 6
Designing the index and its mapping
This chapter assumes you know how an inverted index and text analysis work (earlier chapters), plus basic SQL and REST APIs. Here you decide what goes into the index and how each field is typed.
The index is a read model, not a copy of your tables
A search index is built for queries, not for storing normalized data. Design it around the questions users ask. A typical choice is one document per searchable thing, such as a product, with related data flattened in. A product document might hold its category name directly instead of a foreign key. This avoids joins at query time, which most search engines handle poorly or not at all.
The cost is duplication. If a category is renamed, every product document that carries the name must be updated. Keep this in mind when you design the sync pipeline in the next chapter.
Full-text fields and exact-match fields
The main mapping decision is how each field is treated.
- A text field is run through analysis (tokenizing, lowercasing, stemming). It supports relevance-ranked search such as "running shoes" matching "Trail Running Shoe".
- A keyword field is stored as one unanalyzed term. It supports exact match, filtering, sorting, and aggregations such as facets.
Using the wrong type is a common error. A category stored as text gets split into tokens, so a filter on "Home & Garden" can match unrelated documents. A product name stored as keyword only matches the full, exact string.
This mapping separates the two kinds of field:
PUT /products
{
"mappings": {
"properties": {
"name": { "type": "text" },
"category": { "type": "keyword" },
"price": { "type": "float" },
"in_stock": { "type": "boolean" }
}
}
}Here name is searched by relevance. category, price, and in_stock are used to narrow results, and they do not affect the score.
Fields that need both
Some fields need both behaviors. A product name should be searchable as text and also sortable alphabetically. Most engines let you index one field in several ways. In Elasticsearch this is called a multi-field:
PUT /products
{
"mappings": {
"properties": {
"name": {
"type": "text",
"fields": {
"raw": { "type": "keyword" }
}
}
}
}
}You search on name and sort on name.raw. The cost is extra index size, so add sub-fields only when a query needs them.
Decide what to index and what to store
Index only the fields that queries use. The search result usually needs just enough data to render a list: ID, title, price, image URL. Fetch full details from the primary database by ID if the user opens an item. Smaller documents mean a smaller index, faster queries, and fewer updates.
Dynamic mapping and why to avoid it
If you send a document with an unknown field, many engines guess its type and add it to the mapping. This is convenient for experiments. In production it causes trouble:
- A string such as an ID may be mapped as
textwhen you wantedkeyword. - A bad record can add hundreds of unwanted fields and bloat the mapping.
- A field's type cannot be changed after documents exist. You must reindex.
Set mappings explicitly. In Elasticsearch you can set "dynamic": "strict" so documents with unknown fields are rejected.
Design checklist
- List the user queries: free-text search, filters, sort orders, facets.
- Give each field a type that matches how it is queried.
- Flatten related data into the document, and note what must be updated when it changes.
- Turn off dynamic mapping.
- Version the index name (for example
products_v1) so you can change it later.
Exercise
A document has a sku field such as "AB-1042", used only for exact lookups and filters. Which type should it have, and why would text be a poor choice?
Show answer
Use keyword. A text field would split "AB-1042" into tokens like "ab" and "1042", so a filter could match other SKUs that share a token, and exact matching would be unreliable.