# Filtering on array

**URL:** <https://discuss.elastic.co/t/filtering-on-array/9091>\
**Category:** Elasticsearch\
**Created:** [September 21, 2012, 8:40am UTC](https://discuss.elastic.co/t/filtering-on-array/9091 "2012-09-21T08:40:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan\_Fairs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dan_fairs/32/1205_2.png) [@Dan\_Fairs](https://discuss.elastic.co/u/Dan_Fairs)\
**Post date:** [September 21, 2012, 8:40am UTC](https://discuss.elastic.co/t/filtering-on-array/9091/1 "2012-09-21T08:40:13Z")

</div>

Hi,

I have a load of documents indexed, and they have a 'tags' key with an array of, unsurprisingly, tags - all not\_analyzed.

I'd like to search my documents, excluding those with a certain tag or tags. However, I'd like a document to be included in search results if it has any further tags. For example, consider the documents:

{ 'tags': ['foo'], '\_id': 'a' }  
{ 'tags': ['foo', 'bar'], '\_id': 'b'}

Searching with the exclusion of 'foo' should still return document id 'b', as it contains the tag 'bar' as well.

Is this something I should be looking at scripts for, or is there a cleaner way within the existing query DSL?

## Cheers, Dan

Dan Fairs | [dan.fairs@gmail.com](mailto:dan.fairs@gmail.com) | @danfairs | [www.fezconsulting.com](http://www.fezconsulting.com)

--

---

<div class="post-metadata">

**Author:** ![Dan\_Fairs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dan_fairs/32/1205_2.png) [@Dan\_Fairs](https://discuss.elastic.co/u/Dan_Fairs)\
**Post date:** [September 21, 2012, 7:34pm UTC](https://discuss.elastic.co/t/filtering-on-array/9091/2 "2012-09-21T19:34:13Z")

</div>

> I have a load of documents indexed, and they have a 'tags' key with an array of, unsurprisingly, tags - all not\_analyzed.
> 
> I'd like to search my documents, excluding those with a certain tag or tags. However, I'd like a document to be included in search results if it has any further tags. For example, consider the documents:
> 
> { 'tags': ['foo'], '\_id': 'a' }  
> { 'tags': ['foo', 'bar'], '\_id': 'b'}
> 
> Searching with the exclusion of 'foo' should still return document id 'b', as it contains the tag 'bar' as well.
> 
> Is this something I should be looking at scripts for, or is there a cleaner way within the existing query DSL?

For the benefit of the archives, the script is the route I went down (native script, as it happened), and it seems to be working well.

There isn't much in the way of documentation out there writing native scripts used as filters - at least, not that I could find. It took a bit of trial and error to get things going. In the end, I subclassed AbstractSearchScript, and overrode run() to return a boolean to indicate whether the document should pass the filter or not.

## Cheers, Dan

Dan Fairs | [dan.fairs@gmail.com](mailto:dan.fairs@gmail.com) | @danfairs | [www.fezconsulting.com](http://www.fezconsulting.com)

--

---

<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, 3:11am UTC](https://discuss.elastic.co/t/filtering-on-array/9091/3 "2017-07-06T03:11:52Z")

</div>


