FilterBuilders v0.9 to v6.0 valid code replace


(SatyaRaj) #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....


(David Pilato) #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)));

(SatyaRaj) #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++;
    }
}

(David Pilato) #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.


(SatyaRaj) #5

Clear, One last doubt, if how does BoolQueryBuilder know it is filter or query.


(David Pilato) #6

If you use filter or must not it's a filter.
If you use must or should it's a query.


(SatyaRaj) #7

Thank you sir.
That post concludes my query.


(system) #8

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