Skip to content

Highlight nested query #960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
samlauff opened this issue Jul 22, 2018 · 2 comments
Closed

Highlight nested query #960

samlauff opened this issue Jul 22, 2018 · 2 comments

Comments

@samlauff
Copy link

First off - awesome library.

Is there a way to highlight hits from nested queries? I don't see anything about it in the docs:

http://elasticsearch-dsl.readthedocs.io/en/latest/api.html#elasticsearch_dsl.Search.highlight

@honzakral
Copy link
Contributor

honzakral commented Jul 22, 2018

You can highlight things if you use the inner_hits option (0). Just pass inner_hits={"highlight": {"fields": {"nested_path.field": {}}}} to your nested query like so:

s = Search().query('nested', path='comments', query=Q('match', comments__content='python'), inner_hits={"highlight": {"fields": {"comments.content": {}}}})

for hit in s:
    for comment in hit.meta.inner_hits.comments.hits:
        # not 100% sure about the path, but look into comment.meta
        for fragment in comment.meta.highlight['comment.content']:
            print(fragment)

Hope this helps!

0 - https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html#nested-inner-hits

@samlauff
Copy link
Author

Worked. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants