【问题标题】:elasticsearch aggregations on substring子字符串上的弹性搜索聚合
【发布时间】:2017-09-06 12:01:40
【问题描述】:

我在 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";

【问题讨论】:

    标签: php json elasticsearch aggregation analysis


    【解决方案1】:

    您可以使用 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 中工作。

    【讨论】:

    • 这段代码给出了这个错误Fielddata is disabled on text fields by default. Set fielddata=true on [PublicationDate.keyword] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory.
    • 确保您使用 PublicationDate.keyword 字段进行聚合,其映射类型为 keyword
    • 没看懂,请详细说明
    • 您需要将PublicationDate.keyword 的映射定义为keyword 类型。顺便说一句,你能分享PublicationDate.keyword 字段的当前映射吗?
    【解决方案2】:

    这完成了任务

    $params['body']["aggs"]["PublicationYear"]["terms"]["script"] = "_value.substring(0,4)";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      • 2018-06-24
      • 2017-03-10
      相关资源
      最近更新 更多