Elasticsearch查询名称与特定模式匹配的索引
Elasticsearch Query on indexes whose name is matching a certain pattern
我的 Elasticsearch 数据库中有几个索引,如下所示
Index_2019_01
Index_2019_02
Index_2019_03
Index_2019_04
.
.
Index_2019_12
假设我只想搜索前 3 个索引。
我的意思是像这样的正则表达式:
select count(*) from Index_2019_0[1-3] where LanguageId="English"
在 Elasticsearch 中正确的做法是什么?
如何查询具有特定名称的多个索引?
这可以通过 multi-index search 实现,这是 Elasticsearch 的内置功能。要实现所描述的行为,应该尝试这样的查询:
POST /index_2019_01,index_2019_02/_search
{
"query": {
"match": {
"LanguageID": "English"
}
}
}
或者,使用 URI search:
curl 'http://<host>:<port>/index_2019_01,index_2019_02/_search?q=LanguageID:English'
可获得更多详细信息here。请注意,Elasticsearch 要求索引名称为小写。
我可以使用正则表达式来指定索引名称模式吗?
简而言之,没有。可以在使用特殊 "virtual" 字段 _index
的查询中使用索引名称,但它的使用是有限的。例如,不能对索引名称使用正则表达式:
The _index is exposed as a virtual field — it is not added to the
Lucene index as a real field. This means that you can use the _index
field in a term or terms query (or any query that is rewritten to a
term query, such as the match, query_string or simple_query_string
query), but it does not support prefix, wildcard, regexp, or fuzzy
queries.
例如,上面的查询可以重写为:
POST /_search
{
"query": {
"bool": {
"must": [
{
"terms": {
"_index": [
"index_2019_01",
"index_2019_02"
]
}
},
{
"match": {
"LanguageID": "English"
}
}
]
}
}
}
希望对您有所帮助!
如果不向其中添加任何其他数据,为什么要使用 POST。
我建议为您的情况使用 GET。其次,如果索引的名称与您的情况相似,您应该使用如下查询中的索引模式,
GET /index_2019_*/_search
{
"query": {
"match": {
"LanguageID": "English"
}
}
}
或 URL
curl -XGET "http://<host>:<port>/index_2019_*/_search" -H 'Content-Type: application/json' -d'{"query": {"match":{"LanguageID": "English"}}}'
虽然无法使用正则表达式搜索索引,但您可以使用日期数学来进一步了解。
你可以看看文档here
例如,假设您希望这些指数的最后 3 个月
这意味着如果我们有
index_2019_01
index_2019_02
index_2019_03
index_2019_04
而今天是 2019/04/20,我们可以使用以下查询来获取 04,03 和 02
GET /<index-{now/M-0M{yyyy_MM}}>,<index-{now/M-1M{yyyy_MM}}>,<index-{now/M-2M{yyyy_MM}}>
我对第一个索引使用了 M-0M,因此查询构造循环不需要第一个索引的特殊情况
查看有关 URL 编码此查询的文档以及如何在索引名称中使用文字大括号,如果使用客户端,则 URL 编码已为您完成(至少在 python客户端)
我的 Elasticsearch 数据库中有几个索引,如下所示
Index_2019_01
Index_2019_02
Index_2019_03
Index_2019_04
.
.
Index_2019_12
假设我只想搜索前 3 个索引。 我的意思是像这样的正则表达式:
select count(*) from Index_2019_0[1-3] where LanguageId="English"
在 Elasticsearch 中正确的做法是什么?
如何查询具有特定名称的多个索引?
这可以通过 multi-index search 实现,这是 Elasticsearch 的内置功能。要实现所描述的行为,应该尝试这样的查询:
POST /index_2019_01,index_2019_02/_search
{
"query": {
"match": {
"LanguageID": "English"
}
}
}
或者,使用 URI search:
curl 'http://<host>:<port>/index_2019_01,index_2019_02/_search?q=LanguageID:English'
可获得更多详细信息here。请注意,Elasticsearch 要求索引名称为小写。
我可以使用正则表达式来指定索引名称模式吗?
简而言之,没有。可以在使用特殊 "virtual" 字段 _index
的查询中使用索引名称,但它的使用是有限的。例如,不能对索引名称使用正则表达式:
The _index is exposed as a virtual field — it is not added to the Lucene index as a real field. This means that you can use the _index field in a term or terms query (or any query that is rewritten to a term query, such as the match, query_string or simple_query_string query), but it does not support prefix, wildcard, regexp, or fuzzy queries.
例如,上面的查询可以重写为:
POST /_search
{
"query": {
"bool": {
"must": [
{
"terms": {
"_index": [
"index_2019_01",
"index_2019_02"
]
}
},
{
"match": {
"LanguageID": "English"
}
}
]
}
}
}
希望对您有所帮助!
如果不向其中添加任何其他数据,为什么要使用 POST。 我建议为您的情况使用 GET。其次,如果索引的名称与您的情况相似,您应该使用如下查询中的索引模式,
GET /index_2019_*/_search
{
"query": {
"match": {
"LanguageID": "English"
}
}
}
或 URL
curl -XGET "http://<host>:<port>/index_2019_*/_search" -H 'Content-Type: application/json' -d'{"query": {"match":{"LanguageID": "English"}}}'
虽然无法使用正则表达式搜索索引,但您可以使用日期数学来进一步了解。
你可以看看文档here
例如,假设您希望这些指数的最后 3 个月
这意味着如果我们有
index_2019_01
index_2019_02
index_2019_03
index_2019_04
而今天是 2019/04/20,我们可以使用以下查询来获取 04,03 和 02
GET /<index-{now/M-0M{yyyy_MM}}>,<index-{now/M-1M{yyyy_MM}}>,<index-{now/M-2M{yyyy_MM}}>
我对第一个索引使用了 M-0M,因此查询构造循环不需要第一个索引的特殊情况
查看有关 URL 编码此查询的文档以及如何在索引名称中使用文字大括号,如果使用客户端,则 URL 编码已为您完成(至少在 python客户端)