Script query within has_child query slow


#1

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


(system) #2