Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if the index exists or not Elasticsearch

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();**
like image 392
Istiak Mahmood Avatar asked Jun 29 '15 19:06

Istiak Mahmood


People also ask

How to Check if an index exists in Elasticsearch?

curl –i -XHEAD 'http://localhost:9200/myindex/' Copy If the index exists an HTTP status code 200 is returned, if missing a 404 is returned.


3 Answers

As per index operations and source code the following should work

 $client = new Elasticsearch\Client();
 $indexParams['index']  = 'my_index';   
 $client->indices()->exists($indexParams);
like image 72
keety Avatar answered Oct 19 '22 17:10

keety


This will return true or false:

$params = ['index' => 'products'];
$bool=$client->indices()->exists($params);
like image 5
shankar kumar Avatar answered Oct 19 '22 16:10

shankar kumar


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'
like image 2
ciscogambo Avatar answered Oct 19 '22 17:10

ciscogambo