# Dynamic code evaluation using Script

**URL:** https://discuss.elastic.co/t/dynamic-code-evaluation-using-script/179479
**Category:** Elasticsearch
**Created:** [May 3, 2019, 6:32am UTC](https://discuss.elastic.co/t/dynamic-code-evaluation-using-script/179479 "2019-05-03T06:32:22Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![sgoel](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@sgoel](https://discuss.elastic.co/u/sgoel)
#### Post date: [May 3, 2019, 6:32am UTC](https://discuss.elastic.co/t/dynamic-code-evaluation-using-script/179479/1 "2019-05-03T06:32:22Z")

</div>

I have a field in Elastic search of type 'keyword' with value = "Quantity \<= 10".  
"hits": [  
{  
"\_index": "rules1",  
"\_type": "\_doc",  
"\_id": "zvMleGoBkFxQI52IVKGV",  
"\_score": 1,  
"\_source": {  
"id": "1",  
"criteria1": "Quantity \<= 10"  
}  
},  
{  
"\_index": "rules1",  
"\_type": "\_doc",  
"\_id": "z\_MleGoBkFxQI52ItqFC",  
"\_score": 1,  
"\_source": {  
"id": "2",  
"criteria1": "Quantity \<= 20"  
}  
}  
]

I need to filter documents with passing Quantity as one of the parameters. I was using painless script -

```
{
"query": {
    "bool" : {
        "filter" : {
            "script" : {
                "script" : {
                    "source" : "doc['criteria1'].value",
                    "lang" : "painless",
                    "params" : {
                        "Quantity" : "5"
                    }
                }
            }
        }
    }
}

```

}

But it is giving an error -

```
{
"error": {
    "root_cause": [
        {
            "type": "script_exception",
            "reason": "runtime error",
            "script_stack": [
                "doc['criteria1'].value",
                " ^---- HERE"
            ],
            "script": "doc['criteria1'].value",
            "lang": "painless"
        }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
        {
            "shard": 0,
            "index": "rules1",
            "node": "bYDoLP0hSUuOcp9eEriIqQ",
            "reason": {
                "type": "script_exception",
                "reason": "runtime error",
                "script_stack": [
                    "doc['criteria1'].value",
                    " ^---- HERE"
                ],
                "script": "doc['criteria1'].value",
                "lang": "painless",
                "caused_by": {
                    "type": "class_cast_exception",
                    "reason": "cannot cast def [java.lang.String] to boolean"
                }
            }
        }
    ]
},
"status": 400

```

}

---

<div class="post-metadata">

### Author: ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)
#### Post date: [May 6, 2019, 5:07pm UTC](https://discuss.elastic.co/t/dynamic-code-evaluation-using-script/179479/2 "2019-05-06T17:07:53Z")

</div>

You need to convert that into a boolean expression, like `doc['criteria1'].value > params.Quantity`

---

<div class="post-metadata">

### Author: ![sgoel](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@sgoel](https://discuss.elastic.co/u/sgoel)
#### Post date: [May 7, 2019, 12:11pm UTC](https://discuss.elastic.co/t/dynamic-code-evaluation-using-script/179479/3 "2019-05-07T12:11:14Z")

</div>

Hi @polyfractal . Thank you for your reply. Actually, I already have an expression in the field 'criteria1'.  
Example: "criteria1":"Quantity \<= 10"

Now, I want to write a elastic search query in such a way that if I pass Quantity as a param in the query, it will replace that value in criteria1 field and evaluate it to a boolean. (Sort of rule evaluation to a boolean value with a dynamic value).

Example: If I pass Quantity = 5 to criteria1, the document will be retrieved, else if Quantity = 15, the document will not be retrieved.

---

<div class="post-metadata">

### Author: ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)
#### Post date: [May 7, 2019, 2:02pm UTC](https://discuss.elastic.co/t/dynamic-code-evaluation-using-script/179479/4 "2019-05-07T14:02:37Z")

</div>

Yep, you need to prefix the variable with `params`, e.g. `params.Quantity` 🙂

---

<div class="post-metadata">

### Author: ![sgoel](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@sgoel](https://discuss.elastic.co/u/sgoel)
#### Post date: [May 8, 2019, 4:30am UTC](https://discuss.elastic.co/t/dynamic-code-evaluation-using-script/179479/5 "2019-05-08T04:30:35Z")

</div>

I tried but it is not working as expected. Again trying to explain my problem statement -

@polyfractal these are 2 sample documents -  
{  
"\_index": "rules1",  
"\_type": "\_doc",  
"\_id": "0fOxkWoBkFxQI52IgKFf",  
"\_score": 1,  
"\_source": {  
"id": "4",  
"criteria1": "Quantity \<= 5"  
}  
},  
{  
"\_index": "rules1",  
"\_type": "\_doc",  
"\_id": "zvMleGoBkFxQI52IVKGV",  
"\_score": 1,  
"\_source": {  
"id": "1",  
"criteria1": "Quantity \<= 10"  
}  
}

Can you please help me with a query where I pass Quantity to the query.  
If Quantity = 3, both id = 1 and id = 4 are retrieved.  
If Quantity = 7, only id = 1 is retrieved.

---

<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: [June 5, 2019, 4:30am UTC](https://discuss.elastic.co/t/dynamic-code-evaluation-using-script/179479/6 "2019-06-05T04:30:36Z")

</div>

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