【问题标题】:highlight does not properly work with exact phrase match search in elasticsearch突出显示不适用于弹性搜索中的精确短语匹配搜索
【发布时间】:2020-01-30 02:29:23
【问题描述】:

我在突出显示确切的词组搜索时遇到问题。当突出显示返回时,它会突出显示所有术语,而不管确切的搜索。看例子:

Put /test2
PUT /test2/all/1
{
  "name" :         "climate behaviour change"
}
PUT /test2/all/3
{
  "name" :         "climate behaviour change it is climate change"
}

还有我的地图:

{
  "test2": {
    "mappings": {
      "all": {
        "properties": {
          "name": {
            "type": "string"
          }
        }
      }
    }
  }
}

当我运行这个查询时:

GET /test2/_search
{
  "query": {
    "query_string": {
      "fields": ["name"],
      "query": "\"climate change\""

 }
}
    ,"highlight": {
            "fields": {
            "name": {"number_of_fragments": 0}
                }
    }
}

结果是

"hits": [
      {
        "_index": "test2",
        "_type": "all",
        "_id": "3",
        "_score": 0.23013961,
        "_source": {
          "name": "climate behaviour change it is climate change"
        },
        "highlight": {
          "name": [
            "<em>climate</em> behaviour <em>change</em> it is <em>climate</em> <em>change</em>"
          ]
        }
      }
    ]

由于我搜索了确切的气候变化,因此我希望仅在结果的第二部分突出显示,因此不应突出显示气候行为变化。有谁知道为什么会这样? 或任何其他方式我可以得到我想要的?

【问题讨论】:

  • 你用的是什么版本的ES?
  • 我尝试了上面的查询。对我来说效果很好。可以分享一下映射吗。 GET /test2/all/_mapping
  • @Richa 映射添加到帖子中
  • 2.1 中存在一个错误,您可能需要升级 stackoverflow.com/questions/34643781/…
  • @ChintanShah25 我已经尝试过 2.0.2 并且可以在该版本上正常工作。找不到高于 2.1.1 版本的 debian 包,有吗? 2.0.2 的另一个问题是我当前的 kibana 版本不适用于此版本。

标签: elasticsearch


【解决方案1】:

默认情况下query_string 使用OR 运算符see here

宁可使用AND 运算符。

GET /test2/_search
{
  "query": {
    "query_string": {
      "fields": ["name"],
      "query": "\"climate change\"",
      "default_operator": AND

 }
}
    ,"highlight": {
            "fields": {
            "name": {"number_of_fragments": 0}
                }


    }
}

【讨论】:

    猜你喜欢
    • 2012-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多