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

问题描述 投票:2回答:3

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

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

如果输入info,则输出将显示infoINFO

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

elasticsearch case-sensitive elasticsearch-query
3个回答
0
投票
有关模板,字段类型以及是否进行了分析的所有内容。您可以在下面查看更多详细信息:

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


0
投票
official ES doc中提到的搜索栏或常规全文搜索不建议使用查询字符串。从同一链接:

由于它针对任何无效语法返回错误,因此我们不建议使用query_string查询搜索框。

如果您不需要支持查询语法,请考虑使用匹配项查询。如果您需要查询语法的功能,请使用simple_query_string查询,不那么严格。

我建议使用上面建议的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" } } ]


0
投票
如果您的映射已将'message'设置为已分析字段,则可以尝试使用'message.keyword'字段。这将导致区分大小写的搜索。
© www.soinside.com 2019 - 2024. All rights reserved.