Sorting with has_child function_score - illegal argument exception, variable is not found

Elasticsearch 5.5.1 (out-of-the box/stock)
Example nearly identical to the Elasticsearch 5.5 docs here.
The only difference is the child doc index type name and the property name.

Copy and paste the following curl commands to reproduce the issue.

Searching turned up a similar issue here, however not seeing what I need to fix.
The documentation is either incomplete, incorrect, or there is a bug.

I've tried:

  • doc['ranking.likes']
  • doc['first.likes']
  • adding parent type to query /ss/asset
  • removing single quotes around likes "doc[likes] or doc[ranking.likes]" - no affect on error message which is surprising.

Could it be an issue with curl and quotes? Shows query beyond single quotes.
Maybe the example is groovy centric instead of painless? Matches painless examples.
Maybe the example query is only a partial solution and there is something missing.

Create Index

curl -s -H "Content-Type: application/json" -XPUT localhost:9200/ss -d '
{
  "mappings": {
    "asset": {
      "properties": {
        "name": { "type": "text" },
        "type": { "type": "text" }
      }
    },
    "ranking": {
      "_parent": {
        "type": "asset"
      },
      "properties": {
        "likes": { "type": "integer" }
      }
    }
  }
}'

Index

curl -XGET localhost:9200/ss?pretty
{
  "ss" : {
    "aliases" : { },
    "mappings" : {
      "asset" : {
        "properties" : {
          "name" : {
            "type" : "text"
          },
          "type" : {
            "type" : "text"
          }
        }
      },
      "ranking" : {
        "_parent" : {
          "type" : "asset"
        },
        "_routing" : {
          "required" : true
        },
        "properties" : {
          "likes" : {
            "type" : "integer"
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1501410680501",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "5kWl-phRSoS3G14iy3rLDw",
        "version" : {
          "created" : "5050199"
        },
        "provided_name" : "ss"
      }
    }
  }
}

Index Parent docs

curl -s -H "Content-Type: application/x-ndjson" -XPOST localhost:9200/ss/asset/_bulk -d'
{ "index": { "_id": "a1" }}
{ "name": "foo", "type": "video" }
{ "index": { "_id": "a2" }}
{ "name": "bar", "type": "video" }
'

Index Child docs

curl -s -H "Content-Type: application/x-ndjson" -XPOST localhost:9200/ss/ranking/_bulk -d '
{ "index": { "_id": "r1", "parent": "a1" }}
{ "likes": 100 }
{ "index": { "_id": "r2", "parent": "a2" }}
{ "likes": 50 }
'

Indexed Docs

curl http://localhost:9200/ss/_search?pretty=true
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 4,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "ss",
        "_type" : "asset",
        "_id" : "a2",
        "_score" : 1.0,
        "_source" : {
          "name" : "bar",
          "type" : "video"
        }
      },
      {
        "_index" : "ss",
        "_type" : "ranking",
        "_id" : "r2",
        "_score" : 1.0,
        "_routing" : "a2",
        "_parent" : "a2",
        "_source" : {
          "likes" : 50
        }
      },
      {
        "_index" : "ss",
        "_type" : "asset",
        "_id" : "a1",
        "_score" : 1.0,
        "_source" : {
          "name" : "foo",
          "type" : "video"
        }
      },
      {
        "_index" : "ss",
        "_type" : "ranking",
        "_id" : "r1",
        "_score" : 1.0,
        "_routing" : "a1",
        "_parent" : "a1",
        "_source" : {
          "likes" : 100
        }
      }
    ]
  }
}

Query
from elasticsearch docs here
Executed in Ubuntu 14.04 bash terminal

curl 'http://localhost:9200/ss/_search?pretty=true' -d '
{
    "query": {
        "has_child" : {
            "type" : "ranking",
            "score_mode" : "max",
            "query" : {
                "function_score" : {
                    "script_score": {
                        "script": "_score * doc['likes'].value"
                    }
                }
            }
        }
    }
}'

Error

{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "compile error",
        "script_stack" : [
          "_score * doc[likes].value",
          "             ^---- HERE"
        ],
        "script" : "_score * doc[likes].value",
        "lang" : "painless"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "ss",
        "node" : "FiSprCgmQ7eki2ZLagy5Vw",
        "reason" : {
          "type" : "query_shard_exception",
          "reason" : "script_score: the script could not be loaded",
          "index_uuid" : "5kWl-phRSoS3G14iy3rLDw",
          "index" : "ss",
          "caused_by" : {
            "type" : "script_exception",
            "reason" : "compile error",
            "script_stack" : [
              "_score * doc[likes].value",
              "             ^---- HERE"
            ],
            "script" : "_score * doc[likes].value",
            "lang" : "painless",
            "caused_by" : {
              "type" : "illegal_argument_exception",
              "reason" : "Variable [likes] is not defined."
            }
          }
        }
      }
    ]
  },
  "status" : 400
}

Firstly create a script

curl -XPOST 'http://localhost:9200/_scripts/score-from-doc-value' -d '
{
  "script": {
    "lang": "painless",
    "code": "_score * doc[\u0027likes\u0027].value"
  }
}'

then use it

curl 'http://localhost:9200/ss/_search?pretty=true' -d '
{
  "query": {
    "has_child" : {
      "type" : "ranking",
      "score_mode" : "max",
      "query" : {
        "function_score" : {
          "script_score": {
            "script": {
              "stored": "score-from-doc-value"
            }
          }
        }
      }
    }
  }
}'

So the real issue was the quotes which I suggested above, but discarded due to the error message not referencing or altering when changes made to the quotes and including data after the quotes that looked fine.

No need to create the script first. The scripts are cached so the only reason one would want to materialize it independently would be for reuse in other queries.

So the precise answer is just change quotes to unicode.

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