# Array intersect filter?

**URL:** https://discuss.elastic.co/t/array-intersect-filter/11100
**Category:** Elasticsearch
**Created:** [March 11, 2013, 4:17pm UTC](https://discuss.elastic.co/t/array-intersect-filter/11100 "2013-03-11T16:17:44Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Rue](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rue/32/2443_2.png) [@Rue](https://discuss.elastic.co/u/Rue)
#### Post date: [March 11, 2013, 4:17pm UTC](https://discuss.elastic.co/t/array-intersect-filter/11100/1 "2013-03-11T16:17:44Z")

</div>

I'm trying to filter results based on whether an array field has at least  
one element from another set.

I have a field containing an array of integers eg:

\_source: {  
...  
thing\_ids: [  
21029,  
21115,  
20996,  
21080,  
20997  
]  
}

I'd like to filter the results such that thing\_ids contains at least one of  
a list of values, eg:

[1, 2, 3, 21115] # would return the above document  
[1, 2, 3, 4] # would not

It doesn't really matter how many it contains, just that it matches at  
least one of them.

I'm trying something like this but can't quite tell if this is correct:

{  
"query":{"bool":{"must":[{"query\_string":{"query":"search  
query","fields":["description","title"],"default\_operator":"AND","use\_dis\_max":true}}]}},  
"filter":{  
"or":[  
{"term":{"thing\_ids":22003}},  
{"term":{"thing\_ids":21757}},  
{"term":{"thing\_ids":21610}}  
]  
}  
}

Any thoughts?

--  
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: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [March 11, 2013, 4:32pm UTC](https://discuss.elastic.co/t/array-intersect-filter/11100/2 "2013-03-11T16:32:43Z")

</div>

I would probably use: [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/terms-filter.html)  
{  
"constant\_score" : {  
"filter" : {  
"terms" : { "thing\_ids" : [22003, 21757, 21610]}  
}  
}  
}

Does it fit to your use case?

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr | @scrutmydocs

Le 11 mars 2013 à 17:17, Rue [rue@thinlayer.co.uk](mailto:rue@thinlayer.co.uk) a écrit :

> I'm trying to filter results based on whether an array field has at least one element from another set.
> 
> I have a field containing an array of integers eg:
> 
> \_source: {  
> ...  
> thing\_ids: [  
> 21029,  
> 21115,  
> 20996,  
> 21080,  
> 20997  
> ]  
> }
> 
> I'd like to filter the results such that thing\_ids contains at least one of a list of values, eg:
> 
> [1, 2, 3, 21115] # would return the above document  
> [1, 2, 3, 4] # would not
> 
> It doesn't really matter how many it contains, just that it matches at least one of them.
> 
> I'm trying something like this but can't quite tell if this is correct:
> 
> {  
> "query":{"bool":{"must":[{"query\_string":{"query":"search query","fields":["description","title"],"default\_operator":"AND","use\_dis\_max":true}}]}},  
> "filter":{  
> "or":[  
> {"term":{"thing\_ids":22003}},  
> {"term":{"thing\_ids":21757}},  
> {"term":{"thing\_ids":21610}}  
> ]  
> }  
> }
> 
> Any thoughts?
> 
> --  
> 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: ![Rue](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rue/32/2443_2.png) [@Rue](https://discuss.elastic.co/u/Rue)
#### Post date: [March 12, 2013, 10:16am UTC](https://discuss.elastic.co/t/array-intersect-filter/11100/3 "2013-03-12T10:16:00Z")

</div>

Thanks David.

From the docs it looked like that was exactly what I was looking for (it  
may still be) but with it I am getting every document returned whatever I  
put in it. Whatever values (including one that should return no results)  
gives me all docs in the index.

It's bound to be something in my usage:

{  
"query": { "bool": { "must": [ { "term": { "state": { "term": "live" } }  
}, { "term": { "library\_id": { "term": 11 } } } ], "must\_not": [ { "term":  
{ "state": { "term": "deleted" } } } ] },  
"constant\_score": {  
"filter": {  
"terms": {  
"thingy\_ids": [  
21029  
]  
}  
}  
}  
},  
"sort": [{ "taken\_at": "desc" }],  
"size": 25,  
"from": 0  
}

On Monday, 11 March 2013 16:32:43 UTC, David Pilato wrote:

> I would probably use:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/terms-filter.html)
> 
> {  
> "constant\_score" : {  
> "filter" : {  
> "terms" : { "thing\_ids" : [22003, 21757, 21610]}  
> }  
> }  
> }
> 
> Does it fit to your use case?
> 
> --  
> _David Pilato_ | _Technical Advocate_ | _[Elasticsearch.com](http://Elasticsearch.com)_  
> @dadoonet [https://twitter.com/dadoonet](https://twitter.com/dadoonet) | @elasticsearchfr[https://twitter.com/elasticsearchfr](https://twitter.com/elasticsearchfr)  
> | @scrutmydocs [https://twitter.com/scrutmydocs](https://twitter.com/scrutmydocs)
> 
> Le 11 mars 2013 à 17:17, Rue \<[r...@thinlayer.co.uk](mailto:r...@thinlayer.co.uk) \<javascript:\>\> a écrit  
> :
> 
> I'm trying to filter results based on whether an array field has at least  
> one element from another set.
> 
> I have a field containing an array of integers eg:
> 
> \_source: {  
> ...  
> thing\_ids: [  
> 21029,  
> 21115,  
> 20996,  
> 21080,  
> 20997  
> ]  
> }
> 
> I'd like to filter the results such that thing\_ids contains at least one  
> of a list of values, eg:
> 
> [1, 2, 3, 21115] # would return the above document  
> [1, 2, 3, 4] # would not
> 
> It doesn't really matter how many it contains, just that it matches at  
> least one of them.
> 
> I'm trying something like this but can't quite tell if this is correct:
> 
> {  
> "query":{"bool":{"must":[{"query\_string":{"query":"search  
> query","fields":["description","title"],"default\_operator":"AND","use\_dis\_max":true}}]}},  
> "filter":{  
> "or":[  
> {"term":{"thing\_ids":22003}},  
> {"term":{"thing\_ids":21757}},  
> {"term":{"thing\_ids":21610}}  
> ]  
> }  
> }
> 
> Any thoughts?
> 
> --  
> 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> 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: ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)
#### Post date: [March 12, 2013, 10:28pm UTC](https://discuss.elastic.co/t/array-intersect-filter/11100/4 "2013-03-12T22:28:03Z")

</div>

I am not even sure if your query is valid the way it is written. The terms  
filter example is wrapped by a constant score query. Try moving it to be  
directly after your query.

{  
"query": {  
"bool": {  
"must": [  
{  
"term": {  
"state": {  
"term": "live"  
}  
}  
},  
{  
"term": {  
"library\_id": {  
"term": 11  
}  
}  
}  
],  
"must\_not": [  
{  
"term": {  
"state": {  
"term": "deleted"  
}  
}  
}  
]  
}  
},  
"filter": {  
"terms": {  
"thingy\_ids": [  
21029  
]  
}  
},  
"sort": [  
{  
"taken\_at": "desc"  
}  
],  
"size": 25,  
"from": 0  
}

On Tue, Mar 12, 2013 at 3:16 AM, Rue [rue@thinlayer.co.uk](mailto:rue@thinlayer.co.uk) wrote:

> Thanks David.
> 
> From the docs it looked like that was exactly what I was looking for (it  
> may still be) but with it I am getting every document returned whatever I  
> put in it. Whatever values (including one that should return no results)  
> gives me all docs in the index.
> 
> It's bound to be something in my usage:
> 
> {  
> "query": { "bool": { "must": [ { "term": { "state": { "term": "live" } }  
> }, { "term": { "library\_id": { "term": 11 } } } ], "must\_not": [ { "term":  
> { "state": { "term": "deleted" } } } ] },  
> "constant\_score": {  
> "filter": {  
> "terms": {  
> "thingy\_ids": [  
> 21029  
> ]  
> }  
> }  
> }  
> },  
> "sort": [{ "taken\_at": "desc" }],  
> "size": 25,  
> "from": 0  
> }
> 
> On Monday, 11 March 2013 16:32:43 UTC, David Pilato wrote:
> 
> > I would probably use: [http://www.elasticsearch](http://www.elasticsearch).\*\*  
> > org/guide/reference/query-dsl/\*\*terms-filter.html[http://www.elasticsearch.org/guide/reference/query-dsl/terms-filter.html](http://www.elasticsearch.org/guide/reference/query-dsl/terms-filter.html)
> > 
> > {  
> > "constant\_score" : {  
> > "filter" : {  
> > "terms" : { "thing\_ids" : [22003, 21757, 21610]}  
> > }  
> > }  
> > }
> > 
> > Does it fit to your use case?
> > 
> > --  
> > _David Pilato_ | _Technical Advocate_ | _[Elasticsearch.com](http://Elasticsearch.com)_  
> > @dadoonet [https://twitter.com/dadoonet](https://twitter.com/dadoonet) | @elasticsearchfr[https://twitter.com/elasticsearchfr](https://twitter.com/elasticsearchfr)  
> > |\*\* @scrutmydocs [https://twitter.com/scrutmydocs](https://twitter.com/scrutmydocs)
> > 
> > Le 11 mars 2013 à 17:17, Rue [r...@thinlayer.co.uk](mailto:r...@thinlayer.co.uk) a écrit :
> > 
> > I'm trying to filter results based on whether an array field has at least  
> > one element from another set.
> > 
> > I have a field containing an array of integers eg:
> > 
> > \_source: {  
> > ...  
> > thing\_ids: [  
> > 21029,  
> > 21115,  
> > 20996,  
> > 21080,  
> > 20997  
> > ]  
> > }
> > 
> > I'd like to filter the results such that thing\_ids contains at least one  
> > of a list of values, eg:
> > 
> > [1, 2, 3, 21115] # would return the above document  
> > [1, 2, 3, 4] # would not
> > 
> > It doesn't really matter how many it contains, just that it matches at  
> > least one of them.
> > 
> > I'm trying something like this but can't quite tell if this is correct:
> > 
> > {  
> > "query":{"bool":{"must":[{"**query\_string":{"query":"search  
> > query","fields":["description"**,"title"],"default\_operator":"\*\*  
> > AND","use\_dis\_max":true}}]}},  
> > "filter":{  
> > "or":[  
> > {"term":{"thing\_ids":22003}},  
> > {"term":{"thing\_ids":21757}},  
> > {"term":{"thing\_ids":21610}}  
> > ]  
> > }  
> > }
> > 
> > Any thoughts?
> > 
> > --  
> > 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 elasticsearc...@\*\*[googlegroups.com](http://googlegroups.com).
> > 
> > For more options, visit [https://groups.google.com/\*\*groups/opt\_out](https://groups.google.com/**groups/opt_out)[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).

--  
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: ![Rue](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rue/32/2443_2.png) [@Rue](https://discuss.elastic.co/u/Rue)
#### Post date: [March 14, 2013, 8:29am UTC](https://discuss.elastic.co/t/array-intersect-filter/11100/5 "2013-03-14T08:29:34Z")

</div>

I think you have it there Ivan - thanks!

--  
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:46am UTC](https://discuss.elastic.co/t/array-intersect-filter/11100/6 "2017-07-06T02:46:35Z")

</div>


