我在elasticsearch 5中有一个索引为字符串的字段
例如20090219、20100416等
我可以对这些数据进行聚合,但我想聚合子字符串。
这是
2009,2010
我不想转换为日期。我想获取前 4 个字符 并获取计数。 这是我当前的代码。对 Elasticsearch 非常陌生
$params['body']["aggs"]["Year"]["terms"]["field"] = "PublicationDate.keyword";
$params['body']["aggs"]["Year"]["terms"]["size"] = 10;
$params['body']["aggs"]["Year"]["terms"]["order"]["_count"] = "desc";
最佳答案
您可以使用elasticsearch script
功能来实现这一点。
GET my-index/_search
{
"aggs" : {
"my-agg" : {
"terms" : {
"script": {
"inline": "doc['PublicationDate.keyword'].getValue().substring(0,4)"
},
"size": 10,
"order" : { "_count" : "desc" }
}
}
}
}
我不知道上述命令的等效 php
脚本,但相信您能够使其在 php
中工作。
关于php - 子字符串上的elasticsearch聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43340175/