高亮显示不适用于elasticsearch中的精确短语匹配搜索

时间:2016-01-28 15:41:09

标签: elasticsearch

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

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>"
          ]
        }
      }
    ]

因为我搜索了确切的气候变化,我希望只对结果的第二部分有所突出,所以不应该强调气候行为的变化。有谁知道为什么会这样? 或任何其他方式我可以得到我想要的东西?

1 个答案:

答案 0 :(得分:0)

默认情况下,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}
                }


    }
}