Using source filtering to get nested object count


(Steve Wall) #1

I have an index with documents like this:

 {
   "firstName": "Fred",
   "lastName": "Flinstone",
   "children": [
      {
         "firstName": "Pebbles",
         "lastName": "Flinstone",
         "age": 1
      }
   ]
 }

When I do my search query, I want to get back just 3 fields: firstName, lastName and then the number of children. I am currently using Elasticsearch 5 and source filtering to include the firstName and lastName. The first two fields are not a problem, but how do I get a count of the nested children object to be returned?

In the end, I want my search result hits _source to be something like this:

"_source": {
    "firstName": "Fred",
    "lastName": "Flinstone",
    "numberOfChildren": 1
}

(Alexander Reelsen) #2

Hey,

source filtering is not a scripting language, but really just allows you to include or exclude parts of the JSON document - no more, no less.

You could either use scripting on query time to return the number of children or just add a field during indexing that contains the number of children.

If you however want to execute searches on the children and only get back those children that matched, you should take a look at the inner_hits feature.

--Alex


(system) #3

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