ElasticSearch 无法 return 关键字字段的结果,其中值包含撇号

ElasticSearch fails to return a result on a keyword field where the value contains an apostrophe

我正在开发的应用程序允许在标签中使用任何字符。标签作为数组存储在文档中。其他字符似乎工作正常,但撇号不工作。在以前版本的 ES 中,我们将此字段映射为分析字符串,但当前版本不允许我们使用我们正在使用的某些聚合,因此我将其更改为关键字,此问题浮出水面。当我查询具有指定标签的文档时,即使我知道我的查询对于该字段是正确的,但我没有得到任何结果(除了包含撇号的标签之外,所有其他标签的相同查询 returns 结果)。设置如下:

字段映射:

{ 
    "tags" : {
       "type" : "keyword",
       "store" : true
    }
}

字段数据:

"tags" : [
    "Mike's Tag"
  ]

查询

{
  "query": {
    "bool": {
      "should": [
        {
          "query_string": {
            "query": "tags:\"Mike's Test\"",
            "default_operator": "AND"
          }
        }
      ]
    }
  }
}

如果您不想对标签进行评分,您可以使用 term 查询来获得所需的结果。

使用下面的查询:

{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "tags": "Mike's Tag"
          }
        }
      ]
    }
  }
}

评论后更新:

如果您想通过 query_string 执行此操作,请使用以下查询:

{
  "query": {
    "query_string": {
      "query": "Mike's Tag",
      "fields": [
        "tags"
      ]
    }
  }
}

注意:如果您不想评分,我仍然建议您在过滤器上下文中使用 termterms 查询。

上述查询的输出(使用 term 查询):

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0,
    "hits": [
      {
        "_index": "test",
        "_type": "_doc",
        "_id": "1",
        "_score": 0,
        "_source": {
          "tags": [
            "Mike's Tag",
            "Another Tag"
          ]
        }
      },
      {
        "_index": "test",
        "_type": "_doc",
        "_id": "2",
        "_score": 0,
        "_source": {
          "tags": [
            "Mike's Tag",
            "Another one Tag"
          ]
        }
      }
    ]
  }
}