Multi-field Mapping in Elasticsearch: What It Is and How It Works
multi-field mapping allows a single field to be indexed in multiple ways with different analyzers or data types. This means you can search or sort the same data differently, like full text search and exact keyword matching, using one field stored in multiple forms.How It Works
Imagine you have a book title and you want to search it in two ways: by the exact title and by words inside the title. Multi-field mapping lets Elasticsearch store the same text twice but treat each copy differently. One copy can be analyzed for full-text search, breaking it into words and ignoring case, while the other copy is stored as a keyword for exact matches or sorting.
This works by defining a main field and adding sub-fields under it with different settings. When you index a document, Elasticsearch creates multiple versions of that field behind the scenes. This way, you get flexible search options without duplicating your data manually.
Example
This example shows a title field indexed as both analyzed text and keyword for exact matching.
{
"mappings": {
"properties": {
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}When to Use
Use multi-field mapping when you want to perform different types of searches or operations on the same data. For example, you might want to do full-text search on a product name but also sort or filter by exact product names. It is common in e-commerce, content management, and logging systems where flexible search and sorting are needed.
This approach saves space and keeps your index clean by avoiding duplicate fields for the same data.
Key Points
- Multi-field mapping indexes one field in multiple ways.
- It allows full-text search and exact matching on the same data.
- Sub-fields are defined inside the main field's
fieldsproperty. - Useful for sorting, filtering, and flexible querying.