# Filter a filed so that it matches if is has a certain value or if it is empty

**URL:** <https://discuss.elastic.co/t/filter-a-filed-so-that-it-matches-if-is-has-a-certain-value-or-if-it-is-empty/13907>\
**Category:** Elasticsearch\
**Created:** [October 10, 2013, 3:08pm UTC](https://discuss.elastic.co/t/filter-a-filed-so-that-it-matches-if-is-has-a-certain-value-or-if-it-is-empty/13907 "2013-10-10T15:08:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bruno\_ais](https://avatars.discourse-cdn.com/v4/letter/b/50afbb/32.png) [@bruno\_ais](https://discuss.elastic.co/u/bruno_ais)\
**Post date:** [October 10, 2013, 3:08pm UTC](https://discuss.elastic.co/t/filter-a-filed-so-that-it-matches-if-is-has-a-certain-value-or-if-it-is-empty/13907/1 "2013-10-10T15:08:35Z")

</div>

How do I do a search so that it matches if a certain parameter is an empty  
array or if it has a certain set of values.  
I'm trying something like this:

...  
"filters": [  
{  
"script" : {  
"script" :  
"doc['filed.subfield.all'].values.length == 0"  
}  
},  
{  
"and":{  
"filters": [  
{  
"term": {  
"filed.subfield.all": 123  
}  
}  
]  
}  
}  
]  
...

but it throws the following error:  
Query Failed [Failed to execute main query]]; nested:  
PropertyAccessException[[Error: could not access: length; in class:  
java.util.Collections$EmptyList] [Near : {... doc['"filed.subfield.all'].value  
....}] ^ [Line: 1, Column: 1]]; }]

Can anyone get why I'm getting this?

An example data structure (one of the ones I'm trying to get):

{  
"data": "123 can",  
"subfiled": {  
"all":[],  
"some":[  
123  
]  
}  
}

Another example data structure (one of the ones I'm trying _not_ to get):

{  
"data": "123 can",  
"subfiled": {  
"all":[123,321],  
"some":[]  
}  
}

I didn't give more information because I think you don't require but if you  
still require more information, just ask.

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![johtani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/johtani/32/44956_2.png) [@johtani](https://discuss.elastic.co/u/johtani)\
**Post date:** [October 10, 2013, 4:58pm UTC](https://discuss.elastic.co/t/filter-a-filed-so-that-it-matches-if-is-has-a-certain-value-or-if-it-is-empty/13907/2 "2013-10-10T16:58:14Z")

</div>

Hi bruno,

You try to use "doc['filed.subfield.all'].empty" instead of "values.length == 0".  
See [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html)

I try to use this query, then i get data that include "all":.

{  
"query": {  
"filtered": {  
"query": {"match\_all": {}},  
"filter": {  
"script": {  
"script": "doc['subfiled.all'].empty"  
}  
}  
}  
}  
}

* * *

Jun Ohtani  
[johtani@gmail.com](mailto:johtani@gmail.com)  
blog : [http://blog.johtani.info](http://blog.johtani.info)  
twitter : [http://twitter.com/johtani](http://twitter.com/johtani)

On 2013/10/11, at 0:08, bruno ais [brunoaiss@gmail.com](mailto:brunoaiss@gmail.com) wrote:

> How do I do a search so that it matches if a certain parameter is an empty array or if it has a certain set of values.  
> I'm trying something like this:
> 
> ...  
> "filters": [  
> {  
> "script" : {  
> "script" : "doc['filed.subfield.all'].values.length == 0"  
> }  
> },  
> {  
> "and":{  
> "filters": [  
> {  
> "term": {  
> "filed.subfield.all": 123  
> }  
> }  
> ]  
> }  
> }  
> ]  
> ...
> 
> but it throws the following error:  
> Query Failed [Failed to execute main query]]; nested: PropertyAccessException[[Error: could not access: length; in class: java.util.Collections$EmptyList] [Near : {... doc['"filed.subfield.all'].value ....}] ^ [Line: 1, Column: 1]]; }]
> 
> Can anyone get why I'm getting this?
> 
> An example data structure (one of the ones I'm trying to get):
> 
> {  
> "data": "123 can",  
> "subfiled": {  
> "all":,  
> "some":[  
> 123  
> ]  
> }  
> }
> 
> Another example data structure (one of the ones I'm trying not to get):
> 
> {  
> "data": "123 can",  
> "subfiled": {  
> "all":[123,321],  
> "some":  
> }  
> }
> 
> I didn't give more information because I think you don't require but if you still require more information, just ask.
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
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, 2:12am UTC](https://discuss.elastic.co/t/filter-a-filed-so-that-it-matches-if-is-has-a-certain-value-or-if-it-is-empty/13907/3 "2017-07-06T02:12:59Z")

</div>


