我想在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
由于它针对任何无效语法返回错误,因此我们不建议使用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" } } ]