为什么区分大小写在 elasticSearch 中不起作用

why case sensitive doesn't work in elasticSearch

我想在 Elasticsearch 中使用区分大小写 query_string

query_string: {
      default_field : 'message',
      query: 'info',
    }

如果我输入 info,输出会显示 info 以及 INFO

如何在 Elasticsearch 中区分大小写 query_string?

一切都与您的模板有关,您的字段类型是什么以及是否进行了分析。 您可以在下面查看更多详细信息:

https://discuss.elastic.co/t/is-elasticsearch-querying-on-a-field-value-case-sensitive/74005

official ES doc 中所述,不建议将查询字符串用于搜索栏或普通全文搜索。来自同一个 link:

Because it returns an error for any invalid syntax, we don’t recommend using the query_string query for search boxes.

If you don’t need to support a query syntax, consider using the match query. If you need the features of query syntax, use the simple_query_string query, which is less strict.

我建议使用上面推荐的 match 查询,该查询经过分析并提供对文本字段不区分大小写的搜索。因此,在您的示例中,您可以按如下方式定义映射:

"mappings": {
        "properties": {
            "message": {
                "type": "text" --> note `text` type which uses `standard` analyzer
            }
        }
    }

索引示例数据(注意区分大小写的文档)

{
    "message": "foo"
}
{
    "message": "Foo"
}
{
    "message": "FOO"
}

然后使用下面的查询查询数据:

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "message": "foo" -->you can change it to `Foo` and it will still give all results.
                    }
                }
            ]
        }
    }
}

它给出了如下所示的所有结果:

"hits": [
            {
                "_index": "querystring",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.13353139,
                "_source": {
                    "message": "FOO"
                }
            },
            {
                "_index": "querystring",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.13353139,
                "_source": {
                    "message": "Foo"
                }
            },
            {
                "_index": "querystring",
                "_type": "_doc",
                "_id": "3",
                "_score": 0.13353139,
                "_source": {
                    "message": "foo"
                }
            }
        ]

如果您的映射已将 'message' 设置为分析字段,您可以尝试使用字段 'message.keyword'。它将导致区分大小写的搜索。