# Filter any documents containing any of several values in a number of fields

**URL:** <https://discuss.elastic.co/t/filter-any-documents-containing-any-of-several-values-in-a-number-of-fields/17235>\
**Category:** Elasticsearch\
**Created:** [April 28, 2014, 2:24pm UTC](https://discuss.elastic.co/t/filter-any-documents-containing-any-of-several-values-in-a-number-of-fields/17235 "2014-04-28T14:24:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Snare](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mike_snare/32/1569_2.png) [@Mike\_Snare](https://discuss.elastic.co/u/Mike_Snare)\
**Post date:** [April 28, 2014, 2:24pm UTC](https://discuss.elastic.co/t/filter-any-documents-containing-any-of-several-values-in-a-number-of-fields/17235/1 "2014-04-28T14:24:55Z")

</div>

Is there any way to construct a filter that will filter out any documents  
that match any values in an array from, say, 2 different fields?

Imagine a document that stores friendships/recommendations/etc, where the  
document might store ids for each user as user\_1 and user\_2, and then other  
information in whatever other fields you'd need. The important thing is  
the user\_1 and user\_2 fields, but here's an idea of what the doc might look  
like:

{  
user\_1: 12345,  
user\_2: 23456,  
facebook\_friends: true,  
twitter\_friends: false,  
comments: "blahblahblahh"  
}

I would like to execute a query that returns a set of documents but  
excludes any friendships for a list of user ids. These excluded user\_ids  
might be listed as user\_1 in some docs, but might be listed as user\_2 in  
others. I can easily construct a terms query for each field that will say  
"exclude where user\_1 is any of these values AND THEN exclude where user\_2  
is any of these values" but what I'd like to do is say "exclude where  
either user\_1 or user\_2 are any of these values".

Right now I have to do this:

{  
query: {  
filtered: {  
query: {  
// whatever...  
},  
filter: {  
bool: {  
must\_not: [  
{  
terms: {  
user\_1: [12345, 23456]  
},  
{  
terms: {  
user\_2: [12345, 23456]  
}  
]  
}  
}  
}  
}  
}

Some of these filters end up getting pretty large, and I'd like to be able  
to avoid duplicating the array within the same query if I can. Something  
like this would be awesome, but I can't figure out if there's any way to do  
it:

{  
query: {  
filtered: {  
query: {  
// whatever...  
},  
filter: {  
not: {  
multi\_match: {  
terms: ["user\_1", "user\_2"],  
values: [12345, 23456]  
}  
}  
}  
}  
}  
}

Thanks,  
-Mike

--  
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/4fcef91a-3f44-4452-acb3-dc704c8c2005%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4fcef91a-3f44-4452-acb3-dc704c8c2005%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Mike\_Snare](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mike_snare/32/1569_2.png) [@Mike\_Snare](https://discuss.elastic.co/u/Mike_Snare)\
**Post date:** [April 29, 2014, 2:06pm UTC](https://discuss.elastic.co/t/filter-any-documents-containing-any-of-several-values-in-a-number-of-fields/17235/2 "2014-04-29T14:06:14Z")

</div>

Bump

--  
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/fad5147a-4a5d-444c-8ac5-9f404e098e9a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/fad5147a-4a5d-444c-8ac5-9f404e098e9a%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:32am UTC](https://discuss.elastic.co/t/filter-any-documents-containing-any-of-several-values-in-a-number-of-fields/17235/3 "2017-07-06T01:32:43Z")

</div>


