简体   繁体   English

从Elasticsearch聚合中的第一个和最后一个文档返回字段

[英]Return field from first and last document in Elasticsearch aggregation

I have a sorted elasticsearch histogram aggregation that works fine as is. 我有一个排序的弹性搜索直方图聚合,工作正常。

I need to return a field from the first and last document of each aggregation bucket. 我需要从每个聚合桶的第一个和最后一个文档返回一个字段。

Current query (ruby-style syntax): 当前查询(ruby风格的语法):

{
  query: {
    filtered: {
      filter: {
        bool: {
          must: [
            { term: { some_id: 'something' } },
            { range: {
                completed_at: {
                  gte: start,
                  lte: end
                }
              }
            }
          ]
        }
      },
      _cache: true,
      _cache_key: "special-query"
    }
  },
  aggs: {
    intervals: {
      histogram: {
        field: 'completed_at',
        interval: 24.hours.to_i,
        min_doc_count: 0,
        order: { _key: 'asc' }
      },
      aggs: {
        start_at: { min: { field: 'completed_at' } },
        end_at: { max: { field: 'completed_at' } },
        price_stats: { extended_stats: { field: 'rate' } },
      }
    }
  }
}

I have searched through the documentation and googled extensively but can't find any solutions. 我搜索了文档并广泛搜索,但找不到任何解决方案。 Currently I have to send a second query to the database to explicitly fetch all the values for this field but I would like to roll that functionality in to my elasticsearch query. 目前,我必须向数据库发送第二个查询以显式获取此字段的所有值,但我想将该功能滚动到我的elasticsearch查询中。

You want to return a result per aggregation right? 您想要返回每个聚合的结果吗?

That's called Field Collapsing and unfortunately isn't yet available in Elasticsearch, see: https://github.com/elasticsearch/elasticsearch/issues/256 这称为Field Collapsing,遗憾的是Elasticsearch尚未提供,请参阅: https//github.com/elasticsearch/elasticsearch/issues/256

Update sept 2014 2014年9月更新

As per @bpierce in comments below: Now that Elasticsearch 1.3.x is out, field collapsing is available via the top_hits aggregation. 根据以下评论中的@bpierce:现在Elasticsearch 1.3.x已经用完,可以通过top_hits聚合获得字段折叠。 I'm currently using it in a query similar to this one. 我目前在类似于这个的查询中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM