1

I indexed the post and community models,

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(),
    })

I want to search posts and aggregate the communities
(returns communities of the posts in the result)

I may need to use aggregation, I had difficulties while implementing it, the documentation was not clear for me.

q = Q("multi_match", query=query, fields=['title', 'content'])
document.query(q)
document.aggs.bucket('per_tag', 'terms', field='community')
4
  • 1
    You need to aggregate on a leaf field (e.g. community.id), not on an object field. Can you try that? Commented Jun 26, 2019 at 13:44
  • @Val I try it, it works but not giving me the result I want, could u help plz ? ``` 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 ``` Commented Jun 27, 2019 at 12:02
  • not giving me the result I want it's hard to know what you expect if you don't explain it ;-) Please show some sample data and what results you expect, it'll be much easier for everyone Commented Jun 27, 2019 at 12:23
  • @Val I want to return a list of Community object not only the IDs Commented Jun 30, 2019 at 12:44

1 Answer 1

1

I think you need change the aggregation to something similar to:

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

Because community is a complex objects, and elasticsearch only can do aggregation with simple fields. (keyword or integer)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.