【问题标题】:ElasticSearch: return some text surrounding a match on a full-text query?ElasticSearch:返回一些围绕全文查询匹配的文本?
【发布时间】:2018-07-03 07:02:49
【问题描述】:

我在文章索引上有一些全文搜索查询:

  "query": {
    "multi_match": {
      "query": article,
      "fields": [ "text" ],
      "minimum_should_match": "75%"
    }
  }

我想知道是否可以将其更改为仅返回部分文本而不是整个匹配的文本。例如,假设我搜索“棕狐”。与其返回整篇文章,我只想返回围绕任何匹配“brown fox”的几个词,这样结果可能是“.. is said that any brown fox could jump over fences..”,不考虑换行。

这在 ES 中可能吗?

【问题讨论】:

标签: elasticsearch full-text-search


【解决方案1】:

正如@Adam-t 提到的,在 EC 中突出显示是此答案的关键。对于将来的参考,我已经添加了我的搜索查询,我可以在其中获得所需的答案。我发布这个答案是因为,我也遇到了同样的问题,我花了一段时间才找到合适的解决方案。

{
   "query":{
      "match_phrase":{
         "text":"investors"
      }
   },
   "highlight":{
      "fragment_size":100,
      "fields":{
         "text":{}
      }
   }
}

上面的搜索查询将通过大文本搜索术语“投资者”并返回如下响应,

"highlight" : {
     "content" : [
          "*stocks closed at a near three-week high on Wednesday, led by blue-chips, but foreign <em>investors</em>",
          "The dollar currency ended weaker. ** Local <em>investors</em> picked up select shares, with one of the two presidential"
      ]
  }

fragment_size 以默认值 100 突出显示周围的文本

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-25
    • 2016-08-12
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多