【问题标题】:Aggregation in elastic search by field value data弹性搜索中按字段值数据聚合
【发布时间】:2018-06-27 10:59:04
【问题描述】:

我有以下数据集,我希望根据状态进行汇总。不知道如何比较状态的值与拒绝或成功并获得结果的计数。

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2874,
        "max_score": 1,
        "hits": [
            {
                "_index": "testfiles",
                "_type": "testfiles",
                "_id": "testfile.one",
                "_score": 1,
                "_source": {
                    "businessDate": 20171013,
                    "status": "Success"
                }
            },
            {
                "_index": "testfiles",
                "_type": "testfiles",
                "_id": "testfile.two",
                "_score": 1,
                "_source": {
                    "businessDate": 20171013,
                    "status": "Success"
                }
            },
            {
                "_index": "testfiles",
                "_type": "testfiles",
                "_id": "testfile.three",
                "_score": 1,
                "_source": {
                    "businessDate": 20171013,
                    "status": "Rejected"
                }
            },
            {
                "_index": "testfiles",
                "_type": "testfiles",
                "_id": "testfile.four",
                "_score": 1,
                "_source": {
                    "businessDate": 20171013,
                    "status": "Rejected"
                }
            }
        ]
    }
}

有人可以帮助如何在弹性搜索聚合中实现这一点。

预期的响应如下所示

"aggregations": {
        "success_records": 2,
        "rejected_records": 2
    }

【问题讨论】:

    标签: elasticsearch aggregation


    【解决方案1】:

    假设status 字段的类型为text,您需要将其更新为multi-fields,并具有聚合所需的keyword 类型。然后使用查询:

    GET my_index/_search
    {
      "size": 0,
      "aggs": {
        "statuses": {
          "terms": {
            "field": "status.raw"
          }
      }
    }
    

    如果您已经将status 作为keyword 字段,则在上述查询中将status.raw 更改为status

    【讨论】:

    • 没错,在新版本中,类型字符串被分成了两个文本和关键字。正如@Chandra 所说,您必须使用关键字类型。
    • Chandra Praneeth 我能问你一个关于 elasticsearch 的问题吗?
    • @RodriKing,告诉我吧
    • 我想进行通配符查询,但通配符查询不适用于分析/过滤器(匹配查询适用)。我想应用忽略单词重音的分析。与我不能使用的通配符查询一样,我正在使用查询字符串查询。对吗?
    • @RodriKing,抱歉延迟回复,您能否创建一个包含更多详细信息的新问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    • 2016-06-04
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多