Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 检查索引是否存在Elasticsearch_Php_Indexing_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch - Fatal编程技术网 elasticsearch,Php,Indexing,elasticsearch" /> elasticsearch,Php,Indexing,elasticsearch" />

Php 检查索引是否存在Elasticsearch

Php 检查索引是否存在Elasticsearch,php,indexing,elasticsearch,Php,Indexing,elasticsearch,我想在elasticsearch中检查索引是否存在。如果它不存在,它应该创建索引并执行其他功能。我试图找到一个解决方案,但没有找到任何完美的解决方案。有谁能找到解决这个问题的办法吗 我正在使用Elasticsearch库 **$client = new Elasticsearch\Client();** 此处列出了所有索引的文档: 使用curl: curl 'localhost:9200/_cat/indices?v' 根据,以下各项应起作用 $client = new Elasticse

我想在elasticsearch中检查索引是否存在。如果它不存在,它应该创建索引并执行其他功能。我试图找到一个解决方案,但没有找到任何完美的解决方案。有谁能找到解决这个问题的办法吗

我正在使用Elasticsearch库

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

此处列出了所有索引的文档:

使用curl:

curl 'localhost:9200/_cat/indices?v'
根据,以下各项应起作用

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

这将返回true或false:

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

使用Facade的其他方式:

使用ScoutElastic\Facades\ElasticClient;
$indexParams['index']=“模型索引”;
$exists=ElasticClient::Indexes()->exists($indexParams);
如果($存在){
//干坏事
}

我可以用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
  }
}

我无法在我的应用程序中使用localhost,这就是我搜索其他解决方案的原因…是真是假,还是1或0?值得注意的是,对于更高版本的elastic(我使用7.6),访问ES的用户需要查看索引元数据的权限。如果使用Kibana,您可以通过
management/security/roles
的Index privileges部分访问这些选项,然后通过
management/security/users
$client->Index()->exists($indexParams)将角色分配给用户将返回true或false