# Elasticsearch v8 Java API client search with filter (not postFilter)

**URL:** https://discuss.elastic.co/t/elasticsearch-v8-java-api-client-search-with-filter-not-postfilter/315823
**Category:** Elasticsearch
**Tags:** language-clients
**Created:** [October 4, 2022, 10:06pm UTC](https://discuss.elastic.co/t/elasticsearch-v8-java-api-client-search-with-filter-not-postfilter/315823 "2022-10-04T22:06:24Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ijaxahmed](https://avatars.discourse-cdn.com/v4/letter/i/f17d59/32.png) [@ijaxahmed](https://discuss.elastic.co/u/ijaxahmed)
#### Post date: [October 4, 2022, 10:06pm UTC](https://discuss.elastic.co/t/elasticsearch-v8-java-api-client-search-with-filter-not-postfilter/315823/1 "2022-10-04T22:06:24Z")

</div>

I wanted to replace postFilter with filter in the search, but not sure how to because Query has "query.postFilter(postFilters) " method but nothing as ".filter(filters) " .

Current implementations is like , Creating Query , creating postFilters and add them to SearchRequest e.g

```auto
Query query = MatchPhraseQuery.of(matchPhraseQuery->matchPhraseQuery.field(fieldName).query(value.trim()))._toQuery()

var postFilters = new BoolQuery.Builder();
 postFilters.must(MatchQuery.of(matchQuery->matchQuery.field("name").query(value.toString()))._toQuery());
   postFilters.mustNot(BoolQuery.of(boolQuery->boolQuery.should(MatchQuery.of(matchQuery->matchQuery.field("status").query( Status.ACTIVE.name()))._toQuery()))._toQuery());

SearchRequest srb1 = SearchRequest.of(r -> r.query(query)
                .index(indexName)
                .postFilter(postFilters);
            

```

Now i want to replace postFilters with Filter in the query. But not sure how to do it ?

As I tried this but not sure is it right approach?  
-\> created BoolQuery with "must" and "filter", "must" contains actual query and "filter" contains filer e.g.

```auto
var filteredQuery = new BoolQuery.Builder().must(query).filter(postFilters).build()._toQuery();
SearchRequest srb2 = SearchRequest.of(r -> r.query(filteredQuery)
                .index(indexName);

```

Note postFilter is removed form srb2.  
Will srb1 and srb2 returns the same result? is it correct away to add filter to the query?

Please guide how to add filter

---

<div class="post-metadata">

### Author: ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)
#### Post date: [October 4, 2022, 11:09pm UTC](https://discuss.elastic.co/t/elasticsearch-v8-java-api-client-search-with-filter-not-postfilter/315823/2 "2022-10-04T23:09:30Z")

</div>

Hi @ijaxahmed

About post filter:

> The Post\_Filter is applied to the search hits at the very end of a search request, after aggregations have already been calculated.

If you use post\_filter it was probably for some reason, and this is not clear in the post. If you want to use filters you will get one of the benefits, like caching.

I suggest you test the two approaches, which are different, to understand the results.  
Try to detail, in the post, the reason for using the post\_filter and if you intend to change to filter, which criteria you intend to have with it.

---

<div class="post-metadata">

### Author: ![ijaxahmed](https://avatars.discourse-cdn.com/v4/letter/i/f17d59/32.png) [@ijaxahmed](https://discuss.elastic.co/u/ijaxahmed)
#### Post date: [October 5, 2022, 10:02am UTC](https://discuss.elastic.co/t/elasticsearch-v8-java-api-client-search-with-filter-not-postfilter/315823/3 "2022-10-05T10:02:25Z")

</div>

@RabBit_BR thanks for the reply

I want to use Filter over the post\_filter due to performance becase i dont have any aggregation, its mentioned here about the performance.

> **[Post Filter | Elasticsearch: The Definitive Guide \[2.x\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/guide/current/_post_filter.html)**

> Performance consideration  
> Use a post\_filter only if you need to differentially filter search results and aggregations. Sometimes people will use post\_filter for regular searches.  
> Don’t do this! The nature of the post\_filter means it runs after the query, so any performance benefit of filtering (such as caches) is lost completely.  
> The post\_filter should be used only in combination with aggregations, and only when you need differential filtering.

I am more concerned about the right way to add filter using Java API Client. is it correct way as i mention in the first comment above ? in otherway how to add filter (considering the above code, as there is query, there is filter and want to make query using that )

---

<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: [November 2, 2022, 10:03am UTC](https://discuss.elastic.co/t/elasticsearch-v8-java-api-client-search-with-filter-not-postfilter/315823/4 "2022-11-02T10:03:03Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
