gpt4 book ai didi

python - Elasticsearch DSL python 查询,对嵌套属性进行过滤器和聚合

转载 作者:太空宇宙 更新时间:2023-11-03 10:55:35 25 4
gpt4 key购买 nike

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

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

任何人都可以给我一个关于如何将它们绑定(bind)在一起的例子吗?我试图在一个响应中同时获取过滤后的查询结果和从 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 字段)

最佳答案

添加

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

在下一行应该可以做到这一点。

关于python - Elasticsearch DSL python 查询,对嵌套属性进行过滤器和聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41558241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com