# Using Scripted Conditionals

**URL:** <https://discuss.elastic.co/t/using-scripted-conditionals/197861>\
**Category:** Elasticsearch\
**Created:** [September 3, 2019, 12:48pm UTC](https://discuss.elastic.co/t/using-scripted-conditionals/197861 "2019-09-03T12:48:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![spupapi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spupapi/32/49899_2.png) [@spupapi](https://discuss.elastic.co/u/spupapi)\
**Post date:** [September 3, 2019, 12:48pm UTC](https://discuss.elastic.co/t/using-scripted-conditionals/197861/1 "2019-09-03T12:48:06Z")

</div>

Hello,

so I've got an index called Hamlet. And in Hamlet there is a field "Speaker" with value of the name of the speaker. So it looks like this:

```
{
"speaker": "Hamlet"
}

```

Now I want to make script which will:  
Create a script named `set_is_hamlet` and save it into the cluster state. The script (i) adds a field named `is_hamlet` to each document, (ii) sets the field to "true" if the document has `speaker` equals to "HAMLET", (iii) sets the field to "false" otherwise.

How would the script look like?

---

<div class="post-metadata">

**Author:** ![Bernt\_Rostad](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Bernt\_Rostad](https://discuss.elastic.co/u/Bernt_Rostad)\
**Post date:** [September 3, 2019, 3:12pm UTC](https://discuss.elastic.co/t/using-scripted-conditionals/197861/2 "2019-09-03T15:12:32Z")

</div>

> [@spupapi](#):
>
> Create a script named `set_is_hamlet`

Not sure what you mean by naming a script, a [pipeline](https://www.elastic.co/guide/en/elasticsearch/reference/current/ingest.html) can be named but an inline script used in an [update\_by\_query](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html) has no name. For the sake of simplicity I'll write the script as for use by the latter. But first a comment:

> [@spupapi](#):
>
> I've got an index called Hamlet.

Index names in Elasticsearch must be lower case (at least up until version 6.8).

So I opened Kibana in my test cluster and created an index named **hamlet** by adding four small documents of the format you mentioned. E.g:

```
PUT /hamlet/_doc/1
{
    "speaker": "Ophelia"
}
... etc

```

The result looks like this:

```
GET /hamlet/_search
...
  "hits" : {
    "total" : 4,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "hamlet",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "speaker" : "Hamlet"
        }
      },
      {
        "_index" : "hamlet",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "speaker" : "HAMLET"
        }
      },
      {
        "_index" : "hamlet",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "speaker" : "Ophelia"
        }
      },
      {
        "_index" : "hamlet",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "speaker" : "Laertes"
        }
      }
    ]
  }

```

The final step is to write a small painless script and use it in an **\_update\_by\_query** , like this:

```
POST /hamlet/_update_by_query
{
   "script": {
      "lang": "painless",
      "source": """
         if (ctx._source.speaker == "HAMLET") {
            ctx._source.set_is_hamlet = true
         } else {
            ctx._source.set_is_hamlet = false
         }
               """
    }
 }

```

This correctly found the documents with speaker "HAMLET" and set its _set\_is\_hamlet_ to _true_ while the rest were set _false_ (notice that Elasticsearch is case sensitive, so only speaker HAMLET gets _set\_is\_hamlet: true_):

```
GET hamlet/_search
...
  "hits" : {
    "total" : 4,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "hamlet",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "set_is_hamlet" : false,
          "speaker" : "Hamlet"
        }
      },
      {
        "_index" : "hamlet",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "set_is_hamlet" : true,
          "speaker" : "HAMLET"
        }
      },
      {
        "_index" : "hamlet",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "set_is_hamlet" : false,
          "speaker" : "Ophelia"
        }
      },
      {
        "_index" : "hamlet",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "set_is_hamlet" : false,
          "speaker" : "Laertes"
        }
      }
    ]
  }

```

I hope this answered your question.

---

<div class="post-metadata">

**Author:** ![spupapi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spupapi/32/49899_2.png) [@spupapi](https://discuss.elastic.co/u/spupapi)\
**Post date:** [September 4, 2019, 7:35am UTC](https://discuss.elastic.co/t/using-scripted-conditionals/197861/3 "2019-09-04T07:35:14Z")

</div>

> [@Bernt\_Rostad](#):
>
> Not sure what you mean by naming a script, a [pipeline](https://www.elastic.co/guide/en/elasticsearch/reference/current/ingest.html) can be named but an inline script used in an [update\_by\_query](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html) has no name.

What I meant was to create a templated script and save it into cluster state, using your code below.

```
POST _scripts/script_hamlet_example
{
   "script": {
      "lang": "painless",
      "source": """
         if (ctx._source.speaker == "HAMLET") {
            ctx._source.set_is_hamlet = true
         } else {
            ctx._source.set_is_hamlet = false
         }
               """
    }
 }

```

And yes! Thank you for helping, you made it seem so simple 🙂.

---

<div class="post-metadata">

**Author:** ![Bernt\_Rostad](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Bernt\_Rostad](https://discuss.elastic.co/u/Bernt_Rostad)\
**Post date:** [September 4, 2019, 7:49am UTC](https://discuss.elastic.co/t/using-scripted-conditionals/197861/4 "2019-09-04T07:49:51Z")

</div>

> [@spupapi](#):
>
> What I meant was to create a templated script and save it into cluster state,

Oh, I see. I haven't stored scripts to the cluster before, only used them in pipelines or \_update\_by\_query statements.

> [@spupapi](#):
>
> Thank you for helping

I'm glad you found your solution 🙂

---

<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:** [October 2, 2019, 7:49am UTC](https://discuss.elastic.co/t/using-scripted-conditionals/197861/5 "2019-10-02T07:49:57Z")

</div>

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