# Accessing \_score of inner hits for further sorting

**URL:** <https://discuss.elastic.co/t/accessing-score-of-inner-hits-for-further-sorting/136687>\
**Category:** Elasticsearch\
**Created:** [June 20, 2018, 12:28pm UTC](https://discuss.elastic.co/t/accessing-score-of-inner-hits-for-further-sorting/136687 "2018-06-20T12:28:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![randel\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/randel_2/32/31785_2.png) [@randel\_2](https://discuss.elastic.co/u/randel_2)\
**Post date:** [June 20, 2018, 12:28pm UTC](https://discuss.elastic.co/t/accessing-score-of-inner-hits-for-further-sorting/136687/1 "2018-06-20T12:28:52Z")

</div>

While the [documentation of `inner hits` shows](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html#_options) 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](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html) 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
                  ]
                }
              ]
            }
          }
        }
      }
    ]
  }
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 18, 2018, 12:28pm UTC](https://discuss.elastic.co/t/accessing-score-of-inner-hits-for-further-sorting/136687/2 "2018-07-18T12:28:55Z")

</div>

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