【发布时间】: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