# I am confused by the postFilter and filter of elasticsearch‘s java client

**URL:** https://discuss.elastic.co/t/i-am-confused-by-the-postfilter-and-filter-of-elasticsearch-s-java-client/16067
**Category:** Elasticsearch
**Created:** [February 28, 2014, 9:04am UTC](https://discuss.elastic.co/t/i-am-confused-by-the-postfilter-and-filter-of-elasticsearch-s-java-client/16067 "2014-02-28T09:04:35Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![xzer\_LR](https://avatars.discourse-cdn.com/v4/letter/x/2acd7d/32.png) [@xzer\_LR](https://discuss.elastic.co/u/xzer_LR)
#### Post date: [February 28, 2014, 9:04am UTC](https://discuss.elastic.co/t/i-am-confused-by-the-postfilter-and-filter-of-elasticsearch-s-java-client/16067/1 "2014-02-28T09:04:35Z")

</div>

AFAIK, There are three types search I can perform on elasticsearch:

1. query =\> which will do scoring and affect the hit
2. filter =\> which will not do scoring and affect the hit
3. post\_filter =\> which will be applied on the result of query/filter and  
will not affect the hit and facet aggregation

At the java client library side, I am assuming there should be same three  
types of apis existing, yes I found three.  
But unfortunately the setFilter method of SearchRequestBuilder had been  
marked as deprecated and also had  
been delegated to the setPostFilter method of itself.

My question is how can I perform a filter query by java client? I found a  
way by that I can do filter query as following:

search.setQuery(  
QueryBuilders.filteredQuery(  
QueryBuilders.matchAllQuery(),  
FilterBuilders.termFilter("xx", "v")  
)  
);

But it seems so strange that I have to declare an unnecessary match all  
query.

--  
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/6aefc449-3997-4128-b9ee-715d7a998b5c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6aefc449-3997-4128-b9ee-715d7a998b5c%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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: [February 28, 2014, 1:23pm UTC](https://discuss.elastic.co/t/i-am-confused-by-the-postfilter-and-filter-of-elasticsearch-s-java-client/16067/2 "2014-02-28T13:23:19Z")

</div>

That looks correct to me.

--  
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/4ad78d4d-3e0f-4dfe-a4b9-1d59c349e56e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4ad78d4d-3e0f-4dfe-a4b9-1d59c349e56e%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![javanna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javanna/32/4698_2.png) [@javanna](https://discuss.elastic.co/u/javanna)
#### Post date: [March 4, 2014, 5:44pm UTC](https://discuss.elastic.co/t/i-am-confused-by-the-postfilter-and-filter-of-elasticsearch-s-java-client/16067/3 "2014-03-04T17:44:09Z")

</div>

If you only need the filter part you can use a constant score query that  
only contains a filter, in that case you wouldn't need the match\_all query  
anymore.

On Friday, February 28, 2014 10:04:35 AM UTC+1, xzer LR wrote:

> AFAIK, There are three types search I can perform on elasticsearch:
> 
> 1. query =\> which will do scoring and affect the hit
> 2. filter =\> which will not do scoring and affect the hit
> 3. post\_filter =\> which will be applied on the result of query/filter and  
> will not affect the hit and facet aggregation
> 
> At the java client library side, I am assuming there should be same three  
> types of apis existing, yes I found three.  
> But unfortunately the setFilter method of SearchRequestBuilder had been  
> marked as deprecated and also had  
> been delegated to the setPostFilter method of itself.
> 
> My question is how can I perform a filter query by java client? I found a  
> way by that I can do filter query as following:
> 
> search.setQuery(  
> QueryBuilders.filteredQuery(  
> QueryBuilders.matchAllQuery(),  
> FilterBuilders.termFilter("xx", "v")  
> )  
> );
> 
> But it seems so strange that I have to declare an unnecessary match all  
> query.

--  
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/0ec29ae2-fc52-4ebe-b83e-a94b3ea775d2%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0ec29ae2-fc52-4ebe-b83e-a94b3ea775d2%40googlegroups.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, 1:45am UTC](https://discuss.elastic.co/t/i-am-confused-by-the-postfilter-and-filter-of-elasticsearch-s-java-client/16067/4 "2017-07-06T01:45:48Z")

</div>


