php - 子字符串上的elasticsearch聚合

标签 php json elasticsearch aggregation analysis

我在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/

相关文章:

php - 为什么这个 mysql 查询没有成功插入到我的数据库中?

php - 检查url是否存在

json - 如何配置ExtJS 4 Store(代理和阅读器)以读取元数据

php - 尝试用 SED 替换 PHP 中的行

PHP 多个复选框

javascript - 递归传递Json来获取特定key的所有值

json - 如何从文本中提取需要的信息?

Elasticsearch "starts with"短语中的第一个词

search - 使用“stop”时,在Elasticsearch中“_all”过滤器的行为有所不同

java - 在 Elasticsearch 插件中读取文件