Search inner_hits using a script

According to the documentation of inner_hits 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?

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;"
      }
    }
  }
}

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