Unable to get _score value when using field collapse and sort


(Yifeng Chen) #1

When I search with sort other than _score, the results dose not show _score. However if I add "track_scores" : "true", it does.

If for this same query, field collapse is used, "track_scores" : "true" doesn't help.

I found a similar issue in ES bug 27122/ (or 23840), and it was fixed in 6.1.0. But this issue only address the max_score, not the case above.

Any idea?

thanks!

Yifeng


(Luiz Santos) #2

Hi @amyc,

You can add _score to your sort fields and track its value:

POST likes/doc/_bulk
{"index":{"_id": 1}}
{"likes": 10, "user": "john", "message": "elastic"}
{"index":{"_id": 2}}
{"likes": 20, "user": "john", "message": "elastic"}
{"index":{"_id": 3}}
{"likes": 5, "user": "ryan", "message": "elastic"}
{"index":{"_id": 4}}
{"likes": 10, "user": "ryan", "message": "elastic"}

GET likes/_search
{
  "track_scores": "true",
  "query": {
    "match": {
      "message": "elastic"
    }
  },
  "collapse": {
    "field": "user.keyword"
  },
  "sort": [
    "likes",
    "_score"
  ]
} 

Response:

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "likes",
        "_type": "doc",
        "_id": "3",
        "_score": 0.2876821,
        "_source": {
          "likes": 5,
          "user": "ryan",
          "message": "elastic"
        },
        "fields": {
          "user.keyword": [
            "ryan"
          ]
        },
        "sort": [
          5,
          0.2876821
        ]
      },
      {
        "_index": "likes",
        "_type": "doc",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "likes": 10,
          "user": "john",
          "message": "elastic"
        },
        "fields": {
          "user.keyword": [
            "john"
          ]
        },
        "sort": [
          10,
          0.2876821
        ]
      }
    ]
  }
}

Hope it helps.

Cheers,
LG


(Yifeng Chen) #3

@luiz.santos, this works for me, thank you very much!


(system) #4

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