2 filters with query not working in elasticSearch, java -
this old code working fine
qb = querybuilders.querystring(query.replaceall(" ", " or ").replaceall(",", " , ").replaceall("!", " not ")); filterbuilder fb = filterbuilders.andfilter(filterbuilders.rangefilter("experiance").from(smonth).to(emonth)); filteredquerybuilder fqbuilder = querybuilders.filteredquery(qb, fb); org.elasticsearch.action.search.searchresponse searchhits = node.client() .preparesearch(name) .setquery(fqbuilder)
in piece of code, searching data experience between smonth , emonth.
now need add more filters in search, move 'nativesearchquerybuilder'. after modification, write code:-
qb = querybuilders.querystring(query.replaceall(" ", " or ").replaceall(",", " , ").replaceall("!", " not ")); filterbuilder fb = filterbuilders.andfilter(filterbuilders.rangefilter("experiance").from(smonth).to(emonth)); nativesearchquerybuilder builder = new nativesearchquerybuilder(); builder.withquery(qb); andfilterbuilder filters = null; filters = new andfilterbuilder(fb); filters.add(filterbuilders.andfilter(filterbuilders.boolfilter().must(filterbuilders.termfilter("providedzipcode", zipcode)))); //third filter builder.withfilter(filters); org.elasticsearch.action.search.searchresponse searchhits = node.client() .preparesearch(name) .setquery(builder.build().getquery());
but when searching filter not working. not getting data according experience , zipcode.
got it.
we can use setpostfilter
https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/search.html
org.elasticsearch.action.search.searchresponse searchhits = node.client() .preparesearch(name) // .setindices(name) .setsearchtype(searchtype.dfs_query_then_fetch) .setquery(fqbuilder) .setpostfilter(filterbuilders.andfilter(filterbuilders.boolfilter().must(filterbuilders.termfilter("providedzipcode", zipcode)))); // filter
but have problem. if didnt pass zipcode it's not showing data. want data if not pass zipcode.
Comments
Post a Comment