regex - Slash doesn't work in matching query using regexp in Elasticsearch
问题描述
In the official Elasticsearch documentation there is written Any reserved character can be escaped with a backslash "\*" including a literal backslash character: "\\"
.
Can you explain me why query like this
{
"query": {
"bool": {
"must": [
{
"regexp": {
"path": ".*test\/test.txt.*"
}
},
{
"match": {
"user_id": 1
}
}
]
}
}
}
doesn't find index like this
{
"_index": "pictures",
"_type": "picture",
"_id": "wiskQ2kBi923Omj4U",
"_score": 1,
"_source": {
"user_id": 1,
"tag": [],
"text": "some text",
"path": "test/test.txt"
}
}
解决方案
Since path
is analysed field the regex wont match it. The reason is that test/test.txt
get tokenised into two different terms: test
and test.txt
. Since path
has a sub-field keyword
of data type keyword
which will index test/test.txt
as it is, you should query on this field i.e. path.keyword
.
Use the below query:
{
"query": {
"bool": {
"must": [
{
"regexp": {
"path.keyword": ".*test/test.txt.*"
}
},
{
"match": {
"user_id": 1
}
}
]
}
}
}
推荐阅读
- google-cloud-platform - 尝试通过 gulp-gcloud-publish 包将编译后的 CSS 发布到 GCP 存储桶时出现“TypeError:file.pipe 不是函数”
- laravel - Livewire:如何获取新创建行的 ID?
- asp.net - 如果从 [Authorized] 重定向到帐户/登录,则 oauth 的 CORS 问题
- .htaccess - 使用 htaccess 删除尾随 /connect 字符串(301 重定向)
- pine-script - Pine Script - 有没有办法及时找到价格,但只需根据该变量在价格上划一条水平线
- python - 如何从 Azure Blob 存储下载图像并将其存储到本地目录中?
- ios - 如何在 Xcode 12.4 上运行具有控制中心的 iOS 模拟器?
- python - 如何修改networkx有向图的外观并向边缘添加权重
- go - 如何动态添加列到 container.NewGridWithColumns()
- c++ - 使用 C++ 验证数字