Use Elasticsearch java api, hits score always be 1.0


(lisiyu) #1

Hi,
I used ElasticSearch java client. I do search with query_string, and I get response, but score always be 1.0 .

code is:

String query = "{\"query\": {\"query_string\": {\"query\": \"weblog data4\"}}}";
    SearchRequestBuilder builder = client
            .prepareSearch("flume-2016-08-10")
            .setQuery(query)
            .addHighlightedField("*")
            .setHighlighterRequireFieldMatch(false)
            .setFrom(0).setSize(60).setExplain(true);
    SearchResponse response  = builder.execute().actionGet();

    System.out.println(response.toString());
    System.out.println(response.getHits().getAt(0).getSource());
    System.out.println(response.getHits().getAt(0).getHighlightFields());
    client.close();

result is:

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [ {
      "_shard" : 1,
      "_node" : "by8Gb6CfRG-mz7eUYeqBOA",
      "_index" : "flume-2016-08-10",
      "_type" : "logs",
      "_id" : "AVZy7VMhAbkZavCS5dph",
      "_score" : 1.0,
      "_source" : {
        "body" : "website:weblog:docs_page weblog data4"
      },
      "highlight" : {
        "body" : [ "website:weblog:docs_page <em>weblog</em> <em>data4</em>" ]
      },
      "_explanation" : {
        "value" : 1.0,
        "description" : "ConstantScore(_all:weblog _all:data4), product of:",
        "details" : [ {
          "value" : 1.0,
          "description" : "boost",
          "details" : [ ]
        }, {
          "value" : 1.0,
          "description" : "queryNorm",
          "details" : [ ]
        } ]
      }
    }, {
      "_shard" : 1,
      "_node" : "by8Gb6CfRG-mz7eUYeqBOA",
      "_index" : "flume-2016-08-10",
      "_type" : "logs",
      "_id" : "AVZy7VMiAbkZavCS5dpl",
      "_score" : 1.0,
      "_source" : {
        "body" : "syslog:syslog:sysloggroup syslog data4"
      },
      "highlight" : {
        "body" : [ "syslog:syslog:sysloggroup syslog <em>data4</em>" ]
      },
      "_explanation" : {
        "value" : 1.0,
        "description" : "ConstantScore(_all:weblog _all:data4), product of:",
        "details" : [ {
          "value" : 1.0,
          "description" : "boost",
          "details" : [ ]
        }, {
          "value" : 1.0,
          "description" : "queryNorm",
          "details" : [ ]
        } ]
      }
    } ]
  }
}
{body=website:weblog:docs_page weblog data4}
{body=[body], fragments[[website:weblog:docs_page <em>weblog</em> <em>data4</em>]]}

However, I do search in elaseticsearch-head, I get response with correct score.

{"query":{"query_string":{"query":"weblog data4"}},"highlight":{"require_field_match":false,"explain":true,"fields":{"*":{}}}}

{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.5506518,
"hits": [
{
"_index": "flume-2016-08-10",
"_type": "logs",
"_id": "AVZy7VMhAbkZavCS5dph",
"_score": 1.5506518,
"_source": {
"body": "website:weblog:docs_page weblog data4"
},
"highlight": {
"body": [
"website:weblog:docs_page <em>weblog</em> <em>data4</em>"
]
}
}
,
{
"_index": "flume-2016-08-10",
"_type": "logs",
"_id": "AVZy7VMiAbkZavCS5dpl",
"_score": 0.31629312,
"_source": {
"body": "syslog:syslog:sysloggroup syslog data4"
},
"highlight": {
"body": [
"syslog:syslog:sysloggroup syslog <em>data4</em>"
]
}
}
]
}
}

So, How do I get correct score with java?


(Adrien Grand) #2

This should be

String query = "{\"query_string\": {\"query\": \"weblog data4\"}}";

Otherwise you are wrapping under the deprecated query query has the side effect of making scores equal to 1.


(lisiyu) #3

This solved my problem:

String query = "{"query_string": {"query": "weblog data4"}}";

Thanks a lot, jpountz


(system) #4