Elasticsearch DSL python查询,在嵌套属性上使用过滤器和聚合



我想通过嵌套对象进行过滤,构建一个经过过滤的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文档(它们有唯一的MyExample_id字段)获取所有嵌套的1.foo.bar值的最小值

添加

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

在下一行应该做的把戏。

最新更新