在嵌套和非嵌套对象上组合ElasticSearch聚合

时间:2017-04-12 20:50:54

标签: elasticsearch elasticsearch-5 elasticsearch-aggregation

如何组合两个存储桶聚合的结果(在本例中为术语聚合),一个在嵌套字段上,另一个不在。我想将结果合并到一个桶列表中(包括其他子聚合)。

示例文件

{ "foo":
  { "nesty": [
    { "bar":{"id":1}},
    { "bar":{"id":2}}
    ]
  }
}

{ "foo": { "bar" : {"id": 1} } }

这是我到目前为止所尝试的内容

GET _search
{
  "size": 0,
  "aggs": {
    "a": {
      "nested": {
        "path": "foo.nesty"
      },
      "aggs": {
        "a": {
          "terms": {
            "field": "foo.nesty.bar.id"
          }
        }
      }
    },
    "b": {
      "terms": {
        "field": "for.bar.id"
      }
    },
    "c":{
      "<some pipeline agg>":{...},
      "aggs": {...}
    }
  }
}

似乎没有一个管道聚合可以让我合并来自其他两个的桶。

我还尝试使用聚合术语中的脚本组合这两者:

 "c": {
  "nested": {
    "path": "foo.nesty"
  },
  "aggs": {
    "c": {
      "terms": {
        "script": {
          "inline": "(doc['for.bar.id'].value != null) ? doc['for.bar.id'].value : doc['foo.nesty.bar.id']"
        }
      },
      "aggs": {...}
    }
  }
}

但这不起作用!一旦我使用嵌套我似乎无法访问其他字段。相反,如果我在子聚合中使用reverse_nested,我将无法访问嵌套字段。

有没有办法在脚本中同时访问它们?

如果我能做到这一点,我可以让它发挥作用。

为了清楚起见,我期待这种回应:

{
  ...

  "aggregations" : {
      "c" : {  
          "buckets" : [ 
            {
                "key" : "1",
                "doc_count" : 2
            },
            {
                "key" : "2",
                "doc_count" : 1
            }
          ]
      }
  }
}

示例映射:

"mappings": {
  "test": {
    "dynamic": "false",
    "properties": {
      "foo": {
        "dynamic": "false",
        "properties": {
          "bar": {
            "dynamic": "false",
            "properties": {
              "id": {
                "type": "long"
              }
            }
          },
          "nesty": {
            "type": "nested",
            "properties": {
              "bar": {
                "dynamic": "false",
                "properties": {
                  "id": {
                    "type": "long"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

0 个答案:

没有答案
相关问题