斜杠在 Elasticsearch 中使用正则表达式匹配查询时不起作用

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)

Nis*_*ini 6

由于path分析字段,正则表达式不会匹配它。原因是test/test.txt被标记为两个不同的术语:testtest.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)