Search query to search in a nested type object

I have a source document that looks like this.

   "_id" : "1",
   "_source" : {
      "Id" : "1",
      "details" : [
        {
          "propId" : "test8",
          "liked" : false,
          "updatedBy" : "userId",
          "timestamp" : "2019-12-11T23:24:59.759+0000"
        },
        {
          "propId" : "test9",
          "liked" : true,
          "updatedBy" : "userId",
          "timestamp" : "2019-12-11T23:24:59.759+0000"
        }
      ]
    }

The details object is a nested type.
I'm trying to do 2 types of searches. One is to get_by_propId and the other is to get objects from the array where liked is true

I have this query for the first type of search at least. It's returning the entire object though.

GET /listing_preferences/_search
{
    "query": {
      "nested": {
        "path": "details",
        "query": {"bool": {
          "filter": [ 
                {"match": {"_id": "1"}},
                {"match": {"details.propId": "test8"}}
            ],
        
        "adjust_pure_negative": true,
        "boost": 1
    }}
  }
    
}

}

Is it possible to get the two type of searches working without any parent-child relationship?

For the part 1 where you want to get only the nested documents that matches the id to be returned, all you have to do is to use inner_hits inside the nested query. This will add an additional key inner_hits in the response under which you'll get only those nested documents which matched the query.

GET /listing_preferences/_search
{
  "query": {
    "nested": {
      "inner_hits": {}, 
      "path": "details",
      "query": {
        "bool": {
          "filter": [
            {
              "match": {
                "_id": "1"
              }
            },
            {
              "match": {
                "details.propId": "test8"
              }
            }
          ],
          "adjust_pure_negative": true,
          "boost": 1
        }
      }
    }
  }

For the second part i.e. to get only the nested documents where value of liked field is true you can create a similar query for the field liked and use inner_hits as in the above query.

Thank you. That's exactly what I wanted. I got it to work using inner_hits.

I do have a similar question question though. Is it possible to do something similar if I had a map of objects instead of a nested object? So suppose if my source looked like the following:

        "_id" : "1",
        "_source" : {
          "Id" : "1",
          "test29" : {
            "propId" : "test29",
            "liked" : false,
            "updatedBy" : "userId"
          },
          "test30" : {
            "updatedBy" : "userId",
            "propId" : "test30",
            "liked" : true
          },
          "test31" : {
            "updatedBy" : "userId",
            "propId" : "test31",
            "liked" : true
          }
        }

Is it still possible to get the two queries to work?

Inner hits works for parent-child relationship or nested objects. If the field is of object type you won't be able to apply same to it.

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