the*_*ogh 2 regex indexing elasticsearch elasticsearch-query elastic-stack
Elasticsearch 官方文档中有这样写Any reserved character can be escaped with a backslash "\*" including a literal backslash character: "\\"
。
你能解释一下为什么这样查询吗
{
"query": {
"bool": {
"must": [
{
"regexp": {
"path": ".*test\/test.txt.*"
}
},
{
"match": {
"user_id": 1
}
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
找不到这样的索引
{
"_index": "pictures",
"_type": "picture",
"_id": "wiskQ2kBi923Omj4U",
"_score": 1,
"_source": {
"user_id": 1,
"tag": [],
"text": "some text",
"path": "test/test.txt"
}
}
Run Code Online (Sandbox Code Playgroud)
由于path
分析字段,正则表达式不会匹配它。原因是test/test.txt
被标记为两个不同的术语:test
和test.txt
。由于path
有一个keyword
数据类型的子字段keyword
将按test/test.txt
原样进行索引,因此您应该查询该字段,即path.keyword
。
使用以下查询:
{
"query": {
"bool": {
"must": [
{
"regexp": {
"path.keyword": ".*test/test.txt.*"
}
},
{
"match": {
"user_id": 1
}
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)