简体   繁体   中英

Elasticsearch highlight with nested objects

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.

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.

您可以在字段中使用force_source“:true,以便在连接嵌套字段后突出显示文档。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM