5

我想使用通过嵌套对象进行过滤来构建过滤的 Elasticsearch 查询,并进行聚合以获取嵌套对象列表中嵌套对象的最小值。

过滤部分有效,但我无法将它与 aggs(聚合)部分绑定。当我.aggs.bucket在过滤器之后将该部分添加到我的代码中时,它要么被忽略(在 中不可见search.to_dict()),要么给我语法错误。

任何人都可以给我一个如何将它们绑定在一起的例子吗?nested1.foo.bar我正在尝试从一个响应中获取过滤后的查询结果和计算出的最小值

示例架构:

class MyExample(DocType):
    myexample_id = Integer()
    nested1 = Nested(
        properties={
            'timestamp': Date(),
            'foo': Nested(
                properties={
                    'bar': Float(),
                }
            )
        }
    )
    nested2 = Nested(
        multi=False,
        properties={
            'x': String(),
            'y': String(),
        }
    )

构建查询:

from elasticsearch_dsl import Search, Q

search = Search().filter(
    'nested', path='nested1', inner_hits={},
    query=Q(
        'range', **{
            'nested1.timestamp': {
                'gte': exampleDate1,
                'lte': exampleDate2
            }
        }
    )
).filter(
    'nested', path='nested2', inner_hits={'name': 'x'},
    query=Q(
        'term', **{
            'nested2.x': x
        }
    )
).filter(
    'nested', path='nested2', inner_hits={'name': 'y'},
    query=Q(
        'term', **{
            'nested2.y': y
        }
    )
)

基本上我需要做的是获取每个唯一 MyExample 文档的所有嵌套 nested1.foo.bar 值的最小值(它们具有唯一的 myexample_id 字段)

4

1 回答 1

3

添加

search.aggs\
    .bucket('nested1', 'nested', path='nested1')\
    .bucket('nested_foo', 'nested', path='nested1.foo')\
    .metric('min_bar', 'min', field='nested1.foo.bar')

在下一行应该可以解决问题。

于 2017-01-10T08:43:24.097 回答