首页 > 解决方案 > 是否可以在 elasticsearch-js 中使用高亮显示?

问题描述

我正在开发以下代码片段:

app.get('/search', function (req, res){
  let body = {
    size: 200,
    from: 0, 
    highlight: {
      pre_tags : ["<tag1>"],
      post_tags : ["</tag1>"],
      fields: {
        "_content" : {}
      }
    }
  }
  client.search({index:'juridico',  q: req.query['q'], body: body})
  .then(results => {
    console.log(results);
    res.send(results.hits.hits);
  })
  .catch(err=>{
    console.log(err)
    res.send([]);
  });

})

但是当我在 body 上传递 highlight 参数时,我没有得到它的响应。

标签: node.jselasticsearch

解决方案


我不得不像这样解决身体周围的领域:

let body = {
  highlight: {
    require_field_match: false,
    fields: {
      '*': {
        pre_tags: ['<span>'],
        post_tags: ['</span>']
      }
    }
  }
}

我使用'*'here 作为通配符来搜索所有字段。我相信如果您想搜索具有相同前置/后置修复的许多字段,您也可以使用它。希望这对你有用!


推荐阅读