【发布时间】:2015-09-16 09:46:07
【问题描述】:
我想在 elasticsearch 中检查索引是否存在。如果它不存在,它应该创建索引并执行其他功能。我试图找到一个解决方案,但没有找到任何完美的解决方案。任何人都可以有任何解决方案来解决这个问题。
我正在使用 Elasticsearch 库。
**$client = new Elasticsearch\Client();**
【问题讨论】:
标签: php indexing elasticsearch
我想在 elasticsearch 中检查索引是否存在。如果它不存在,它应该创建索引并执行其他功能。我试图找到一个解决方案,但没有找到任何完美的解决方案。任何人都可以有任何解决方案来解决这个问题。
我正在使用 Elasticsearch 库。
**$client = new Elasticsearch\Client();**
【问题讨论】:
标签: php indexing elasticsearch
此处列出所有索引的文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/_list_all_indexes.html
使用卷曲:
curl 'localhost:9200/_cat/indices?v'
【讨论】:
根据index operations 和source code,以下应该可以工作
$client = new Elasticsearch\Client();
$indexParams['index'] = 'my_index';
$client->indices()->exists($indexParams);
【讨论】:
view_index_metadata的权限。如果使用 Kibana,您可以通过management/security/roles 的索引权限部分访问这些选项,然后通过management/security/users 将角色分配给用户
$client->indices()->exists($indexParams); 将返回真或假
这将返回真或假:
$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
}
}
【讨论】: