Search highlight in Elasticsearch (javascript) - Stack Overflow

I am having a problem with result highlighting in Elasticsearch. My query works, it does return results

I am having a problem with result highlighting in 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 am having a problem with result highlighting in 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!

Share Improve this question asked Feb 14, 2014 at 14:46 JesperJesper 5994 silver badges9 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

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. Upon further inspection, the ES docs seem to confirm this:

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:

<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:

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

that simplier version works for me too, elasticsearch 5, node 8.7, elasticsearch.js node module:

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

Working version for ES 2.2. In the highlight section of the query use

require_field_match: false,

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);
});
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745205139a4616558.html

相关推荐

  • Search highlight in Elasticsearch (javascript) - Stack Overflow

    I am having a problem with result highlighting in Elasticsearch. My query works, it does return results

    10小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信