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
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.
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?
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.
I want to use Filter over the post_filter due to performance becase i dont have any aggregation, its mentioned here about the performance.
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 )
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.