首页 > 解决方案 > 如何在多个索引上使用 python elasticseach 模块进行查询?

问题描述

只是为了测试,我有一个包含如下索引的弹性搜索节点:

服务日志 17032020 服务日志 20032020 服务日志 21032020

我正在尝试使用 service-log-* 模式构建用于搜索所有索引的查询。此查询与完整索引名称完美配合,我如何搜索所有索引?

index = INDEX_NAME
query_body = {
            "from":0,
            "size":100,
            "query": {  
              "bool": {
                "must": [
                  {
                    "match" : {
                      "field": "text"
                    }
                  },
                  {                       
                    "range": {
                      "@timestamp": {
                        "gt":str(date)
                      }
                    }
                  }
                ]
              }               
            }
        }

result = elastic_client.search(index=INDEX_NAME, body=query_body)

标签: pythonelasticsearch

解决方案


由于您使用的是 Python 客户端,因此您可以执行以下操作:

from elasticsearch import Elasticsearch

es = Elasticsearch()

# Queries all indices in the cluster.
es.search(index="*", body=...)

# Queries all indices that start with 'logs-'
es.search(index="logs-*", body=...)

# Queries 'logs-1', 'logs-2', and 'logs-5'.
# Serializes to 'logs-1,logs-2,logs-5' in the URL. 
es.search(index=["logs-1", "logs-2", "logs-5"], body=...)

<披露:我维护 Python Elasticsearch 客户端并受雇于 Elastic>


推荐阅读