首页 > 解决方案 > 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"
                }
            }

标签: regexelasticsearchindexingelastic-stackelasticsearch-query

解决方案


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

推荐阅读