# Unexpected behaviour with script query (script executed twice)

**URL:** <https://discuss.elastic.co/t/unexpected-behaviour-with-script-query-script-executed-twice/116420>\
**Category:** Elasticsearch\
**Created:** [January 22, 2018, 2:12am UTC](https://discuss.elastic.co/t/unexpected-behaviour-with-script-query-script-executed-twice/116420 "2018-01-22T02:12:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![luis1](https://avatars.discourse-cdn.com/v4/letter/l/91b2a8/32.png) [@luis1](https://discuss.elastic.co/u/luis1)\
**Post date:** [January 22, 2018, 2:12am UTC](https://discuss.elastic.co/t/unexpected-behaviour-with-script-query-script-executed-twice/116420/1 "2018-01-22T02:12:51Z")

</div>

Hi, all. I found some unexpected behaviour with script query (script is executing twice in a simple query). The steps to reproduce the issue:

My elastic search version: 2.4.6  
My elasticsearch.yml:

```
script.indexed: true

```

Steps:

1. I have one simple document, doc1.json:

```auto
{
	"id": "1",
	"tags": "t1"
}

```

1. Insert doc1 in Elastic:

```auto
http PUT localhost:9200/default/type1/1 @doc1.json

```

1. I have one simple groovy script, script1.json (just returns the score and print it):

```auto
{
     "script": "println('Score is ' + _score * 1.0 + ' for document ' + doc['id'] + ' at ' + DateTime.now().getMillis()); return _score;"
}

```

1. Register script1:

```auto
http POST 'localhost:9200/_scripts/groovy/script1' @script1.json

```

1. Execute this query\_with\_script.json:

```auto
{
   "query":{
      "function_score":{
         "query":{
            "bool":{
               "must":{
                  "match":{
                     "tags":{
                        "query":"t1",
                        "type":"boolean"
                     }
                  }
               }
            }
         },
         "functions":[
            {
               "script_score":{
                  "script":{
                     "id":"script1",
                     "lang":"groovy"
                  }
               }
            }
         ],
         "boost_mode":"replace"
      }
   },
   "explain" : true
}

```

```auto
http GET 'localhost:9200/default/type1/_search' @query_with_script.json

```

1. Why in Elastic search logs I see that the script is executed in two different times?

```auto
Score is 0.19178301095962524 for document [1] at 1516586818596
Score is 0.19178301095962524 for document [1] at 1516586818606

```

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 22, 2018, 4:13am UTC](https://discuss.elastic.co/t/unexpected-behaviour-with-script-query-script-executed-twice/116420/2 "2018-01-22T04:13:00Z")

</div>

May be you have 2 shards?

---

<div class="post-metadata">

**Author:** ![luis1](https://avatars.discourse-cdn.com/v4/letter/l/91b2a8/32.png) [@luis1](https://discuss.elastic.co/u/luis1)\
**Post date:** [January 22, 2018, 4:43am UTC](https://discuss.elastic.co/t/unexpected-behaviour-with-script-query-script-executed-twice/116420/3 "2018-01-22T04:43:02Z")

</div>

Hi! I have 5 yards, but only one document. Moreover, I created an index with only 1 yard, but the issue still remains. Issue can be easily reproduced in a couple of minutes following the steps. Thanks!

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 22, 2018, 9:56am UTC](https://discuss.elastic.co/t/unexpected-behaviour-with-script-query-script-executed-twice/116420/4 "2018-01-22T09:56:20Z")

</div>

As far as I remember we fixed once such an issue but I can't recall where it has been fixed.

Any chance you could reproduce this on 5.6 or better on 6.1 with a painless script instead?

---

<div class="post-metadata">

**Author:** ![luis1](https://avatars.discourse-cdn.com/v4/letter/l/91b2a8/32.png) [@luis1](https://discuss.elastic.co/u/luis1)\
**Post date:** [January 22, 2018, 8:08pm UTC](https://discuss.elastic.co/t/unexpected-behaviour-with-script-query-script-executed-twice/116420/5 "2018-01-22T20:08:56Z")

</div>

Unfortunately, we are at production and we can't migrate to 5.x because of lack of compatibility between Elastic 5.x and the well known artifact spring-boot-starter-data-elasticsearch 😕

---

<div class="post-metadata">

**Author:** ![luis1](https://avatars.discourse-cdn.com/v4/letter/l/91b2a8/32.png) [@luis1](https://discuss.elastic.co/u/luis1)\
**Post date:** [January 26, 2018, 2:52am UTC](https://discuss.elastic.co/t/unexpected-behaviour-with-script-query-script-executed-twice/116420/6 "2018-01-26T02:52:22Z")

</div>

Same behaviour in Elastic 5.6.0, why script is being executed twice? Is it a bug?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 26, 2018, 12:43pm UTC](https://discuss.elastic.co/t/unexpected-behaviour-with-script-query-script-executed-twice/116420/7 "2018-01-26T12:43:29Z")

</div>

What happen if you remove `"explain" : true`?

---

<div class="post-metadata">

**Author:** ![luis1](https://avatars.discourse-cdn.com/v4/letter/l/91b2a8/32.png) [@luis1](https://discuss.elastic.co/u/luis1)\
**Post date:** [January 26, 2018, 5:01pm UTC](https://discuss.elastic.co/t/unexpected-behaviour-with-script-query-script-executed-twice/116420/8 "2018-01-26T17:01:54Z")

</div>

It was that. Thanks!

---

<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:** [February 23, 2018, 5:02pm UTC](https://discuss.elastic.co/t/unexpected-behaviour-with-script-query-script-executed-twice/116420/9 "2018-02-23T17:02:41Z")

</div>

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