# Filter first then search

**URL:** <https://discuss.elastic.co/t/filter-first-then-search/16636>\
**Category:** Elasticsearch\
**Created:** [March 27, 2014, 9:50am UTC](https://discuss.elastic.co/t/filter-first-then-search/16636 "2014-03-27T09:50:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Max\_Ivanov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/max_ivanov/32/1684_2.png) [@Max\_Ivanov](https://discuss.elastic.co/u/Max_Ivanov)\
**Post date:** [March 27, 2014, 9:50am UTC](https://discuss.elastic.co/t/filter-first-then-search/16636/1 "2014-03-27T09:50:24Z")

</div>

Gist below in short: I have 1000 documents, only one of them matches filter  
and all of them match text query.  
When I do filtered query the document is only found if I set large  
max\_expansions. The real data is tens of thousands documents, so it doesn't  
look like a way.  
Is there another way to filter documents first and then to perform search  
on what was filtered?

ES 1.0.1. The queries to reproduce:

curl -XPUT '[http://localhost:9200/test\_max\_expansions/](http://localhost:9200/test_max_expansions/)'

curl -XPUT '[http://localhost:9200/test\_max\_expansions/posts/\_mapping](http://localhost:9200/test_max_expansions/posts/_mapping)' -d '  
{  
"posts" : {  
"properties" : {  
"title" : {"type" : "string"},  
"hidden" : {"type" : "boolean"}  
}  
}  
}'

for i in {1..1000}; do curl -XPUT "[http://localhost:9200/test\_max\_expansions/posts/$i](http://localhost:9200/test_max_expansions/posts/%24i)" -d "{"title" : "a$i", "hidden" : false}"; done

curl -XPUT '[http://localhost:9200/test\_max\_expansions/posts/1001](http://localhost:9200/test_max_expansions/posts/1001)' -d '  
{  
"title" : "a1001",  
"hidden" : true  
}'

curl -XGET '[http://localhost:9200/test\_max\_expansions/posts/\_search?pretty](http://localhost:9200/test_max_expansions/posts/_search?pretty)' -d '{  
"query": {  
"filtered": {  
"filter": { "term": { "hidden": true } },  
"query": {  
"match\_phrase\_prefix": {  
"title": {  
"query": "a"  
}  
}  
}  
}  
}  
}'

--  
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/a518e17c-a349-4295-a963-597bbfb53939%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a518e17c-a349-4295-a963-597bbfb53939%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly\_2](https://avatars.discourse-cdn.com/v4/letter/b/d07c76/32.png) [@Binh\_Ly\_2](https://discuss.elastic.co/u/Binh_Ly_2)\
**Post date:** [March 27, 2014, 1:20pm UTC](https://discuss.elastic.co/t/filter-first-then-search/16636/2 "2014-03-27T13:20:59Z")

</div>

Not sure if your example data is representative of production data, but if  
you have single not\_analyzed term values in the field title, you can  
probably use the prefix query instead.

--  
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/be8be85d-5437-4c2c-9c60-5bd9ea01dc98%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/be8be85d-5437-4c2c-9c60-5bd9ea01dc98%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Max\_Ivanov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/max_ivanov/32/1684_2.png) [@Max\_Ivanov](https://discuss.elastic.co/u/Max_Ivanov)\
**Post date:** [April 1, 2014, 9:56am UTC](https://discuss.elastic.co/t/filter-first-then-search/16636/3 "2014-04-01T09:56:53Z")

</div>

Production data is ~ 10000 documents, 230 of them match filter.  
I need my text queries to be analyzed, and prefix query doesn't support  
that, so it won't help.

On Thursday, March 27, 2014 1:20:59 PM UTC, Binh Ly wrote:

> Not sure if your example data is representative of production data, but if  
> you have single not\_analyzed term values in the field title, you can  
> probably use the prefix query instead.

--  
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/e74f146f-10db-4759-8abc-08b89b389e63%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e74f146f-10db-4759-8abc-08b89b389e63%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![David\_Smith\_2](https://avatars.discourse-cdn.com/v4/letter/d/a9a28c/32.png) [@David\_Smith\_2](https://discuss.elastic.co/u/David_Smith_2)\
**Post date:** [April 18, 2014, 8:05pm UTC](https://discuss.elastic.co/t/filter-first-then-search/16636/4 "2014-04-18T20:05:35Z")

</div>

I'm also curious to know if there is way to do the opposite of  
FilteredQuery... basically QueriedFilter. Filter first and then run a query  
on the filtered results.

--  
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/818d6e9f-b4d8-4427-b9c1-1723ac0dd5d7%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/818d6e9f-b4d8-4427-b9c1-1723ac0dd5d7%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:34am UTC](https://discuss.elastic.co/t/filter-first-then-search/16636/5 "2017-07-06T01:34:57Z")

</div>


