简体   繁体   English

Elasticsearch中的搜索突出显示(javascript)

[英]Search highlight in Elasticsearch (javascript)

I am having a problem with result highlighting in Elasticsearch. 我在Elasticsearch中突出显示结果时遇到问题。 My query works, it does return results, but they are NOT highlighted... so I've been searching but I can't find what I'm doing wrong! 我的查询有效,它确实返回结果,但是未突出显示它们……因此我一直在搜索,但找不到我做错的事情!

This is my code: 这是我的代码:

function search(searchInput){
    emptyTable();
    client.search({
        index: 'movies',
        size: 5,
        body: {
            query: {
                //match: {_all: searchInput}
                "term": {
                    "_all" : searchInput                
                }
            },
            "highlight": {
            "require_field_match": true,
            "fields": {
                "_all": {
                    "pre_tags": [
                        "<b>"
                    ],
                    "post_tags": [
                        "</b>"
                    ]
                }
            }
        }
        }
    }).then(function (resp) {
        var hits = resp.hits.hits;
        var hitcount = resp.hits.total;
        if(!jQuery.isEmptyObject(hits)){
            console.log(hits);
            $.each(hits, function(key,obj) {                        
                if(key%2==0){
                    $('#table').append('<tr><td>'+obj._source.imdbid+'</td><td>'+obj._source.name+'</td><td>'+obj._source.desc+'</td></tr>');
                }else{
                    $('#table').append('<tr class="even"><td>'+obj._source.imdbid+'</td><td>'+obj._source.name+'</td><td>'+obj._source.desc+'</td></tr>');              
                }
            }); 
        }
        $('#count').html("Aantal resultaten: "+hitcount);
    });
}

I am searching data then putting it in a table, works fine. 我正在搜索数据,然后将其放在表中,效果很好。 But the highlighting is not working at all. 但是突出显示根本不起作用。 Please help me out! 请帮帮我!

I was having this same problem, and it turns out that when you specify the highlight parameter, elasticsearch returns not only the '_source' fields, but also 'highlight' fields. 我遇到了同样的问题,事实证明,当您指定Highlight参数时,elasticsearch不仅返回'_source'字段,而且还返回'highlight'字段。 Upon further inspection, the ES docs seem to confirm this: 经过进一步检查,ES 文档似乎确认了这一点:

there will be another element in each search hit, called highlight, which includes the highlighted fields and the highlighted fragments 每个搜索命中都会有另一个元素,称为突出显示,其中包括突出显示的字段和突出显示的片段

So, to get this working, you'd need to swap '_source' for 'highlight' in your code: 因此,要使其正常工作,您需要在代码中将“ _source”替换为“ highlight”:

<td>'+obj.highlight.name+'</td>

I also found that ES also puts the highlight response in square brackets, so in my case, (using AngularJS) I accessed the value as follows: 我还发现ES还将突出显示的响应放在方括号中,因此在我的情况下(使用AngularJS)我按如下方式访问该值:

// ...ng-repeat=result in results...
<p ng-bind-html="result.highlight.body[0]">{{result.highlight.body[0]}}</p>

Working version for ES 2.2. ES 2.2的工作版本。 In the highlight section of the query use 在查询的突出显示部分中使用

require_field_match: false, require_field_match:否,

function search(searchInput){
emptyTable();
client.search({
index: 'movies',
size: 5,
body: {
    query: {
             //match: {_all: searchInput}
             term: {
                    _all: searchText
                   }
            },
   highlight: {
                require_field_match: false,
                fields: {
                    "*": {
                            "pre_tags": [
                                "<b>"
                            ],
                            "post_tags": [
                                "</b>"
                            ]

                        }
                    }
       }

}
}).then(function (resp) {
var hits = resp.hits.hits;
var hitcount = resp.hits.total;
if(!jQuery.isEmptyObject(hits)){
    console.log(hits);
    $.each(hits, function(key,obj) {                        
    if(key%2==0){
        // All highlight fields here...
        $('#table').append('<tr><td>'+obj.highlight.imdbid+'</td><td>'+obj.highlight.name+'</td><td>'+obj.highlight.desc+'</td></tr>');
    }else{
        $('#table').append('<tr class="even"><td>'+obj._source.imdbid+'</td><td>'+obj._source.name+'</td><td>'+obj._source.desc+'</td></tr>');              
    }
    }); 
}
$('#count').html("Aantal resultaten: "+hitcount);
});
}

that simplier version works for me too, elasticsearch 5, node 8.7, elasticsearch.js node module: 这个更简单的版本也适用于我,elasticsearch 5,节点8.7,elasticsearch.js节点模块:

    response = await client.search({
      index: indexName,
      type: indexType,
      q: query,
        highlight: {
          fields: {
            "*": {
              "pre_tags": ["<b>"],
              "post_tags": ["</b>"]
           }
         }
        }
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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