弹性搜索:如何突出显示搜索词后的片段?

时间:2019-04-20 12:01:16

标签: elasticsearch

我正在研究一个搜索项目,该项目需要在搜索词后加亮显示片段。

我的查询是

{
  "query": {
        "multi_match" : {
            "query" : "prawn",
            "fields": ["name"]
            , "operator": "and",
            "use_dis_max": true
        }
  },

  "_source": ["name"],
  "highlight": {
    "fields": {
      "name": {
        "pre_tags" : [""], "post_tags" : [""],
        "fragment_size": 3,
        "number_of_fragments": 1
      }
    }
  }
}

结果:

{
 "name" : "special prawn curry"
        },
        "highlight" : {
          "name" : [
            "special prawn"
          ]
        }

我想得到这样的结果

 "name" : "special prawn curry"
        },
        "highlight" : {
          "name" : [
            "prawn curry"
          ]
        }

,即搜索词后的片段。有可能吗?

1 个答案:

答案 0 :(得分:0)

好吧,您可以在突出显示查询中使用Plain highlighter (using "type":"plain"),看看是否可行。

这一直是默认的荧光笔,直到6.0 release成为Unified的默认荧光笔。

POST <your_index_name>/_search
{
  "query": {
        "multi_match" : {
            "query" : "prawn",
            "fields": ["name"]
            , "operator": "and",
            "use_dis_max": true
        }
  },

  "_source": ["name"],
  "highlight": {
    "fields": {
      "name": {
        "type": "plain",                        <---- Added this
        "pre_tags" : [""], "post_tags" : [""],
        "fragment_size": 3,
        "number_of_fragments": 1,
        "order": "score"
      }
    }
  }
}

希望这会有所帮助!