nm10
nm10

Reputation: 11

Is this possible with elasticsearch?

Below is sample format of my JSON documents which is stored inside elasticsearch.

{
    "_index": "in22",
    "_type": "event",
    "_id": "ET00009709",
    "_version": 1,
    "_score": 1,
    "_source": {
        "Group": "Event",
        "Title": "Jurassic World",
        "_boost": 3,
        "inner_hits": [
            {
            "Code": "ET00009709",
            "IsDefault": "",
            "Language": "English",
            "Format": "3D",
            "Region": "MUMBAI"
        },
        {
            "Code": "ET00009710",
            "IsDefault": "Y",
            "Language": "English",
            "Format": "2D",
            "Region": "CHEN"
        },
        {
            "Code": "ET00009713",
            "IsDefault": "",
            "Language": "Hindi",
            "Format": "2D",
            "Region": "MUMBAI"
        },
        {
            "Code": "ET00009714",
            "IsDefault": "",
            "Language": "Tamil",
            "Format": "3D",
            "Region": "MUMBAI"
        },
        {
            "Code": "ET00009715",
            "IsDefault": "",
            "Language": "Hindi",
            "Format": "3D",
            "Region": "MUMBAI"
        },
        {
            "Code": "ET00009716",
            "IsDefault": "",
            "Language": "Bengali",
            "Format": "2D",
            "Region": "MUMBAI"
        }
        ]
    }
}

Now what I want to achieve is whenever I search for Title=Jurassic World and region=MUMBAI, I should get the above document but inner_hits should not contain

{
                "Code": "ET00009710",
                "IsDefault": "Y",
                "Language": "English",
                "Format": "2D",
                "Region": "CHEN"
 }

So is this achievable?

What I tries so far is

{
  "query": {
    "nested": {
      "path": "inner_hits",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "inner_hits.Region": "MUMBAI"
              }
            }
          ]
        }
      }
    }
  }
}

But I couldn't achieve what i wanted, it didn't at all removed that block which contains Region: Chen

If anyone knows how to work around with this, please share. Thanks

Upvotes: 0

Views: 101

Answers (1)

keety
keety

Reputation: 17441

one work around is to use the inner_hit feature added in elasticsearch 1.5. This returns the nested documents which matched the nested query.

Example:

   {
   "_source": {
      "exclude": ["inner_hits.*"]
   },
   "query": {
      "nested": {
         "path": "inner_hits",
         "query": {
            "bool": {
               "must": [
                  {
                     "match": {
                        "inner_hits.Region": "MUMBAI"
                     }
                  }
               ]
            }
         },
         "inner_hits": {
              "size" : 10
          }
      }
   }
}

Response

"hits": [
     {
        "_index": "test",
        "_type": "test",
        "_id": "1",
        "_score": 3.8630462,
        "_source": {
           "Group": "Event",
           "_boost": 3,
           "inner_hits": [],
           "Title": "Jurassic World"
        },
        "inner_hits": {
           "inner_hits": {
              "hits": {
                 "total": 2,
                 "max_score": 3.8630462,
                 "hits": [
                    {
                       "_index": "test",
                       "_type": "test",
                       "_id": "1",
                       "_nested": {
                          "field": "inner_hits",
                          "offset": 2
                       },
                       "_score": 3.8630462,
                       "_source": {
                          "Code": "ET00009713",
                          "IsDefault": "",
                          "Language": "Hindi",
                          "Format": "2D",
                          "Region": "MUMBAI"
                       }
                    },
                    {
                       "_index": "test",
                       "_type": "test",
                       "_id": "1",
                       "_nested": {
                          "field": "inner_hits",
                          "offset": 0
                       },
                       "_score": 3.8630462,
                       "_source": {
                          "Code": "ET00009709",
                          "IsDefault": "",
                          "Language": "English",
                          "Format": "3D",
                          "Region": "MUMBAI"
                       }
                    }
                 ]
              }
           }
        }
     }
  ]

There is an additional inner_hits in the response besides the _source which contains only the nested documents that matched. By default inner_hits returns the top 3 nested docs. You can tweak this with the size option.

Upvotes: 1

Related Questions