检查索引是否存在 Elasticsearch
Check if the index exists or not Elasticsearch
我想在 elasticsearch 中检查索引是否存在。如果它不存在,它应该创建索引并执行其他功能。我试图为此找到解决方案,但没有找到任何完美的解决方案。任何人都可以解决这个问题。
我正在使用 Elasticsearch 库。
**$client = new Elasticsearch\Client();**
此处列出所有索引的文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/_list_all_indexes.html
使用卷曲:
curl 'localhost:9200/_cat/indices?v'
根据index operations and source code,以下应该有效
$client = new Elasticsearch\Client();
$indexParams['index'] = 'my_index';
$client->indices()->exists($indexParams);
这将 return 对或错:
$params = ['index' => 'products'];
$bool=$client->indices()->exists($params);
使用 Facade 的其他方式:
use ScoutElastic\Facades\ElasticClient;
$indexParams['index'] = "model_index";
$exists = ElasticClient::indices()->exists($indexParams);
if ($exists) {
//do somthing
}
我可以用 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
}
}
我想在 elasticsearch 中检查索引是否存在。如果它不存在,它应该创建索引并执行其他功能。我试图为此找到解决方案,但没有找到任何完美的解决方案。任何人都可以解决这个问题。
我正在使用 Elasticsearch 库。
**$client = new Elasticsearch\Client();**
此处列出所有索引的文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/_list_all_indexes.html
使用卷曲:
curl 'localhost:9200/_cat/indices?v'
根据index operations and source code,以下应该有效
$client = new Elasticsearch\Client();
$indexParams['index'] = 'my_index';
$client->indices()->exists($indexParams);
这将 return 对或错:
$params = ['index' => 'products'];
$bool=$client->indices()->exists($params);
使用 Facade 的其他方式:
use ScoutElastic\Facades\ElasticClient;
$indexParams['index'] = "model_index";
$exists = ElasticClient::indices()->exists($indexParams);
if ($exists) {
//do somthing
}
我可以用 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
}
}