gpt4 book ai didi

elasticsearch - 如果查询完全是 “*”,则通配符查询仅返回结果

转载 作者:行者123 更新时间:2023-12-03 01:31:01 26 4
gpt4 key购买 nike

我正在使用Elasticsearch 6.7.0,并且正在尝试创建wildcard query,例如选择在datafile_url字段以.RLF结尾的文档。

从简单的查询开始,我只使用通配符*查询任何值:

GET data/_search
{
"query": {
"wildcard": {
"datafile_url": "*"
}
}
}

这将返回文档,例如:
{
"_index" : "data",
"_type" : "doc",
"_id" : "1HzJaWoBVj7X61Ih767N",
"_score" : 1.0,
"_source" : {
"datafile_url" : "/uploads/data/1/MSN001.RLF",
...
}
},

太好了。但是,当我将通配符查询更改为 *.RLF时,没有任何结果。

最佳答案

简短答案:这是因为当未为字段明确指定默认分析器时,elastic会应用Standard Analyzer

如果您对关键字进行通配符搜索,它将起作用并返回预期结果:

GET data/_search
{
"query": {
"wildcard": {
"datafile_url.keyword": "*.RLF"
}
}
}

现在,对于 ,一些背景会介绍为什么没有 .keyword不能正常工作

看一下此示例,然后尝试在自己的索引上运行它。
POST data/_analyze
{
"field": "datafile_url",
"text" : "/uploads/data/1/MSN001.RLF"
}

#Result

{
"tokens": [
{
"token": "uploads",
"start_offset": 1,
"end_offset": 8,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "data",
"start_offset": 9,
"end_offset": 13,
"type": "<ALPHANUM>",
"position": 1
},
{
"token": "1",
"start_offset": 14,
"end_offset": 15,
"type": "<NUM>",
"position": 2
},
{
"token": "msn001",
"start_offset": 16,
"end_offset": 22,
"type": "<ALPHANUM>",
"position": 3
},
{
"token": "rlf",
"start_offset": 23,
"end_offset": 26,
"type": "<ALPHANUM>",
"position": 4
}
]
}

请注意,倒排索引中所有特殊字符都丢失了。您的通配符搜索将仅对倒排索引中的上述任何单词起作用。例如:
#this will work
GET data/_search
{
"query": {
"wildcard": {
"datafile_url": "*rlf"
}
}
}

#this will NOT work because of case sensitive inverted index.
GET data/_search
{
"query": {
"wildcard": {
"datafile_url": "*RLF"
}
}
}

如果要保留这些特殊字符,则需要编写 custom analyzer

关于elasticsearch - 如果查询完全是 “*”,则通配符查询仅返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55939074/

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