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

时间:2019-03-03 10:52:56

标签: regex elasticsearch indexing elastic-stack elasticsearch-query

In the official Elasticsearch documentation中写着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
                                    }
                                }
                            ]
                        }
                    }
                }

找不到这样的索引

{
                "_index": "pictures",
                "_type": "picture",
                "_id": "wiskQ2kBi923Omj4U",
                "_score": 1,
                "_source": {
                    "user_id": 1,
                    "tag": [],
                    "text": "some text",
                    "path": "test/test.txt"
                }
            }

1 个答案:

答案 0 :(得分:1)

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
          }
        }
      ]
    }
  }
}