# Documents filtering

**URL:** https://discuss.elastic.co/t/documents-filtering/58563
**Category:** Elasticsearch
**Created:** [August 22, 2016, 9:38am UTC](https://discuss.elastic.co/t/documents-filtering/58563 "2016-08-22T09:38:57Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Alkalit](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@Alkalit](https://discuss.elastic.co/u/Alkalit)
#### Post date: [August 22, 2016, 9:38am UTC](https://discuss.elastic.co/t/documents-filtering/58563/1 "2016-08-22T09:38:57Z")

</div>

I'm pretty new in elasticsearch and I need help to compose search query.  
I have index type under name "modelresult" wich contains a few indexed DB tables. The mapping [http://pastebin.com/G0AeXDFB](http://pastebin.com/G0AeXDFB)  
I need to filter all ads (docs with django\_ct eq to "[ads.ad](http://ads.ad)") with condition:

(category1: filter1 OR filter2 OR filter3) AND (category2: filter4 OR filter5 OR filter6) AND (categoryN: OR ) AND (etc)

Where is category noted with fields - filters\_ids, filters\_names, filters\_slugs  
and filter is - filters\_ids, filters\_names, filters\_slugs  
(Ad can contain a multiple filters and hence a multiple categories)

Currently I'm using this query , but it's not I wanted.  
Currently I use this query [http://pastebin.com/W6EHeJmx](http://pastebin.com/W6EHeJmx), but that's not what I need.

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [August 23, 2016, 8:29am UTC](https://discuss.elastic.co/t/documents-filtering/58563/2 "2016-08-23T08:29:42Z")

</div>

> [@Alkalit](#):
>
> (category1: filter1 OR filter2 OR filter3) AND (category2: filter4 OR filter5 OR filter6) AND (categoryN: OR ) AND (etc)

If we parse your top level expression which is `(..) AND (..) AND (..)` this can be represented as a `bool` query with a `must` array to hold the subclauses. Each of your subclauses is a list of alternative values for the same field so you can use the `terms` query to list them succinctly. This gives us the following:

```
GET test/_search
{
   "query": {
	  "bool": {
		 "must": [
			{
			   "terms": {
				  "category1": ["filter1", "filter2", "filter3"]
			   }
			},
			{
			   "terms": {
				  "category2": ["filter4", "filter5", "filter6"]
			   }
			}
		 ]
	  }
   }
} 

```

This will work but we can improve on it because there are sections of this query we may not want to contribute to scoring results - it will take longer to compute and if we turn it into a `filter` expression elasticsearch can agressively cache its results as yes/no bits it can reuse in similar queries. To do this we can wrap the `terms` clauses under a `bool>filter` arrangement. See here for more: [Combining Filters | Elasticsearch: The Definitive Guide [2.x] | Elastic](https://www.elastic.co/guide/en/elasticsearch/guide/current/combining-filters.html)

---

<div class="post-metadata">

### Author: ![Alkalit](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@Alkalit](https://discuss.elastic.co/u/Alkalit)
#### Post date: [August 25, 2016, 6:31am UTC](https://discuss.elastic.co/t/documents-filtering/58563/3 "2016-08-25T06:31:04Z")

</div>

Hi. Thanks for the answer. Unfortunately I made a critical error in the question. The bottom line is that the categories to which belongs ad indicates a set of fields: filters\_categories\_ids, filters\_categories\_names and filters\_categories\_slugs. Thus, this answer does not fit as the index does not store a collection of categoryN fields.

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [August 25, 2016, 7:22am UTC](https://discuss.elastic.co/t/documents-filtering/58563/4 "2016-08-25T07:22:15Z")

</div>

I suggest you supply an example doc as JSON and your intended query written as pseudocode

---

<div class="post-metadata">

### Author: ![Alkalit](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@Alkalit](https://discuss.elastic.co/u/Alkalit)
#### Post date: [August 25, 2016, 1:16pm UTC](https://discuss.elastic.co/t/documents-filtering/58563/5 "2016-08-25T13:16:17Z")

</div>

Thanks for taking time. Here is real document. Note - text data is mainly in russian.  
[http://pastebin.com/zRFHRW3u](http://pastebin.com/zRFHRW3u)

Here is how I think it should work:

```auto
var1 = [category1, category2]
var2 = [filter1, filter2]

for doc in docs
    if var1 is a subset of doc.filter_categories_ids
        if any of var2 in doc.filters_ids
            return True

```

P.S. sorry for my english.

---

<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 5, 2017, 10:25pm UTC](https://discuss.elastic.co/t/documents-filtering/58563/6 "2017-07-05T22:25:13Z")

</div>


