Painless query very slowly

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)" }

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

Why not just using the date_histogram aggregation?

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 :joy:
"groupbydep":{ "terms": { "script": { "lang": "painless", "source":"doc['pdep'].value.substring(0,4)"

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.

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