# Filtering nested objects

**URL:** <https://discuss.elastic.co/t/filtering-nested-objects/291911>\
**Category:** Kibana\
**Created:** [December 15, 2021, 7:35am UTC](https://discuss.elastic.co/t/filtering-nested-objects/291911 "2021-12-15T07:35:41Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![dev\_test](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dev_test/32/79287_2.png) [@dev\_test](https://discuss.elastic.co/u/dev_test)\
**Post date:** [December 15, 2021, 7:35am UTC](https://discuss.elastic.co/t/filtering-nested-objects/291911/1 "2021-12-15T07:35:41Z")

</div>

Hi, I'm trying to build a system that will have a user's phone book in nested field and this will have user\_id and user\_gender. I am trying to filter over gender in search result but i'm only getting the whole nested object in response of my query.  
I'm wondering if there exists any mechanism that allows you to filter the array of nested objects and output specific fields  
I'm using Elasticsearch 7.16  
Below is the sample data for one user

```auto
"_source" : {
          "user_id" : 1,
          "user_gender" : 1,
          "user_location" : {
            "lat" : 33.765,
            "lon" : 74.444
          },
          "phone_book" : [
            {
              "user_id" : 2,
              "user_gender" : 1,
              "user_location" : {
                "lat" : 33.665,
                "lon" : 74.444
              }
            },
            {
              "user_id" : 3,
              "user_gender" : 0,
              "user_location" : {
                "lat" : 33.755,
                "lon" : 74.444
              }
            },
            {
              "user_id" : 4,
              "user_gender" : 0,
              "user_location" : {
                "lat" : 33.755,
                "lon" : 74.444
              }
            },
            {
              "user_id" : 5,
              "user_gender" : 1,
              "user_location" : {
                "lat" : 33.755,
                "lon" : 74.434
              }
            },
            {
              "user_id" : 6,
              "user_gender" : 0,
              "user_location" : {
                "lat" : 33.655,
                "lon" : 74.414
              }
            }
          ],
          "user_relations" : {
            "name" : "user"
          }
        }

```

---

<div class="post-metadata">

**Author:** ![maph](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@maph](https://discuss.elastic.co/u/maph)\
**Post date:** [December 23, 2021, 12:44pm UTC](https://discuss.elastic.co/t/filtering-nested-objects/291911/2 "2021-12-23T12:44:21Z")

</div>

Did you find a solution to your problem? I have a similar issue, and am curious to hear if and how you managed to solve it?

---

<div class="post-metadata">

**Author:** ![dev\_test](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dev_test/32/79287_2.png) [@dev\_test](https://discuss.elastic.co/u/dev_test)\
**Post date:** [December 24, 2021, 12:47pm UTC](https://discuss.elastic.co/t/filtering-nested-objects/291911/3 "2021-12-24T12:47:04Z")

</div>

Hi Michael,  
No i did not found any solution to filter over nested data type. I guess Elasticsearch doesn't support this thing as it return us the whole document or specific fields that we mention in the \_source list. As nested type is a single field and all of the objects inside it are associated with that particular document and when we require this field in the resulting document it get all of the nested objects present in it.

what i did was that i changed the mapping of the phone\_book from nested to join with reverse relation i.e both when a user will save a contact that is on our platform, the system will index two documents one having the info of that user say B whose contact is just saved by former say A (parent A) and other child document will have information of user A and this will have its parent B. so searching and filtering on child documents will be easy.

you may not need this reverse relation thing.  
if you find some way to achieve filtering in nested objects do share with me as well -\_- 🙂

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [December 26, 2021, 12:17pm UTC](https://discuss.elastic.co/t/filtering-nested-objects/291911/4 "2021-12-26T12:17:14Z")

</div>

Hi @dev_test and @maph

I'm not sure what "output specific fields" mean. Is it "output specific nested object"??  
If so, you can filter nested objects by [nested query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html) and [inner\_hits](https://www.elastic.co/guide/en/elasticsearch/reference/current/inner-hits.html) option.

If you mean filtering field of nested objects, you can use \_source option for inner\_hits.

```auto
{
  "query":{
    "nested":{
      "path": "phone_book",
      "query":{
        "term":{
          "phone_book.user_gender":{
            "value": 1
          }
        }
      },
      "inner_hits": {
        "_source":["phone_book.user_id", "phone_book.user_gender"]
      }
    }
  },
  "_source": ["user_id", "user_gender"]
}

```

---

<div class="post-metadata">

**Author:** ![dev\_test](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dev_test/32/79287_2.png) [@dev\_test](https://discuss.elastic.co/u/dev_test)\
**Post date:** [December 30, 2021, 11:05am UTC](https://discuss.elastic.co/t/filtering-nested-objects/291911/5 "2021-12-30T11:05:36Z")

</div>

Thanks,

---

<div class="post-metadata">

**Author:** ![maph](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@maph](https://discuss.elastic.co/u/maph)\
**Post date:** [January 5, 2022, 3:41pm UTC](https://discuss.elastic.co/t/filtering-nested-objects/291911/6 "2022-01-05T15:41:13Z")

</div>

Hi @Tomo_M and @dev_test,

Thank you for both of your replies, and also happy new year to you.

I'm not able to change the mappings of the index, as it's a public data source which I'm accessing but have no control over. But glad you found a solution @dev_test !

@Tomo_M, thanks for that suggestion. I've tried using inner\_hits in a nested query as well, without success. Perhaps it's because I'm a novice at Elasticsearch, but I am looking to query a particular ID with a boolean match query, and then return several different data objects, including certain objects within a nested object. But I'm not interested in also having to query the nested object itself, I just want to filter it's response so I only get some of what it contains back. Does that make sense?

I can try to post an example, if necessary? Thanks for your time!

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [January 6, 2022, 3:33pm UTC](https://discuss.elastic.co/t/filtering-nested-objects/291911/8 "2022-01-06T15:33:21Z")

</div>

How about this? The result seems to match your interest. I used boolean "should" clause and minimum\_should\_match = 0, because to return documents without nested object matching the filter.

```auto
GET test_nested_field/_search
{
  "query":{
    "bool":{
      "filter":[{
        "term":{
          "user_id": "1"
        }
      }],
      "should":[{
        "nested":{
          "path": "phone_book",
          "query":{
            "term":{
              "phone_book.user_gender":{
                "value": 1
              }
            }
          },
          "inner_hits": {
            "_source":["phone_book.user_id", "phone_book.user_gender"]
          }
        }
      }
      ],
      "minimum_should_match": 0
      }
  },
  "_source": ["user_id", "user_gender"]
}

```

```auto
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test_nested_field",
        "_type" : "_doc",
        "_id" : "1Lv7L34Bf0nakUP8DwBZ",
        "_score" : 1.0,
        "_source" : {
          "user_id" : 1,
          "user_gender" : 1
        },
        "inner_hits" : {
          "phone_book" : {
            "hits" : {
              "total" : {
                "value" : 2,
                "relation" : "eq"
              },
              "max_score" : 1.0,
              "hits" : [
                {
                  "_index" : "test_nested_field",
                  "_type" : "_doc",
                  "_id" : "1Lv7L34Bf0nakUP8DwBZ",
                  "_nested" : {
                    "field" : "phone_book",
                    "offset" : 0
                  },
                  "_score" : 1.0,
                  "_source" : {
                    "user_id" : 2,
                    "user_gender" : 1
                  }
                },
                {
                  "_index" : "test_nested_field",
                  "_type" : "_doc",
                  "_id" : "1Lv7L34Bf0nakUP8DwBZ",
                  "_nested" : {
                    "field" : "phone_book",
                    "offset" : 3
                  },
                  "_score" : 1.0,
                  "_source" : {
                    "user_id" : 5,
                    "user_gender" : 1
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

```

If you try but without success, paste the query and return (and your desired output), then someone will help you!

---

<div class="post-metadata">

**Author:** ![maph](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@maph](https://discuss.elastic.co/u/maph)\
**Post date:** [January 7, 2022, 2:47pm UTC](https://discuss.elastic.co/t/filtering-nested-objects/291911/9 "2022-01-07T14:47:24Z")

</div>

Thanks for trying, Tomo!

I tried adapting the query to the index and mapping I'm looking at, but couldn't get it to work.

The nested object has the following mapping:

```auto
"attributter": {                 
             "type": "nested",                 
             "include_in_parent": true,                 
             "properties": {                   
                 "sekvensnr": {                     
                   "type": "long"                   
              },                   
              "type": {                     
              "type": "string"                   
              },                   
              "vaerdier": {                     
              "type": "nested",                     
              "include_in_parent": true,                     
              "properties": {                       
                   "periode": {                        
                   "properties": {                           
                        "gyldigFra": {                             
                        "type": "date",                             
                    "format": "dateOptionalTime"                           
                     },                           
                     "gyldigTil": {                             
                          "type": "date",                             
                          "format": "dateOptionalTime"                           
                       }                         
                     }                       
                   },                       
                   "sidstOpdateret": {                         
                   "type": "date",                         
                   "format": "dateOptionalTime"                       
                    },                       
                   "vaerdi": {                         
                   "type": "string"                       
                    }                     
                  }                   
                },                   
               "vaerditype": {                     
               "type": "string"                  
              }                 
            }               
         },

```

I'm sending the following request, quering for a term elsewhere in the index:

```auto
  http://distribution.virk.dk/cvr-permanent/virksomhed/_search -d '
  {
    "query":{
      "bool":{
        "filter":[{
          "term":{
            "Vrvirksomhed.cvrNummer": "35128417"
          }
        }],
        "should":[{
          "nested":{
            "path": "Vrvirksomhed.attributter",
            "query":{
              "term":{
                "Vrvirksomhed.attributter.KAPITAL":{
                  "value": 1
                }
              }
            },
            "inner_hits": {
              "_source":["Vrvirksomhed.attributter.KAPITAL"]
            }
          }
        }
        ],
        "minimum_should_match": 0
        }
    },
    "_source": ["Vrvirksomhed.attributter.KAPITAL"]
  }' | python -mjson.tool

```

But it returns an empty 'succesful' result:

```auto
{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 6,
        "successful": 6,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.0,
        "hits": [
            {
                "_index": "cvr-v-20200115",
                "_type": "_doc",
                "_id": "4000333365",
                "_score": 0.0,
                "_source": {},
                "inner_hits": {
                    "Vrvirksomhed.attributter": {
                        "hits": {
                            "total": 0,
                            "max_score": null,
                            "hits": []
                        }
                    }
                }
            }
        ]
    }
}

```

And if I change the path, it gives me an error. But I guess it's the path that I'm somehow getting wrong?

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [January 7, 2022, 3:08pm UTC](https://discuss.elastic.co/t/filtering-nested-objects/291911/10 "2022-01-07T15:08:37Z")

</div>

I cant find KAPITAL field in attributter.

---

<div class="post-metadata">

**Author:** ![maph](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@maph](https://discuss.elastic.co/u/maph)\
**Post date:** [January 7, 2022, 3:26pm UTC](https://discuss.elastic.co/t/filtering-nested-objects/291911/11 "2022-01-07T15:26:32Z")

</div>

Yeah, the documentation is problematic because it says that the list of attributes has four areas:

- DIVERSE
- KAPITALFORHOLD
- KATEGORISERING
- REGNSKABSPERIODE
- TEKSTER
- Udenlandske revisionsvirksomheder
- HvidVask

Each of these areas then have a number of attributter, where KAPITAL should be under KAPITALFORHOLD:

> For KAPITALFORHOLD findes følgende attributter:
> 
> - BØRSNOTERET
> - KAPITAL
> - KAPITAL\_DELVIST
> - KAPITALKLASSE
> - KAPITALVALUTA

Yet if I try

```auto
Vrvirksomhed.attributter.KAPITALFORHOLD.KAPITAL

```

I get an error message...

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [January 7, 2022, 3:39pm UTC](https://discuss.elastic.co/t/filtering-nested-objects/291911/12 "2022-01-07T15:39:46Z")

</div>

Maybe there is another problem with nested query (Not just selecting the output, but querying itself). It is something different from the original topic here.

I recommend you to post a new topic. Since the Russian field names are difficult to understand and even impossible to type for the most users including me, I suggest building a simpler example that reproduces the problem and share the whole mappings and the query.

---

<div class="post-metadata">

**Author:** ![maph](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@maph](https://discuss.elastic.co/u/maph)\
**Post date:** [January 7, 2022, 3:45pm UTC](https://discuss.elastic.co/t/filtering-nested-objects/291911/13 "2022-01-07T15:45:10Z")

</div>

Yeah, I suspected it might be something with the way it's nested.

I'll look to post a new topic, with a more clear description. It's Danish, not Russian, but that doesn't help you much I know. 😅

Thanks a lot for your help!

---

<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:** [February 4, 2022, 3:45pm UTC](https://discuss.elastic.co/t/filtering-nested-objects/291911/14 "2022-02-04T15:45:23Z")

</div>

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