0
votes

Say I'm indexing elasticsearch data like so:

{"entities": {

   "type": "firstName",
   "value": "Barack",
},
{
   "type": "lastName",
   "value": "Obama"
}}

I'd like users to be able to add custom attributes, so I don't know every possible value of "type" ahead of time.

My mappings might look like:

typename:
   entities:
      type: nested

If I do a match query for the text "Obama", with highlighting, is there a way to get back the full nested "entity" which matched? I would like to know if my query for "Obama" matched the firstName or the lastName.

1
inner_hits gives you the nested documents that matched: elastic.co/guide/en/elasticsearch/reference/2.4/… - Andrei Stefan

1 Answers

0
votes

I was able to solve this with inner_hits (thanks Andrei!)

{
   "query": {
       "nested": {
          "query": {

               {"match": {"entities.name": "Obama"}}

           }
        },
        "inner_hits": {
          "highlight": {
             "fields": {
                 "entities.name": {}
              }
           }
        }
   }
}