【问题标题】:Elastic search - how to get aggregate nested _source value弹性搜索 - 如何获取聚合嵌套 _source 值
【发布时间】:2016-10-14 00:48:29
【问题描述】:

我们正在使用弹性搜索来获取一些数据。

请告诉我如何获取聚合 _source.eventName 组数据。 像这样的sql

seletc eventName, count(eventName) from events group by eventName;

这是我的 aggs 查询和当前响应数据结构。

{
    type: 'event',
    size: 3,
    aggs: {
      event_group: {
        terms: {
          field: 'eventName'
        }
      }
    }
}

 

"hits": {
  "hits": [
    {
      "_type": "event",
      "_source": {
        "eventName": "event1",
      }
    },
    {
      "_type": "event",
      "_source": {
        "eventName": "event1",
      }
    },
    {
      "_type": "event",
      "_source": {
        "eventName": "event2",
      }
    }
  ]
}

※理想情况(我想得到这样的结果。)

{
  "eventName": "event1",
  "count": 2
},
{
  "eventName": "event2",
  "count": 1
}

【问题讨论】:

  • 不清楚你在问什么。你的问题到底是什么?
  • @Val 抱歉,我更新了问题
  • 你的聚合看起来不错,你运行了吗?你得到了什么?
  • @Val 我想得到这样的结果 { "eventName": "event1", "count": 2 }, { "eventName": "event2", "count": 1 }
  • 你的聚合得到了什么?

标签: elasticsearch


【解决方案1】:

ElasticSearch 不支持这种过滤,但你可以使用 REST API 过滤参数,它会返回类似的东西

GET .../_search?pretty&filter_path=hits.hits._source.*

  "hits": {
    "hits": [
      {
        "_source": {...},
        "_source": {...},
        "_source": {...},
       }]
    }

弹性搜索Documentation 常用选项

【讨论】:

    【解决方案2】:

    您的查询几乎是正确的。如果您只想要eventNames 的计数,请在此处使用size: 0 而不是3。它会告诉 ES 不要返回hits

    响应应该有一个aggregations 属性,如下所示:

    {
        "aggregations":
            {
                "event_group": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0, 
                    "buckets" : [ 
                        {
                            "key" : "event1",
                            "doc_count" : 10
                        },
                        {
                            "key" : "event2",
                            "doc_count" : 10
                        },
                        {
                            "key" : "event3",
                            "doc_count" : 10
                        },
                    ]
                }
            }
    }
    

    doc_count 属性是您要查找的计数。

    注意: ES 只会返回桶中前 10 个eventName。根据您使用的 ES 版本,如果您想获得所有唯一的 eventNames,您需要在 terms 聚合中指定 size。阅读 ES 文档了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-30
      • 2018-08-06
      • 2019-04-24
      • 2018-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多