Elasticsearch-dsl-py: Highlight nested query

Created on 23 Jul 2018  路  2Comments  路  Source: elastic/elasticsearch-dsl-py

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

Most helpful comment

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

All 2 comments

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

Worked. Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rokcarl picture rokcarl  路  4Comments

beanaroo picture beanaroo  路  4Comments

amih90 picture amih90  路  4Comments

leoliuxd picture leoliuxd  路  4Comments

berinhard picture berinhard  路  3Comments