gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:27:22 26 4
gpt4 key购买 nike

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

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

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

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

最佳答案

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

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"
}
}
]

关于elasticsearch - 为什么区分大小写在elasticSearch中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60502370/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com