Check if the index exists or not Elasticsearch

筅森魡賤 提交于 2020-01-03 09:05:21

问题


I want to check in elasticsearch if the index exists or not. If it not exists it should create the index and do other functionality. I try to find out a solution for that, but did not find any perfect solution for that. Can anyone have any solution to solve this problem.

I am using Elasticsearch library.

**$client = new Elasticsearch\Client();**

回答1:


As per index operations and source code the following should work

 $client = new Elasticsearch\Client();
 $indexParams['index']  = 'my_index';   
 $client->indices()->exists($indexParams);



回答2:


This will return true or false:

$params = ['index' => 'products'];
$bool=$client->indices()->exists($params);



回答3:


Other way by using Facade:

use ScoutElastic\Facades\ElasticClient;

$indexParams['index']  = "model_index";
$exists = ElasticClient::indices()->exists($indexParams);
if ($exists) {
   //do somthing
}



回答4:


The documentation for list all indexes here: https://www.elastic.co/guide/en/elasticsearch/reference/current/_list_all_indexes.html

Using curl:

curl 'localhost:9200/_cat/indices?v'


来源:https://stackoverflow.com/questions/31123992/check-if-the-index-exists-or-not-elasticsearch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!