# Searching boolean keys within nested object in Elasticsearch

**URL:** <https://discuss.elastic.co/t/searching-boolean-keys-within-nested-object-in-elasticsearch/27779>\
**Category:** Elasticsearch\
**Created:** [August 20, 2015, 5:27pm UTC](https://discuss.elastic.co/t/searching-boolean-keys-within-nested-object-in-elasticsearch/27779 "2015-08-20T17:27:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sach](https://avatars.discourse-cdn.com/v4/letter/s/858c86/32.png) [@sach](https://discuss.elastic.co/u/sach)\
**Post date:** [August 20, 2015, 5:27pm UTC](https://discuss.elastic.co/t/searching-boolean-keys-within-nested-object-in-elasticsearch/27779/1 "2015-08-20T17:27:49Z")

</div>

I have "user" object and it has nested object "status" which has few boolean flags. I am able to search with name and phone values. I need to search all the users which has that boolean flag. For example search all users which has flag1 status set. Preferable solution will be during indexing time. Just FYI I am using couchdb river to populate the data.

Data structure -  
user:{ name: ABC, phone: 1234567890, status: { flag1: true, flag9: true } }

---

<div class="post-metadata">

**Author:** ![emptyemail](https://avatars.discourse-cdn.com/v4/letter/e/6de8d8/32.png) [@emptyemail](https://discuss.elastic.co/u/emptyemail)\
**Post date:** [August 20, 2015, 7:03pm UTC](https://discuss.elastic.co/t/searching-boolean-keys-within-nested-object-in-elasticsearch/27779/2 "2015-08-20T19:03:52Z")

</div>

you will need to use a nested filter, note the 'status.flag1'

{nested: {path: 'status',filter: {term: 'status.flag1': true}}}

You might want to consider using regular objects rather than nesting in this case, I've found that nesting only helps if you are planning to update the nested objects separately and much more frequently than the main object.

---

<div class="post-metadata">

**Author:** ![sach](https://avatars.discourse-cdn.com/v4/letter/s/858c86/32.png) [@sach](https://discuss.elastic.co/u/sach)\
**Post date:** [August 20, 2015, 8:58pm UTC](https://discuss.elastic.co/t/searching-boolean-keys-within-nested-object-in-elasticsearch/27779/3 "2015-08-20T20:58:19Z")

</div>

Thanks for reply, in this case yes nested object will be updated more often. Just confirming during indexing in mapping I should set the filter on nested object and then if I search for flag1 I will get appropriate results. correct?

---

<div class="post-metadata">

**Author:** ![emptyemail](https://avatars.discourse-cdn.com/v4/letter/e/6de8d8/32.png) [@emptyemail](https://discuss.elastic.co/u/emptyemail)\
**Post date:** [August 20, 2015, 9:26pm UTC](https://discuss.elastic.co/t/searching-boolean-keys-within-nested-object-in-elasticsearch/27779/4 "2015-08-20T21:26:01Z")

</div>

Your mapping document should say that status property is a nested property, during indexing you are just indexing the document as is. You would use the nested filter as part of your query for the user objects. If that doesn't work it might be easier if you post relevant parts of your mapping document along with a sample query.

---

<div class="post-metadata">

**Author:** ![sach](https://avatars.discourse-cdn.com/v4/letter/s/858c86/32.png) [@sach](https://discuss.elastic.co/u/sach)\
**Post date:** [August 20, 2015, 10:46pm UTC](https://discuss.elastic.co/t/searching-boolean-keys-within-nested-object-in-elasticsearch/27779/5 "2015-08-20T22:46:08Z")

</div>

Thank You so much, I got it working using nested mapping and nested filter.

However, is there a way if we enter flag1 as query term and get all the user objects which has flag1?

---

<div class="post-metadata">

**Author:** ![emptyemail](https://avatars.discourse-cdn.com/v4/letter/e/6de8d8/32.png) [@emptyemail](https://discuss.elastic.co/u/emptyemail)\
**Post date:** [August 20, 2015, 11:03pm UTC](https://discuss.elastic.co/t/searching-boolean-keys-within-nested-object-in-elasticsearch/27779/6 "2015-08-20T23:03:04Z")

</div>

If you just use the nested query then that's what you should get back

---

<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:** [July 5, 2017, 11:54pm UTC](https://discuss.elastic.co/t/searching-boolean-keys-within-nested-object-in-elasticsearch/27779/7 "2017-07-05T23:54:44Z")

</div>

Closed on July 5, 2017, 11:54pm UTC


