Search code examples
elasticsearchelasticsearch-jest

Elastic search Query String java api gives same score to all the results


I am using the below search query:

{ "from": "0", "size": "20", "query": {"query_string" : {"fields" : ["title"],"query" : "seller*", "analyze_wildcard": true}}}

This curl hit returns me hits/results with proper score, whereas I am trying same thru the elasticsearch transport client like:

String queryString = "{" 
           + "\"query_string\" : {\n" + 
           "       \"fields\" : [\"title\"],\n" + 
           "       \"query\" : "+ "\"" + searchQuery  + "\",\n" + 
           "       \"analyze_wildcard\": true \n" + 
           "   }}\n";

SearchResponse response2 = client.prepareSearch(indexName).setTypes(successZoneTypeName).setFrom(0).setSize(30).setQuery(QueryBuilders.queryStringQuery(queryString)).get();

This returns the result with same score against all the hits.

Even I tried with jest client the result is same I am not getting proper scores in the result.


Solution

  • This is not the proper way of constructing a query string query via Java. You should do it this way:

    QueryBuilder qs = QueryBuilders.queryStringQuery("\"" + searchQuery  + "\"")
       .field("title")
       .analyzeWildcard(true);
    
    SearchResponse response2 = client.prepareSearch(indexName)
       .setTypes(successZoneTypeName)
       .setFrom(0)
       .setSize(30)
       .setQuery(qs)
       .get();