Tuesday, January 30, 2018

ElasticSearch–Searching through arrays of objects

To better understand how to search through array data in your ElasticSearch JSON document, it is important to know how ElasticSearch stores arrays behind the scenes.

Given a document which contains a ‘names’ array with a list of different name properties(e.g. firstname, lastname,…) using the following mapping:

With the following sample data:

When ElasticSearch indexes this document, it is stored like this:

Lucene has no concept of inner objects, so Elasticsearch flattens object hierarchies into a simple list of field names and values.

If you don’t want this behavior, you have to map the array as a nested object. Internally ElasticSearch maps these array objects as separate documents and does a child query.

No comments: