AI Hints
Home›Elasticsearch Tutorial›Field Types in Depth
Elasticsearch · Lesson 43 of 120

Multi-fields

Text and keyword together.

⏱ 2 min read▶ Video at 39:49🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

Product names need both. You search them as words, but you also sort them alphabetically. A multi field indexes the same value two ways.

  • Search the name
  • Sort the name
  • Index it twice

name and name.raw

Name is text, for searching. Inside fields, we add raw, a keyword version of the same value. We index one product, then search the text, and sort by the keyword. Run it.

Dev Tools Consoletext
PUT products
{ "mappings": { "properties": {
    "name": { "type": "text",
              "fields": { "raw": { "type": "keyword" } } } } } }

PUT products/_doc/1?refresh=true&filter_path=result
{ "name": "Storm Rain Jacket" }

GET products/_search?filter_path=hits.hits._source
{ "query": { "match": { "name": "storm" } },
  "sort": [ { "name.raw": "asc" } ] }
Output
# PUT products 200 OK
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "products"
}

# PUT products/_doc/1 201 Created
{"result": "created"}

# GET products/_search 200 OK
{"hits": {"hits": [{"_source": {"name": "Storm Rain Jacket"}}]}}

When to use multi-fields

Use them when one value needs to be searched and also filtered, sorted, or counted. You can also index a field with several analyzers, like English and plain. The source document stays the same. Only the index has the extra field.

keyvalue
Search + sorttext + keyword
Several analyzersenglish + standard
Sourceunchanged
Costa bit more disk

Summary

Multi fields index one value in several ways, like text for search, and keyword for sorting. Next, numbers, dates and booleans.

Key takeaways
  • fields
  • name.raw
  • Search + sort
Next up: Numbers, Dates, Booleans — the other types