Let's talk about pros and cons among flat fields, dynamic, object, flattened, nested.
flat fields
pros: Simple and clear, field type is specified in mappings.
cons: New field should do put mapping at first. Number of fields is limited by engine. Different fields have no relationship.
dynamic
For new fields or inner object which have no mappings represented.
set to false, will not index new fields, but new fields can be retrieved by _source.
set to strict, will report an error when unknown new fields in doc.
cons: strict is not elastic meanwhile dynamic will lead a mapping explosion problem, as Elasticsearch limit the max number of feild to 1000 by default, see also index.mapping.total_fields.limit: 1000
flattened
Better type than daynamic, comes with 7.3 +
pros: unlimited number of fields, all things flattened as its name described.
cons: only support few query: term or exist, no highlight.
object
Object is another default when you using {} in mappings.
pros: nature supported, also for inner object
cons: not support array of object. Elasticsearch actually has no concept of inner object.Therefore, it flattens object hierarchies into a simple list of field names and values, checkout https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html#nested-arrays-flattening-objects
nested
pros: Support complex structure of array of objects or object of arrays and also keep them separately, therefore the field relation inside single object is kept. Suppose we have a candidate indices and lots of candidates have multiple education backgrounds. with nested fields, we can now retrieve candidates graduated from CMU with a major in CS.
cons: Each nested object is indexed as a separate Lucene document, 1 doc with 100 nested object will create 101 Lucene documents.
Both fields and objects inside nested have limits, default to 50 and 10000.
see also
https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html#_limits_on_nested_mappings_and_objects