【问题标题】:elasticsearch: use aggregated value for filteringelasticsearch:使用聚合值进行过滤
【发布时间】:2014-09-18 21:02:18
【问题描述】:

我正在使用嵌套映射(如下),它代表一个“任务”,并且有一个“请求”的嵌套元素正在朝着该任务取得进展。

我正在尝试查找所有尚未取得进展的任务,即嵌套对象上的“最大”聚合为空的所有文档。这需要能够过滤聚合的结果 - 这就是我有点卡住的地方。

我可以根据the aggregation 的结果订购。但我找不到过滤方法。 有这样的能力吗?

映射:

mapping = {
  properties: {
    'prefix' => {
      type: "string",
      store: true, 
      index: "not_analyzed"
    },
    'last_marker' => {
      type: "string", 
      store: true, 
      index: "not_analyzed"
    },
    'start_time' => {
      type: "date", 
      store: true, 
      index: "not_analyzed"
    },
    'end_time' => {
      type: "date", 
      store: true, 
      index: "not_analyzed"
    },
    'obj_count' => {
      type: "long", 
      store: true, 
      index: "not_analyzed"
    },
    'requests' => {
      type: 'nested',
      include_in_parent: true,
      'properties' => {
        'start_time' => {
          type: "date", 
          store: true, 
          index: "not_analyzed"
        },
        'end_time' => {
          type: "date", 
          store: true, 
          index: "not_analyzed"
        },
        'amz_req_id' => {
          type: "string", 
          store: true, 
          index: "not_analyzed"
        },
        'last_marker' => {
          type: "string", 
          store: true, 
          index: "not_analyzed"
        }
      }
    }
  }
}

按聚合查询排序(并寻找过滤器...):

{
  "size":0,
  "aggs": {
    "pending_prefix": {
      "terms": {
        "field": "prefix",
        "order": {"max_date": "asc"},
        "size":20000
      },
      "aggs": {
        "max_date": {
          "max": {
            "field": "requests.end_time"
          }
        }
      }
    }
  }
}

【问题讨论】:

  • “嵌套对象上的“最大”聚合为空的文档是什么意思?为了让这个聚合给出空值,整个文档的 requests.end_time 字段是否需要为空?如果是这样,您不能搜索不包含字段 requests.end_time 的文档
  • 尝试在映射中去掉store: true(至少对于要聚合的字段)。我有同样的问题。由于某种原因,Elasticsearch 不会对映射中具有此标志的字段进行聚合。不要忘记在映射更改后重新索引您的数据

标签: elasticsearch filtering aggregation facet elasticsearch-aggregation


【解决方案1】:

这就像 SQL 术语中的 HAVING 子句。在当前的 Elasticsearch 版本中是不可能的。

在即将发布的 2.0 版本中,新引入的 Pipeline Aggregation 应该可以实现。

更多:https://www.elastic.co/blog/out-of-this-world-aggregations

【讨论】:

    猜你喜欢
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    • 2014-02-02
    • 2016-03-14
    相关资源
    最近更新 更多