必须匹配在 Elasticsearch 中未按预期工作的查询

must match query not working as expected in Elasticsearch

我在下面使用连接到我的 AWS ES 域的 Kibana 创建了我的索引:

PUT sals_poc_test_20210217-7
{
    "settings" : {
      "index" : {
        "number_of_shards" : 10,
        "number_of_replicas" : 1,
        "max_result_window": 50000,
        "max_rescore_window": 50000
      }
    },
    "mappings": {
      "properties": {
        "identifier": {
          "type": "keyword"
        },
        "CLASS_NAME": {
          "type": "keyword"
        },
        "CLIENT_ID": {
          "type": "keyword"
        }
      }
    }
}

然后我索引了 100 个文档,使用下面的命令 returns 所有 100 个文档:

POST /sals_poc_test_20210217-7/_search
{
  "query": {
    "match": {
      "_index": "sals_poc_test_20210217-7"
    }
  }
}

下面是两个示例文档:

{
        "_index" : "sals_poc_test_20210217-7",
        "_type" : "_doc",
        "_id" : "cd0a3723-106b-4aea-b916-161e5563290f",
        "_score" : 1.0,
        "_source" : {
          "identifier" : "xweeqkrz",
          "class_name" : "/Sample_class_name_1",
          "client_id" : "random_str"
        }
      },
{
        "_index" : "sals_poc_test_20210217-7",
        "_type" : "_doc",
        "_id" : "cd0a3723-106b-4aea-b916-161e556329ab",
        "_score" : 1.0,
        "_source" : {
          "identifier" : "xweeqkra",
          "class_name" : "/Sample_class_name_2",
          "client_id" : "random_str_2"
        }
      }

但是当我想通过以下命令按 CLASS_NAME 搜索时:

POST /sals_poc_test_20210217-7/_search
{
  "size": 200,
  "query": { 
    "bool": { 
      "must": [ 
        { "match": { "CLASS_NAME": "/Sample_class_name_1"}}
      ]
    }
  }
}

不仅返回匹配此 class_name 的文档,还返回其他文档。

任何人都可以对这个案例有所了解吗?

我怀疑我编写搜索查询的方式有问题。但是想不通为什么。

谢谢!

弹性搜索,区分大小写。 class_name 不等于 CLASS_NAME 示例文档似乎有 class_name 但索引中的映射似乎有 'CLASS_NAME.

如果我们GET sals_poc_test_20210217-7,两个class 名称属性都应该在索引映射中。一个是在创建索引时创建的,第二个是在将文档添加到索引时创建的。

所以,查询应该在 CLASS_NAME 或 class_name.keyword 上,默认情况下弹性搜索会为动态属性创建文本和 .keyword 字段

    "CLASS_NAME" : {
      "type" : "keyword"
    },

    "class_name" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    }