satyarajp
(SatyaRaj)
December 20, 2017, 6:26am
1
Java api for Filterbuilders in elasticsearch 2
Hi Team,
I'm going through the above link. Need some help with below code.
BoolQueryBuilder query = QueryBuilders.boolQuery();
BoolFilterBuilder filter = FilterBuilders.boolFilter().filterName("compositeFilter");
query.filter(QueryBuilders.termsQuery("SearchString", toLowerCase(filterList))).must();
SearchRequestBuilder searchRequestBuilder = client
.prepareSearch(CONTENT_INDEX)
.setTypes(CONTENT_TYPE)
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
searchRequestBuilder.setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), filter));
searchRequestBuilder.setQuery(QueryBuilders.filteredQuery(query, filter));
Can the below code be most valid replacement....
BoolQueryBuilder query = QueryBuilders.boolQuery();
SearchRequestBuilder searchRequestBuilder = client
.prepareSearch(INDEX)
.setTypes(TYPE)
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
query.must(QueryBuilders.termsQuery("keyword", toLowerCase(filterList))).filter();
searchRequestBuilder.setQuery(query.must(QueryBuilders.matchAllQuery()));
searchRequestBuilder.setQuery(query.must());
The last line in replacement code doesn't compile. What should be good replacement there.
Thanks in advance....
dadoonet
(David Pilato)
December 20, 2017, 7:39am
2
You need to remove:
searchRequestBuilder.setQuery(query.must(QueryBuilders.matchAllQuery()));
searchRequestBuilder.setQuery(query.must());
And I'd replace:
query.must(QueryBuilders.termsQuery("keyword", toLowerCase(filterList))).filter();
by
query.filter(QueryBuilders.termsQuery("keyword", toLowerCase(filterList)));
satyarajp
(SatyaRaj)
December 20, 2017, 12:37pm
3
Hi David,
seems like I missed the method call part. Here is actual code flow. That I understand is reason you considered can be done in one line. This infact is if..else condition and here is complete snippet.
BoolQueryBuilder query = QueryBuilders.boolQuery();
BoolFilterBuilder filter = FilterBuilders.boolFilter().filterName("compositeFilter");
addTermFilter(true, query, "cEnabled");
addFilter(typeFilters, query, "type");
if (filterCount == 0)
searchRequestBuilder.setQuery(query);
else if (filterCount > 0)
//searchRequestBuilder.setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), filter)); v0.9
searchRequestBuilder.setQuery(query.filter(QueryBuilders.matchAllQuery()));//v6.0
private void addFilter(List<String> filterList, BoolQueryBuilder query, String field) {
if (filterList != null && filterList.size() > 0) {
//query.filter(QueryBuilders.termsQuery("keyword", toLowerCase(filterList))).must();//v0.9
query.must(QueryBuilders.termsQuery("keyword", toLowerCase(filterList))).filter();//v6.0
filterCount++;
}
}
private void addTermFilter(Object value, BoolQueryBuilder query, String field) {
if (value != null) {
//filterBuilder.must(FilterBuilders.termFilter(field, value)); //v0.9
query.must(QueryBuilders.termQuery(field, value)).filter();//v6.0
filterCount++;
}
}
dadoonet
(David Pilato)
December 20, 2017, 12:58pm
4
Sorry but this is super weird to read.
Basically this is wrong:
query.must(QueryBuilders.termsQuery("keyword", toLowerCase(filterList))).filter();
And should be replaced by
query.filter(QueryBuilders.termsQuery("keyword", toLowerCase(filterList)));
This
query.must(QueryBuilders.termQuery(field, value)).filter();//v6.0
By
query.filter(QueryBuilders.termQuery(field, value));//v6.0
This
searchRequestBuilder.setQuery(query.filter(QueryBuilders.matchAllQuery()));
Has no sense and can be removed all together.
In short you can write something like
BoolQueryBuilder query = QueryBuilders.boolQuery();
query.filter(QueryBuilders.termsQuery("keyword", toLowerCase(typeFilters)));
query.filter(QueryBuilders.termQuery("cEnabled", true));
Something along those lines.
1 Like
satyarajp
(SatyaRaj)
December 22, 2017, 7:18am
5
Clear, One last doubt, if how does BoolQueryBuilder know it is filter or query.
dadoonet
(David Pilato)
December 22, 2017, 10:21am
6
If you use filter or must not it's a filter.
If you use must or should it's a query.
1 Like
satyarajp
(SatyaRaj)
December 22, 2017, 11:53am
7
Thank you sir.
That post concludes my query.
system
(system)
Closed
January 19, 2018, 11:53am
8
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.