检查索引是否存在Elasticsearch

时间:2015-06-29 19:30:38

标签: php indexing elasticsearch

如果索引存在与否,我想检查elasticsearch。如果它不存在,它应该创建索引并执行其他功能。我试图找到解决方案,但没有找到任何完美的解决方案。任何人都可以有任何解决方案来解决这个问题。

我正在使用Elasticsearch库。

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

5 个答案:

答案 0 :(得分:22)

根据index operationssource code,以下内容应该有效

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

答案 1 :(得分:2)

这将返回true或false:

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

答案 2 :(得分:1)

我可以用 node.js 做到这一点,如下

const { Client } = require('@elastic/elasticsearch');
const client = new Client({
  node: ES_URL,
});
await client.indices.exists({ index: 'INDEX_NAME' });

并且响应应该与下面的类似:

{
  body: true,
  statusCode: 200,
  headers: {
    date: 'Sun, 07 Mar 2021 13:07:31 GMT',
    server: 'Apache/2.4.46 (Unix) OpenSSL/1.1.1d',
    'content-type': 'application/json; charset=UTF-8',
    'content-length': '2796',
    'keep-alive': 'timeout=5, max=100',
    connection: 'Keep-Alive'
  },
  meta: {
    context: null,
    request: { params: [Object], options: {}, id: 1 },
    name: 'elasticsearch-js',
    connection: {
      url: 'ES_URL',
      id: 'ES_ID',
      headers: {},
      deadCount: 0,
      resurrectTimeout: 0,
      _openRequests: 0,
      status: 'alive',
      roles: [Object]
    },
    attempts: 0,
    aborted: false
  }
}

答案 3 :(得分:0)

此处列出所有索引的文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/_list_all_indexes.html

使用curl:

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

答案 4 :(得分:0)

使用Facade的其他方法:

use ScoutElastic\Facades\ElasticClient;

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