Function Score Query Java API Not working with Multiple Filters


(raghuveer jagirdar) #1

I am using elasticsearch 1.3.1 version. I am facing a unique problem with
FunctionScore Query Java API. I am placing a bool filter inside the
function score query in an effort to combine multiple queries. But the
problem is, with only a single filter as part of the bool filter it is
working fine. The moment I add another filter to the bool filter, the
client is getting stuck and I am getting a timeout exception. However the
same with curl is working. Please help me in this regard. My query is

{
"function_score" : {
"filter" : {
"bool" : {
"should" : [ {
"term" : {
"showId" : "85ef1da5-db78-3fd2-86a4-8c9ff3f053d0"
}
}, {
"term" : {
"showId" : "e275549d-143e-37e8-8689-44a805b957da"
}
} ]
}
},
"functions" : [ {
"filter" : {
"term" : {
"showId" : "85ef1da5-db78-3fd2-86a4-8c9ff3f053d0"
}
},
"boost_factor" : 40.0
}, {
"filter" : {
"term" : {
"showId" : "e275549d-143e-37e8-8689-44a805b957da"
}
},
"boost_factor" : 35.0
} ]
}
}

And the Java Code is as follows.

public static ArrayList getShowIds(){
ArrayList showIds = new ArrayList();
showIds.add("85ef1da5-db78-3fd2-86a4-8c9ff3f053d0");
showIds.add("e275549d-143e-37e8-8689-44a805b957da");
return showIds;
}

public static void functionScoreQueryAudio(InstanceType type){
TransportClient client = null;
int show_base=40; int show_step=5;
try{
if(type==InstanceType.EC2)
client = getClient(InstanceType.EC2);
else
client = getClient(InstanceType.LOCAL);
System.out.println("Client Created");

    BoolFilterBuilder boolFilterBuilder = FilterBuilders.boolFilter();
    FunctionScoreQueryBuilder queryBuilder = QueryBuilders.functionScoreQuery(boolFilterBuilder);
    int i=0;
    for(String str : getShowIds()){
        boolFilterBuilder.should(FilterBuilders.termFilter("showId", str));
        queryBuilder.add(FilterBuilders.termFilter("showId", str),ScoreFunctionBuilders.factorFunction(show_base-(i*show_step)));
        i++;
    }

    SearchResponse response = client.prepareSearch("audio").
            setQuery(queryBuilder).setFrom(0).setSize(200).execute().actionGet(TimeValue.timeValueSeconds(50));

    if(response.getHits().totalHits()>0){

        for(SearchHit hit : response.getHits()){
            System.out.println(hit.getScore());
            System.out.println(hit.getSourceAsString());
            System.out.println("===========================================================================================================================");
        }
        System.out.println(response.getHits().totalHits()+" "+response.getTookInMillis());
    }
    else
        System.out.println("No Records Found");



}
catch(Exception ex){
    System.out.println(ex.getMessage());
}

}

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/450e7932-65db-4a6c-9a46-f5a760dc9f72%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(system) #2