对名称与特定模式匹配的索引进行Elasticsearch查询 [英] Elasticsearch Query on indexes whose name is matching a certain pattern

查看:103
本文介绍了对名称与特定模式匹配的索引进行Elasticsearch查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Elasticsearch数据库中有几个索引,如下所示

I have a couple of indexes in my Elasticsearch DB as follows

Index_2019_01

Index_2019_02

Index_2019_03

Index_2019_04

.
.

Index_2019_12

假设我只想搜索前三个索引.我的意思是这样的正则表达式:

Suppose I want to search only on the first 3 Indexes. I mean a regular expression like this:

select count(*) from Index_2019_0[1-3] where LanguageId="English"

在Elasticsearch中执行此操作的正确方法是什么?

What is the correct way to do that in Elasticsearch?

推荐答案

如何查询具有某些名称的多个索引?

这可以通过

How can I query several indexes with certain names?

This can be achieved via multi-index search, which is a built-in capability of Elasticsearch. To achieve described behavior one should try a query like this:

POST /index_2019_01,index_2019_02/_search
{
  "query": {
    "match": {
      "LanguageID": "English"
    }
  }
}

或者,使用 URI搜索:

curl 'http://<host>:<port>/index_2019_01,index_2019_02/_search?q=LanguageID:English'

可在此处获得更多详细信息..请注意,Elasticsearch要求索引名称必须小写.

More details are available here. Note that Elasticsearch requires index names to be lowercase.

简而言之,不.可以在查询中使用特殊的虚拟"字段 _index ,但其使用受到限制.例如,不能对索引名称使用正则表达式:

In short, no. It is possible to use index name in queries using a special "virtual" field _index but its use is limited. For instance, one cannot use a regexp against index name:

_index显示为虚拟字段-不会将其添加到Lucene索引是一个实际字段.这意味着您可以使用_index字词或字词查询(或任何重写为字词查询,例如match,query_string或simple_query_string查询),但不支持前缀,通配符,正则表达式或模糊查询.

例如,上面的查询可以重写为:

For instance, the query from above can be rewritten as:

POST /_search
{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "_index": [
              "index_2019_01",
              "index_2019_02"
            ]
          }
        },
        {
          "match": {
            "LanguageID": "English"
          }
        }
      ]
    }
  }
}

雇用了 bool 术语查询.

希望有帮助!

这篇关于对名称与特定模式匹配的索引进行Elasticsearch查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆