# Search inner\_hits using a script

**URL:** <https://discuss.elastic.co/t/search-inner-hits-using-a-script/164321>\
**Category:** Elasticsearch\
**Created:** [January 15, 2019, 2:16pm UTC](https://discuss.elastic.co/t/search-inner-hits-using-a-script/164321 "2019-01-15T14:16:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![randel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/randel/32/2541_2.png) [@randel](https://discuss.elastic.co/u/randel)\
**Post date:** [January 15, 2019, 2:16pm UTC](https://discuss.elastic.co/t/search-inner-hits-using-a-script/164321/1 "2019-01-15T14:16:33Z")

</div>

According to the [documentation of inner\_hits](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html#_options) it should be possible to use a script to sort the nested inner\_hits of a document.

But I can't get it to work - the \_score turns NULL as soon as I use it in an script.

An example:

```
PUT /books
{
  "mappings": {
    "_doc": {
      "properties": {
        "editions": {
          "type": "nested"
        }
      }
    }
  }
}

PUT books/_doc/1?refresh
{
  "bookratings": 25,
  "editions": [
    {
      "title": "harry potter I",
      "ratings": 10
    },
    {
      "title": "harry potter 1",
      "ratings": 15
    }
  ]
}

PUT books/_doc/2?refresh
{
  "bookratings": 19,
  "editions": [
    {
      "title": "harry potter II",
      "ratings": 7
    },
    {
      "title": "harry potter 2",
      "ratings": 12
    }
  ]
}

POST /books/_search
{
  "_source": [
    "bookratings"
  ],
  "query": {
    "function_score": {
      "query": {
        "nested": {
          "path": "editions",
          "score_mode": "max",
          "query": {
            "bool": {
              "must": [
                {
                  "match": {
                    "editions.title": {
                      "query": "harry",
                      "operator": "and"
                    }
                  }
                }
              ]
            }
          },
          "inner_hits": {
            "sort": {                  
              "_script": {
                "type": "number",
                "script": "return _score * doc['editions.ratings'].value;"
              }
            },
            "docvalue_fields": [
              "editions.ratings.keyword"
            ]
          }
        }
      },
      "script_score": {
        "script": "return _score * doc['bookratings'].value;"
      }
    }
  }
}

```

So I get

```
"_score" : null,
"sort" : [
	0.0
]

```

Instead when I remove the script from inside "inner\_hits" a score is shown.

`"_score" : 0.18232156,`

Is that a bug? Am I the bug?

---

<div class="post-metadata">

**Author:** ![randel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/randel/32/2541_2.png) [@randel](https://discuss.elastic.co/u/randel)\
**Post date:** [January 15, 2019, 4:15pm UTC](https://discuss.elastic.co/t/search-inner-hits-using-a-script/164321/2 "2019-01-15T16:15:34Z")

</div>

While that might be a bug indeed, it's not necessary. The same sorting can be achieved by

```
POST /books/_search
{
  "_source": [
    "bookratings"
  ],
  "query": {
    "function_score": {
      "query": {
        "nested": {
          "path": "editions",
          "score_mode": "max",
          "query": {
            "function_score": {
              "query": {
                "bool": {
                  "must": [
                    {
                      "match": {
                        "editions.title": {
                          "query": "harry",
                          "operator": "and"
                        }
                      }
                    }
                  ]
                }
              },
              "script_score": {
                "script": {
                  "source": "return _score * doc['editions.ratings'].value;"
                }
              }
            }
          },
          "inner_hits": {
            "size": 1,
            "_source": true,
            "docvalue_fields": [
              "editions.ratings.keyword"
            ]
          }
        }
      },
      "script_score": {
        "script": "return _score * doc['bookratings'].value;"
      }
    }
  }
}
```

---

<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:** [February 12, 2019, 4:15pm UTC](https://discuss.elastic.co/t/search-inner-hits-using-a-script/164321/3 "2019-02-12T16:15:35Z")

</div>

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