# Uncertain how to properly filter when certain fields do not always exist

**URL:** <https://discuss.elastic.co/t/uncertain-how-to-properly-filter-when-certain-fields-do-not-always-exist/15175>\
**Category:** Elasticsearch\
**Created:** [January 9, 2014, 3:17pm UTC](https://discuss.elastic.co/t/uncertain-how-to-properly-filter-when-certain-fields-do-not-always-exist/15175 "2014-01-09T15:17:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phil\_Barresi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/phil_barresi/32/1885_2.png) [@Phil\_Barresi](https://discuss.elastic.co/u/Phil_Barresi)\
**Post date:** [January 9, 2014, 3:17pm UTC](https://discuss.elastic.co/t/uncertain-how-to-properly-filter-when-certain-fields-do-not-always-exist/15175/1 "2014-01-09T15:17:29Z")

</div>

I am trying to filter based on a field that, on some objects, does not  
exist. I was under the impression that ES would match objects that don't  
have that field.

Ultimately, I am trying to filter as such:

- Field A will always exist, and should match on any of tags 1,2,3
- When it exists, either Field B or C must match any of tags 5,6,7
- When it exists, Field B must match any of tags 10, 11, 12
- When it exists, Field B or C must NOT have any of tags 15, 16, 18.

In this case, all my tags are strings. In addition, fields B and C are  
inside of another. I am uncertain if that matters.

Essentially, my object is:

{ a: ["some", "tags", "here"],  
X : {  
B: ["more", "tags", "here"],  
C: ["even", "more", "here"]  
}  
}

Ultimately, I am trying to build a whitelist and blacklist filtering system.

However, when filtering this way, I do not get any results that do not  
contain field X.

How do I properly format this filter?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/754097d9-fff7-4773-bac4-54f4fe3e5172%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/754097d9-fff7-4773-bac4-54f4fe3e5172%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Phil\_Barresi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/phil_barresi/32/1885_2.png) [@Phil\_Barresi](https://discuss.elastic.co/u/Phil_Barresi)\
**Post date:** [January 10, 2014, 12:48am UTC](https://discuss.elastic.co/t/uncertain-how-to-properly-filter-when-certain-fields-do-not-always-exist/15175/2 "2014-01-10T00:48:58Z")

</div>

Following advice I've received[http://stackoverflow.com/questions/21005516/uncertain-how-to-properly-filter-when-certain-fields-do-not-always-exist](http://stackoverflow.com/questions/21005516/uncertain-how-to-properly-filter-when-certain-fields-do-not-always-exist)from StackOverflow poster Sloan Ahrens, I made a  
test [http://sense.qbox.io/gist/8f7369e7991db56f7b1592f11245cb8bf6dc933f](http://sense.qbox.io/gist/8f7369e7991db56f7b1592f11245cb8bf6dc933f) that  
worked on a test index, but not on a main index. However, I was getting  
false matches.

I've added my index settings [http://sense.qbox.io/gist/a73c74e71df4aca6c58283bca1cde6e6071644df](http://sense.qbox.io/gist/a73c74e71df4aca6c58283bca1cde6e6071644df)and  
found that somehow, the addition of the relatedProfiles mapping makes it so  
that I get false-hits. Can anyone explain why that is?

On Thursday, January 9, 2014 10:17:29 AM UTC-5, Phil Barresi wrote:

> I am trying to filter based on a field that, on some objects, does not  
> exist. I was under the impression that ES would match objects that don't  
> have that field.
> 
> Ultimately, I am trying to filter as such:
> 
> - Field A will always exist, and should match on any of tags 1,2,3
> - When it exists, either Field B or C must match any of tags 5,6,7
> - When it exists, Field B must match any of tags 10, 11, 12
> - When it exists, Field B or C must NOT have any of tags 15, 16, 18.
> 
> In this case, all my tags are strings. In addition, fields B and C are  
> inside of another. I am uncertain if that matters.
> 
> Essentially, my object is:
> 
> { a: ["some", "tags", "here"],  
> X : {  
> B: ["more", "tags", "here"],  
> C: ["even", "more", "here"]  
> }  
> }
> 
> Ultimately, I am trying to build a whitelist and blacklist filtering  
> system.
> 
> However, when filtering this way, I do not get any results that do not  
> contain field X.
> 
> How do I properly format this filter?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/0951fad9-32d5-4ab8-801a-619ab4201030%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0951fad9-32d5-4ab8-801a-619ab4201030%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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 6, 2017, 1:57am UTC](https://discuss.elastic.co/t/uncertain-how-to-properly-filter-when-certain-fields-do-not-always-exist/15175/3 "2017-07-06T01:57:32Z")

</div>


