Accessing _score of inner hits for further sorting


(randel-2) #1

While the documentation of inner hits shows that sort can be used to overwrite the default sorting (by _score) of my inner hits I can't seem to access `_score' itself.

The documentation of Sort suggests that I can access _sort and _doc. And while _doc is indeed accessible, I didn't manage to access _score. What did I do wrong?

POST /books_v2/_search
{
  "size": 1,
  "_source": [
    "bookratings"
  ],
  "query": {
    "function_score": {
      "query": {
        "nested": {
          "path": "editions",
          "score_mode": "max",
          "query": {
            "bool": {
              "must": [
                {
                  "match": {
                    "editions.title_author_unique": {
                      "query": "harry",
                      "operator": "and",
                      "fuzziness": "AUTO"
                    }
                  }
                }
              ]
            }
          },
          "inner_hits": {
            "size": 1,
            "_source": false,
            "sort": {
              "_script": {
                "type": "number",
                "script": {
                  "lang": "painless",
                  "source": """return _score;"""
                },
                "order": "desc"
              }
            }
          }
        }
      },
      "script_score": {
        "script": """return _score;"""
      }
    }
  }
}

results in

{
  "took": 40,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 37,
    "max_score": 15.269544,
    "hits": [
      {
        "_index": "books_v2",
        "_type": "_doc",
        "_id": "1845",
        "_score": 15.269544,       //_score is used for sorting
        "_source": {
          "bookratings": 37
        },
        "inner_hits": {
          "editions": {
            "hits": {
              "total": 1,
              "max_score": null,
              "hits": [
                {
                  "_index": "books_v2",
                  "_type": "_doc",
                  "_id": "1845",
                  "_nested": {
                    "field": "editions",
                    "offset": 16
                  },
                  "_score": null,         // original score is not used, instead _score is null
                  "sort": [
                    0
                  ]
                }
              ]
            }
          }
        }
      }
    ]
  }
}

(system) #2

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