快速检查 elasticsearch 索引是否会 return 搜索命中

quick check whether an elasticsearch index will return search hits

我们正在 运行ning 命令针对 elasticsearch 源针对一些索引,如下所示:

curl -XGET 'http://es-server:9200/logstash-2015.01.28,logstash-2015.01.27/_search?pretty' -d @/a_query_file_in_json_format

大多数时候效果很好,我们可以解析我们需要的结果。

然而,当索引处于错误状态时-- 可能存在索引滞后,或者某些分片出现问题-- 上面的查询将 return 没有结果,并且无法知道是否这是因为没有匹配的记录或索引在某种程度上不稳定。

我一直在看 elastic search indices recovery API 但有点不知所措。是否有一些我可以 运行 的查询可以 yes/no 回答 'can a search against these indices be relied upon at the moment?'

您可以通过多种方式获取此信息。

1) 您可以像这样在索引级别使用 cluster health API :

GET _cluster/health/my_index?level=indices

这将输出集群的状态,以及有关状态和索引分片的信息 my_index :

{
   "cluster_name": "elasticsearch_thomas",
   "status": "yellow",
   "timed_out": false,
   "number_of_nodes": 1,
   "number_of_data_nodes": 1,
   "active_primary_shards": 5,
   "active_shards": 5,
   "relocating_shards": 0,
   "initializing_shards": 0,
   "unassigned_shards": 5,
   "indices": {
      "my_index": {
         "status": "yellow",
         "number_of_shards": 5,
         "number_of_replicas": 1,
         "active_primary_shards": 5,
         "active_shards": 5,
         "relocating_shards": 0,
         "initializing_shards": 0,
         "unassigned_shards": 5
      }
   }
}

2) 如果你想要一个不那么冗长的答案,或者只过滤一些特定的信息,你可以依赖 _cat API,它允许你自定义输出。但是,输出不再是 JSON.

例如,如果您只需要索引的名称和运行状况,则以下请求可以解决问题:

GET _cat/indices/my_index?h=index,health&v

通过输出:

index    health 
my_index yellow

请注意,显示列 headers 只是因为详细标志(v 上一个请求中的 GET 参数)。

要获得可用列的完整列表,请使用 help 参数:

GET _cat/indices?help