Multi-fields vs Nested data type

Hey,

Trying to understand difference between "fields" and "nested". For example, I have an array of objects, each one having the following fields:

  • name
  • name_hash

First (and probably obvious) option is to map this as "nested" type with these two fields. However, I just discovered there is an option to use multi fields in mapping. So, theoretically, I could map it this way:

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword",
        "fields": {
          "name_hash": { 
            "type":  "keyword"
          }
        }
      }
    }
  }
}

Since nested objects have several drawbacks (dedicated "nested' queries has to be used, performance issues, etc) - why anyone will use "nested" if same result can be achieved using "fields"? Or I missing something?

Multi fields and nested types are very different concepts and serve completely different purposes.

Multi fields allow a single field to be analyzed in multiple ways, and these different ways are exposed as sub-fields. If you e.g. send in a document with just a name field, you can have this mapped as keywork and specify multi fields mapped as text and/or ngrams etc. The documenmt still only has a single field.

Nested mappings is generally used when you have an array of JSON objects with more than one field and you want to be able to create complex set of boolean criteria against each of the objects in the array. If you do not use nested mappings Elasticsearch will index the fields as an array without keeping track of which fields are grouped together in objects, basically flatten the structure. With nested mappinmgs each object is basically indexed as a separate document behind the scenes, which allows you to write more complex queries and conditions. As you noted this additional flexibility and power naturally comes at a cost.

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.