0
0
ElasticsearchConceptBeginner · 3 min read

Multi-field Mapping in Elasticsearch: What It Is and How It Works

In Elasticsearch, 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.

json
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      }
    }
  }
}
Output
Mapping created with 'title' as text and 'title.keyword' as keyword sub-field.
🎯

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 fields property.
  • Useful for sorting, filtering, and flexible querying.

Key Takeaways

Multi-field mapping lets you index one field in different ways for flexible search.
It combines analyzed text and keyword indexing on the same data.
Use it to enable both full-text search and exact matching or sorting.
Sub-fields are defined inside the main field's mapping under 'fields'.
It helps keep your index efficient without duplicating data.