Flattened field type

By default, each subfield in an object is mapped and indexed separately. If the names or types of the subfields are not known in advance, then they are mapped dynamically.

The flattened type provides an alternative approach, where the entire object is mapped as a single field. Given an object, the flattened mapping will parse out its leaf values and index them into one field as keywords. The object’s contents can then be searched through simple queries and aggregations.

This data type can be useful for indexing objects with a large or unknown number of unique keys. Only one field mapping is created for the whole JSON object, which can help prevent a mappings explosion from having too many distinct field mappings.

On the other hand, flattened object fields present a trade-off in terms of search functionality. Only basic queries are allowed, with no support for numeric range queries or highlighting. Further information on the limitations can be found in the Supported operations section.

The flattened mapping type should not be used for indexing all document content, as it treats all values as keywords and does not provide full search functionality. The default approach, where each subfield has its own entry in the mappings, works well in the majority of cases.

An flattened object field can be created as follows:

  1. PUT bug_reports
  2. {
  3. "mappings": {
  4. "properties": {
  5. "title": {
  6. "type": "text"
  7. },
  8. "labels": {
  9. "type": "flattened"
  10. }
  11. }
  12. }
  13. }
  14. POST bug_reports/_doc/1
  15. {
  16. "title": "Results are not sorted correctly.",
  17. "labels": {
  18. "priority": "urgent",
  19. "release": ["v1.2.5", "v1.3.0"],
  20. "timestamp": {
  21. "created": 1541458026,
  22. "closed": 1541457010
  23. }
  24. }
  25. }

During indexing, tokens are created for each leaf value in the JSON object. The values are indexed as string keywords, without analysis or special handling for numbers or dates.

Querying the top-level flattened field searches all leaf values in the object:

  1. POST bug_reports/_search
  2. {
  3. "query": {
  4. "term": {"labels": "urgent"}
  5. }
  6. }

To query on a specific key in the flattened object, object dot notation is used:

  1. POST bug_reports/_search
  2. {
  3. "query": {
  4. "term": {"labels.release": "v1.3.0"}
  5. }
  6. }

Supported operations

Because of the similarities in the way values are indexed, flattened fields share much of the same mapping and search functionality as keyword fields.

Currently, flattened object fields can be used with the following query types:

  • term, terms, and terms_set
  • prefix
  • range
  • match and multi_match
  • query_string and simple_query_string
  • exists

When querying, it is not possible to refer to field keys using wildcards, as in { "term": {"labels.time*": 1541457010}}. Note that all queries, including range, treat the values as string keywords. Highlighting is not supported on flattened fields.

It is possible to sort on an flattened object field, as well as perform simple keyword-style aggregations such as terms. As with queries, there is no special support for numerics — all values in the JSON object are treated as keywords. When sorting, this implies that values are compared lexicographically.

Flattened object fields currently cannot be stored. It is not possible to specify the store parameter in the mapping.

Parameters for flattened object fields

The following mapping parameters are accepted:

boost

Mapping field-level query time boosting. Accepts a floating point number, defaults to 1.0.

depth_limit

The maximum allowed depth of the flattened object field, in terms of nested inner objects. If a flattened object field exceeds this limit, then an error will be thrown. Defaults to 20. Note that depth_limit can be updated dynamically through the put mapping API.

doc_values

Should the field be stored on disk in a column-stride fashion, so that it can later be used for sorting, aggregations, or scripting? Accepts true (default) or false.

eager_global_ordinals

Should global ordinals be loaded eagerly on refresh? Accepts true or false (default). Enabling this is a good idea on fields that are frequently used for terms aggregations.

ignore_above

Leaf values longer than this limit will not be indexed. By default, there is no limit and all values will be indexed. Note that this limit applies to the leaf values within the flattened object field, and not the length of the entire field.

index

Determines if the field should be searchable. Accepts true (default) or false.

index_options

What information should be stored in the index for scoring purposes. Defaults to docs but can also be set to freqs to take term frequency into account when computing scores.

null_value

A string value which is substituted for any explicit null values within the flattened object field. Defaults to null, which means null sields are treated as if it were missing.

similarity

Which scoring algorithm or similarity should be used. Defaults to BM25.

split_queries_on_whitespace

Whether full text queries should split the input on whitespace when building a query for this field. Accepts true or false (default).