Elasticsearch highlight with nested objects
Asked Answered
C

2

7

I have a question about highlighting nested object fields.

Consider record like this:

_source: {

    id: 286
    translations: [
        {
            id: 568
            language: lang1
            value: foo1 bar1
        }
        {
            id: 569
            language: lang2
            value: foo2 bar2
        }
    ]

}

If the translations.value has ngram filter, is it possible to highlight matches in nested object such as this one? And how would the highlight query look like.

Thanks a lot for response.

Corporeity answered 5/3, 2013 at 17:44 Comment(1)
Hit the same problem today - it tells me I have a highlight (e.g. value: <em>foo2</em> bar2 when I search for foo2) but not simple way of tying that back to the source documentInch
S
3

Same problem over here. It seems that there is now way to do it in elastic search and won't be in near future.

Developer Shay Banon wrote:

In order to do highlighting based on the nested query, the nested documents needs to be extracted as well in order to highlight it, which is more problematic (and less performant).

Also:

His explanation was that this would take a good amount of memory as there can be a large number of children. And it looks genuine to me as adding this feature will violate the basic concept of processing only N number of feeds at a time.

So the only way is to process the result of a query manually in your own programm to add the highlights.

Update

I don't know about tire or ngram filters but i found a way to retrieve all filter matching nested documents by using nested facets and facet filters. You need a seperate query for highlighting but its much faster than browsing through _source, in my case at least.

{"query":
    {"match_all":{}},
    "facets":{
        "matching_translations":{
            "nested":"translations",
            "terms":{"field":"translations.value"},
            "facet_filter":{
                "bool":{"must":[{"terms":{"translations.value":["foo1"]}}]}
            }
        }
    }
}

You can use the resulting facet terms for highlighting in your programm.

For example: i want to highlight links to nested documents (in jquery):

 setHighlights = function(sdata){
        var highlightDocs = [];
        if(sdata['facets'] && sdata['facets']['docIDs'] && sdata['facets']['doctIDs']['terms'] && sdata['facets']['docIDs']['terms'].length >0){
            for(var i in sdata['facets']['docIDs']['terms']){
                highlightDocs.push(sdata['facets']['docIDs']['terms'][i]['term'])
            }
        }
        $('li.document_link').each(function(){
            if($.inArray($(this).attr('id'),highlightDocs) != -1) {
                $(this).addClass('document_selected');
            }
        });

I hope that helps a little.

Sinciput answered 22/3, 2013 at 13:5 Comment(0)
G
-1

You can use force_source" : true in the fields to cause the document be highlighted once nested fields are joined.

Glasscock answered 29/3, 2014 at 23:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.