【问题标题】:elastic-Search Aggregation [python]弹性搜索聚合 [python]
【发布时间】:2019-11-08 11:43:39
【问题描述】:

我索引了帖子和社区模型,

post = Index('posts')
post.settings(
    number_of_shards=1,
    number_of_replicas=0
)

@post.doc_type
class PostDocument(DocType):
    community = fields.ObjectField(properties={
        'id': fields.IntegerField(),
        'description': fields.TextField(),
        'name': fields.StringField(),
    })

我想搜索帖子并聚合社区
(返回结果中帖子的社区)

我可能需要使用聚合,我在实施时遇到了困难,文档对我来说不是很清楚。

q = Q("multi_match", query=query, fields=['title', 'content'])
document.query(q)
document.aggs.bucket('per_tag', 'terms', field='community')

【问题讨论】:

  • 您需要在叶字段(例如community.id)上聚合,而不是在对象字段上聚合。你可以试试吗?
  • @Val 我试了一下,它有效,但没有给我想要的结果,你能帮忙吗? ``` q = Q("multi_match", query=query, fields=['title', 'content']) document.query(q) document.aggs.bucket('per_tag', 'terms', field=' community__id') response = document.execute() return response.aggregations.per_tag.buckets ```
  • not giving me the result I want 不解释就很难知道你的期望 ;-) 请展示一些示例数据和你期望的结果,这对每个人都会容易得多
  • @Val 我想返回 Community 对象的列表,而不仅仅是 ID

标签: django elasticsearch aggregate elasticsearch-aggregation elasticsearch-dsl


【解决方案1】:

我认为您需要将聚合更改为类似于:

document.aggs.bucket('per_tag', 'terms', field='community__id')

因为社区是一个复杂的对象,而elasticsearch只能做简单字段的聚合。 (关键字或整数)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 2021-02-19
    • 2023-03-18
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多