# Search query to search in a nested type object

**URL:** <https://discuss.elastic.co/t/search-query-to-search-in-a-nested-type-object/211721>\
**Category:** Elasticsearch\
**Created:** [December 12, 2019, 9:00pm UTC](https://discuss.elastic.co/t/search-query-to-search-in-a-nested-type-object/211721 "2019-12-12T21:00:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![afzal](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@afzal](https://discuss.elastic.co/u/afzal)\
**Post date:** [December 12, 2019, 9:00pm UTC](https://discuss.elastic.co/t/search-query-to-search-in-a-nested-type-object/211721/1 "2019-12-12T21:00:48Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Opster\_Community1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/opster_community1/32/59062_2.png) [@Opster\_Community1](https://discuss.elastic.co/u/Opster_Community1)\
**Post date:** [December 13, 2019, 3:38am UTC](https://discuss.elastic.co/t/search-query-to-search-in-a-nested-type-object/211721/2 "2019-12-13T03:38:57Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-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.

---

<div class="post-metadata">

**Author:** ![afzal](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@afzal](https://discuss.elastic.co/u/afzal)\
**Post date:** [December 15, 2019, 11:50pm UTC](https://discuss.elastic.co/t/search-query-to-search-in-a-nested-type-object/211721/3 "2019-12-15T23:50:47Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Opster\_Community1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/opster_community1/32/59062_2.png) [@Opster\_Community1](https://discuss.elastic.co/u/Opster_Community1)\
**Post date:** [December 16, 2019, 3:41am UTC](https://discuss.elastic.co/t/search-query-to-search-in-a-nested-type-object/211721/4 "2019-12-16T03:41:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 13, 2020, 3:41am UTC](https://discuss.elastic.co/t/search-query-to-search-in-a-nested-type-object/211721/5 "2020-01-13T03:41:09Z")

</div>

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