让我们看看为什么它不起作用?我已经使用 Kibana UI 索引了两个文档,如下所示 -
PUT myindex/_doc/1
{
"log.file.path" : "/var/log/dev-collateral/uaa.2020-09-26.log"
}
PUT myindex/_doc/2
{
"log.file.path" : "/var/log/dev-collateral/uaa.2020-09-26.txt"
}
当我尝试使用_analyze API 查看log.file.path 字段中文本的tokens 时
POST _analyze
{
"text": "/var/log/dev-collateral/uaa.2020-09-26.log"
}
它给了我,
{
"tokens" : [
{
"token" : "var",
"start_offset" : 1,
"end_offset" : 4,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "log",
"start_offset" : 5,
"end_offset" : 8,
"type" : "<ALPHANUM>",
"position" : 1
},
{
"token" : "dev",
"start_offset" : 9,
"end_offset" : 12,
"type" : "<ALPHANUM>",
"position" : 2
},
{
"token" : "collateral",
"start_offset" : 13,
"end_offset" : 23,
"type" : "<ALPHANUM>",
"position" : 3
},
{
"token" : "uaa",
"start_offset" : 24,
"end_offset" : 27,
"type" : "<ALPHANUM>",
"position" : 4
},
{
"token" : "2020",
"start_offset" : 28,
"end_offset" : 32,
"type" : "<NUM>",
"position" : 5
},
{
"token" : "09",
"start_offset" : 33,
"end_offset" : 35,
"type" : "<NUM>",
"position" : 6
},
{
"token" : "26",
"start_offset" : 36,
"end_offset" : 38,
"type" : "<NUM>",
"position" : 7
},
{
"token" : "log",
"start_offset" : 39,
"end_offset" : 42,
"type" : "<ALPHANUM>",
"position" : 8
}
]
}
您可以看到,当您将输入文本插入索引时,Elasticsearch 已将它们拆分为标记。这是因为 elasticsearch 在我们索引文档时使用标准分析器,并且它将我们的文档分割成小部分作为标记,删除标点符号、小写文本等。这就是您当前的正则表达式查询不起作用的原因。
GET myindex/_search
{
"query": {
"match": {
"log.file.path": "var"
}
}
}
如果您尝试这种方式,它会起作用,但对于您的情况,您需要匹配每个以 .log 结尾的 log.file.path 那么现在该怎么办?只是在索引文档时不要应用分析器。关键字类型按原样存储您提供的字符串。
使用keyword 类型创建映射,
PUT myindex2/
{
"mappings": {
"properties": {
"log.file.path": {
"type": "keyword"
}
}
}
}
索引文档,
PUT myindex2/_doc/1
{
"log.file.path" : "/var/log/dev-collateral/uaa.2020-09-26.log"
}
PUT myindex2/_doc/2
{
"log.file.path" : "/var/log/dev-collateral/uaa.2020-09-26.txt"
}
使用regexp搜索,
GET myindex2/_search
{
"query": {
"regexp": {
"log.file.path": "/var/log/dev-collateral/uaa.2020-09-26.*"
}
}
}