# Script query within has\_child query slow

**URL:** <https://discuss.elastic.co/t/script-query-within-has-child-query-slow/62567>\
**Category:** Elasticsearch\
**Created:** [October 8, 2016, 9:07pm UTC](https://discuss.elastic.co/t/script-query-within-has-child-query-slow/62567 "2016-10-08T21:07:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![turp1twin](https://avatars.discourse-cdn.com/v4/letter/t/ed655f/32.png) [@turp1twin](https://discuss.elastic.co/u/turp1twin)\
**Post date:** [October 8, 2016, 9:07pm UTC](https://discuss.elastic.co/t/script-query-within-has-child-query-slow/62567/1 "2016-10-08T21:07:08Z")

</div>

Just want to reach out to the list and see if this behavior is expected... I have a script query embedded within a has\_child query and it runs pretty slow (~2secs). This slow performance is consistent between multiple query executions. It appears there is no query caching or something similar going on. If I perform the equivelant query directly on the child type, it is somewhat slow on the first run, then very fast on subsequent runs (~2ms), which I would assume is due to caching. Is this to be expected? If so, are there any recommendations for me to speed this up? I have to run a script like this as I need to be able to compare values within a document, like in this example Dates.... Any help would be appreciated!

Slow Query:

```
GET my_index/patients/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "has_child": {
            "type": "claimDiagnoses",
            "query": {
              "script": {
                "script": "doc['serviceEndDate'] ? ((((doc['serviceEndDate'].value - doc['memberDateOfBirth'].value)/(24*60*60*1000)) > 41) && (((doc['serviceEndDate'].value - doc['memberDateOfBirth'].date.toDateTime().plusYears(2).getMillis())/(24*60*60*1000)) <= 0)) : false"
                
              }
            }
          }
        }
      ]
    }
  }
}

```

Fast Query:

```
GET my_index/claimDiagnoses/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "query": {
              "script": {
                "script": "doc['serviceEndDate'] ? ((((doc['serviceEndDate'].value - doc['memberDateOfBirth'].value)/(24*60*60*1000)) > 41) && (((doc['serviceEndDate'].value - doc['memberDateOfBirth'].date.toDateTime().plusYears(2).getMillis())/(24*60*60*1000)) <= 0)) : false"
                
            }
          }
        }
      ]
    }
  }
}

```

Thanks,

Jeff

---

<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:** [July 5, 2017, 10:13pm UTC](https://discuss.elastic.co/t/script-query-within-has-child-query-slow/62567/2 "2017-07-05T22:13:48Z")

</div>

Closed on July 5, 2017, 10:13pm UTC


