# How to execute a stored script in a search query

**URL:** <https://discuss.elastic.co/t/how-to-execute-a-stored-script-in-a-search-query/265514>\
**Category:** Elasticsearch\
**Created:** [February 25, 2021, 4:36pm UTC](https://discuss.elastic.co/t/how-to-execute-a-stored-script-in-a-search-query/265514 "2021-02-25T16:36:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rschirin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rschirin/32/45283_2.png) [@rschirin](https://discuss.elastic.co/u/rschirin)\
**Post date:** [February 25, 2021, 4:36pm UTC](https://discuss.elastic.co/t/how-to-execute-a-stored-script-in-a-search-query/265514/1 "2021-02-25T16:36:49Z")

</div>

hey there,  
I created a stored script that should add a new field to all the docs into an index.

```
POST _scripts/set_new_field
{
  "script": {
    "lang": "painless",
    "source": """
     if (ctx._source.speaker == "TEDX") { 
       ctx._source.conference = true;
     } else 
     {
       ctx._source.conference = false;
     } 
"""
  }

```

}

I am not able right to execute it on a specific index named `speech`  
What should be the request to be executed?  
i.e.

```
GET speech/_search
{
   "query": {
      "match_all": {}
   }
   "script": {
   ... 
   }
}

```

I checked the official documentation but I cannot find the correct address

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [February 27, 2021, 12:33am UTC](https://discuss.elastic.co/t/how-to-execute-a-stored-script-in-a-search-query/265514/2 "2021-02-27T00:33:37Z")

</div>

[Here](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-using.html#_request_examples) is a full example of how to save and then use a script within a search.

```auto
GET my-index-000001/_search
{
  "query": {
    "script_score": {
      "query": {
        "match": {
            "message": "some message"
        }
      },
      "script": {
        "id": "calculate-score",
        "params": {
          "my_modifier": 2
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![rschirin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rschirin/32/45283_2.png) [@rschirin](https://discuss.elastic.co/u/rschirin)\
**Post date:** [February 27, 2021, 9:32am UTC](https://discuss.elastic.co/t/how-to-execute-a-stored-script-in-a-search-query/265514/3 "2021-02-27T09:32:48Z")

</div>

I already saw that script and documentation but I cannot get out with it.  
if I try:

```
GET hamlet/_search
{
  "query": {
    "script_score": {
      "query": {
        "match": {
            "city": "Paris"
        }
      },
      "script": {
        "id": "set_new_field"
      }
    }
  }
}

```

I get this error:

```
"caused_by" : {
        "type" : "illegal_argument_exception",
        "reason" : "cannot resolve symbol [ctx._source.speaker]"
      }

```

furthermore, I don't understand why I should use `script_score`

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [February 27, 2021, 12:48pm UTC](https://discuss.elastic.co/t/how-to-execute-a-stored-script-in-a-search-query/265514/4 "2021-02-27T12:48:41Z")

</div>

Is your end goal to add a new field **permanently** to your index based on the script?

If so I would just use the [Update by Query API](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html).

```auto
POST speech/_update_by_query
{
  "script": {
    "lang": "painless",
    "source": """
     if (ctx._source.speaker == "TEDX") { 
       ctx._source.conference = true;
     } else 
     {
       ctx._source.conference = false;
     } 
    """
  },
  "query": {
    "match_all": {}
  }
}

```

**Result**

```auto
    "hits" : [
      {
        "_source" : {
          "conference" : false,
          "speaker" : "Elastic"
        }
      },
      {
        "_source" : {
          "conference" : true,
          "speaker" : "TEDX"
        }
      }
    ]

```

---

<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:** [March 27, 2021, 12:49pm UTC](https://discuss.elastic.co/t/how-to-execute-a-stored-script-in-a-search-query/265514/5 "2021-03-27T12:49:16Z")

</div>

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