# Sorting with has\_child function\_score - illegal argument exception, variable is not found

**URL:** https://discuss.elastic.co/t/sorting-with-has-child-function-score-illegal-argument-exception-variable-is-not-found/95072
**Category:** Elasticsearch
**Created:** [July 30, 2017, 10:44am UTC](https://discuss.elastic.co/t/sorting-with-has-child-function-score-illegal-argument-exception-variable-is-not-found/95072 "2017-07-30T10:44:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![tcollinsworth](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@tcollinsworth](https://discuss.elastic.co/u/tcollinsworth)
#### Post date: [July 30, 2017, 10:44am UTC](https://discuss.elastic.co/t/sorting-with-has-child-function-score-illegal-argument-exception-variable-is-not-found/95072/1 "2017-07-30T10:44:07Z")

</div>

Elasticsearch 5.5.1 (out-of-the box/stock)  
Example nearly identical to the Elasticsearch 5.5 docs [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-child-query.html#_sorting).  
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](https://stackoverflow.com/questions/39866706/haschild-and-function-score-sorting), 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](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-child-query.html#_sorting)  
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
}
```

---

<div class="post-metadata">

### Author: ![tanguyantoine](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tanguyantoine/32/20592_2.png) [@tanguyantoine](https://discuss.elastic.co/u/tanguyantoine)
#### Post date: [July 31, 2017, 12:29am UTC](https://discuss.elastic.co/t/sorting-with-has-child-function-score-illegal-argument-exception-variable-is-not-found/95072/2 "2017-07-31T00:29:36Z")

</div>

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"
            }
          }
        }
      }
    }
  }
}'
```

---

<div class="post-metadata">

### Author: ![tcollinsworth](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@tcollinsworth](https://discuss.elastic.co/u/tcollinsworth)
#### Post date: [July 31, 2017, 1:23pm UTC](https://discuss.elastic.co/t/sorting-with-has-child-function-score-illegal-argument-exception-variable-is-not-found/95072/3 "2017-07-31T13:23:01Z")

</div>

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.

---

<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: [August 28, 2017, 1:32pm UTC](https://discuss.elastic.co/t/sorting-with-has-child-function-score-illegal-argument-exception-variable-is-not-found/95072/4 "2017-08-28T13:32:17Z")

</div>

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