ElasticSearch:如何在索引上应用正则表达式

时间:2017-04-27 08:51:51

标签: elasticsearch

我试图将搜索查询的返回限制为仅以那些以abc- * pattern开头的索引。

我尝试了以下正则表达式,但它没有用。

{  
  "sort": [
    {
      "timestamp": {
        "order": "desc",
        "unmapped_type": "boolean"
      }
    }
  ],
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "regexp": {
                "index": "abc-*"
            }
        }
      },
      "filter": {
        "bool": {
          "must": [
            {
              "range": {
                "timestamp": {
                   "gte": "now-24h"
                }
              }
            }
          ]
        }
      }
    }
  }   
}

是否可以使用索引查询并对其应用正则表达式? 即使以下情况也没有适当过滤:

{

  "sort": [
    {
      "timestamp": {
        "order": "desc",
        "unmapped_type": "boolean"
      }
    }
  ],
  "query": {
    "filtered": {
      "query": {
        "indices" : {
            "query" : { "regexp" : { "index" : "abc-.*" } }
        }
      },
      "filter": {
        "bool": {
          "must": [
            {
              "range": {
                "timestamp": {
                   "gte": "now-24h"
                }
              }
            }
          ]
        }
      }
    }
  } 

}

3 个答案:

答案 0 :(得分:2)

只需指定index pattern in the URL directly

即可获得更简单的解决方案
POST /abc-*/_search
{  
  "sort": [
    {
      "timestamp": {
        "order": "desc",
        "unmapped_type": "boolean"
      }
    }
  ],
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "range": {
                "timestamp": {
                   "gte": "now-24h"
                }
              }
            }
          ]
        }
      }
    }
  }   
}

答案 1 :(得分:1)

不确定但在不同情况下遇到同样的问题。我认为-中的"abc-*"存在问题。

只需替换 - 使用空格,它将起作用

"index": "abc *"

答案 2 :(得分:0)

URL 中的索引模式只支持原生表达式,不支持正则表达式。不过确实解决了问题。