【问题标题】:Slash doesn't work in matching query using regexp in Elasticsearch在 Elasticsearch 中使用正则表达式匹配查询时,斜线不起作用
【发布时间】:2019-07-24 20:51:39
【问题描述】:

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

【问题讨论】:

  • 你不需要转义/,是吗?
  • @MatBailie 取决于引擎。在某些引擎中,您需要逃脱。但不确定 OP 使用的是哪个引擎
  • 能否也添加索引的映射?
  • @NishantSaini 我无法将其粘贴到已编辑的帖子中,因此我将其粘贴在这里pastebin.com/MmwmVDU0

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


【解决方案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
          }
        }
      ]
    }
  }
}

【讨论】:

  • 请注意 .* 在正则表达式 (".*test/test.txt.*") 开头和结尾的重要性。没有它们,我没有得到任何匹配。
猜你喜欢
  • 1970-01-01
  • 2017-06-18
  • 2014-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-22
相关资源
最近更新 更多