# Check value exists in params array using ElasticSearch painless script

**URL:** <https://discuss.elastic.co/t/check-value-exists-in-params-array-using-elasticsearch-painless-script/158254>\
**Category:** Elasticsearch\
**Created:** [November 26, 2018, 11:35pm UTC](https://discuss.elastic.co/t/check-value-exists-in-params-array-using-elasticsearch-painless-script/158254 "2018-11-26T23:35:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![CEJ](https://avatars.discourse-cdn.com/v4/letter/c/c68b51/32.png) [@CEJ](https://discuss.elastic.co/u/CEJ)\
**Post date:** [November 26, 2018, 11:35pm UTC](https://discuss.elastic.co/t/check-value-exists-in-params-array-using-elasticsearch-painless-script/158254/1 "2018-11-26T23:35:05Z")

</div>

I am new to ElasticSearch. I am working on a project where I have to search listings which matches the input `http://foo.bar/search?listing=123,456` . I have built an array which key and value are the same and representing the listing ids. I have tried to run the following example but it failed near contains. The script is built for painless language. However, I always get the runtime error.

I cannot get this to work, what could I be doing wrong?

```auto
 'function_score' => [
       'query' => $query,
           'score_mode' => 'sum',
              'functions' => [[
                'script_score' => [
                 'script' => [
                   'params' => ['listing' => [123 => 123, 456 => 456]],
    'source' => "(params.listing.contains(doc['id'].value) ? Math.pow(3, 3) : 0)",
                            ],
                        ],
                    ]],
                ],

```

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [November 27, 2018, 7:16am UTC](https://discuss.elastic.co/t/check-value-exists-in-params-array-using-elasticsearch-painless-script/158254/2 "2018-11-27T07:16:54Z")

</div>

If I understand the logic of your query, what you're trying to do is multiply the score by 27 if one of the IDs occurs in the `id` field. You can achieve that without a script, by using a [terms query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html) inside a filter function, instead of a script\_score function. I think this will be easier, as it will allow you to avoid having to do any scripting:

```auto
{
  "query": {
    "function_score": {
      "query": {
        YOUR_$QUERY
      },
      "functions": [
        {
          "filter": {
            "terms": {
              "id": [
                "123",
                "456"
              ]
            }
          },
          "weight": 27
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![CEJ](https://avatars.discourse-cdn.com/v4/letter/c/c68b51/32.png) [@CEJ](https://discuss.elastic.co/u/CEJ)\
**Post date:** [November 29, 2018, 11:01pm UTC](https://discuss.elastic.co/t/check-value-exists-in-params-array-using-elasticsearch-painless-script/158254/3 "2018-11-29T23:01:52Z")

</div>

The thing is that I have to stick with scripting, as there are other scripts running

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [November 30, 2018, 12:50pm UTC](https://discuss.elastic.co/t/check-value-exists-in-params-array-using-elasticsearch-painless-script/158254/4 "2018-11-30T12:50:57Z")

</div>

Alright. Why not simply pass in an array of values, rather than a map with key-value pairs? The following should work:

```auto
      "functions": [
        {
          "script_score": {
            "script": {
              "source": "(params.listing.contains(doc['id'].value) ? Math.pow(3, 3) : 0)",
              "params": {
                "listing": [
                  123,
                  456
                ]
              }
            }
          }
        }
      ]

```

---

<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:** [December 28, 2018, 12:51pm UTC](https://discuss.elastic.co/t/check-value-exists-in-params-array-using-elasticsearch-painless-script/158254/5 "2018-12-28T12:51:16Z")

</div>

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