# Painless query very slowly

**URL:** https://discuss.elastic.co/t/painless-query-very-slowly/127295
**Category:** Elasticsearch
**Created:** [April 9, 2018, 9:48am UTC](https://discuss.elastic.co/t/painless-query-very-slowly/127295 "2018-04-09T09:48:40Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![fapwlh](https://avatars.discourse-cdn.com/v4/letter/f/58f4c7/32.png) [@fapwlh](https://discuss.elastic.co/u/fapwlh)
#### Post date: [April 9, 2018, 9:48am UTC](https://discuss.elastic.co/t/painless-query-very-slowly/127295/1 "2018-04-09T09:48:40Z")

</div>

## I'm a beginner, when I use painless script to search ,very slowly ,but use date\_histogram very quickly.

`POST /productinfo/\_search  
{  
"aggs": {  
"groupbyyear":{  
"terms": {  
"script": {  
"lang": "painless",  
"source":"doc['mdate'].value.year"  
}  
}  
}

}  
}`

## the took is 2494

`{ "took": 2494, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 23139644, "max_score": 1,`

## when I use datehist like this, the took only 438,can somebody tell me why,please!

`POST /productinfo/\_search  
{  
"aggs": {  
"groupbyyear":{  
"date\_histogram": {  
"field": "mdate",  
"interval": "year"  
}  
}

}  
}`

## the result:

`{ "took": 438, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 23139644, "max_score": 1, "hits": [`

## like below also slowly........

`POST /productinfo/_search { "aggs": { "groupbydep":{ "terms": { "script": { "lang": "painless", "source":"doc['pdep'].value.substring(0,4)" }`

---

<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: [April 9, 2018, 10:01am UTC](https://discuss.elastic.co/t/painless-query-very-slowly/127295/2 "2018-04-09T10:01:09Z")

</div>

Because executing a script on every single document (here `23139644` documents) takes a lot of time.

Why not just using the `date_histogram` aggregation?

---

<div class="post-metadata">

### Author: ![fapwlh](https://avatars.discourse-cdn.com/v4/letter/f/58f4c7/32.png) [@fapwlh](https://discuss.elastic.co/u/fapwlh)
#### Post date: [April 9, 2018, 10:07am UTC](https://discuss.elastic.co/t/painless-query-very-slowly/127295/3 "2018-04-09T10:07:49Z")

</div>

**In our case, the date group only one, most like below, need substring some string fields to aggs,so ,have other idea to excute below search quickly, worry ,please 😂**  
"groupbydep":{ "terms": { "script": { "lang": "painless", "source":"doc['pdep'].value.substring(0,4)"

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [April 9, 2018, 10:11am UTC](https://discuss.elastic.co/t/painless-query-very-slowly/127295/4 "2018-04-09T10:11:32Z")

</div>

I would recommend parsing it out at index time and storing it in a separate field if it is something you want to aggregate efficiently on at scale. That way you do the parsing once per document rather than once per document per query.

---

<div class="post-metadata">

### Author: ![fapwlh](https://avatars.discourse-cdn.com/v4/letter/f/58f4c7/32.png) [@fapwlh](https://discuss.elastic.co/u/fapwlh)
#### Post date: [April 9, 2018, 10:17am UTC](https://discuss.elastic.co/t/painless-query-very-slowly/127295/5 "2018-04-09T10:17:07Z")

</div>

ok, thanks,I'll try reindex my document.

---

<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: [May 7, 2018, 10:17am UTC](https://discuss.elastic.co/t/painless-query-very-slowly/127295/6 "2018-05-07T10:17:17Z")

</div>

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