【问题标题】:Check if the index exists or not Elasticsearch检查索引是否存在 Elasticsearch
【发布时间】:2015-09-16 09:46:07
【问题描述】:

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

我正在使用 Elasticsearch 库。

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

【问题讨论】:

    标签: php indexing elasticsearch


    【解决方案1】:

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

    使用卷曲:

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

    【讨论】:

    • 我无法在我的应用程序中使用 localhost,这就是为什么我正在寻找其他解决方案...
    【解决方案2】:

    根据index operationssource code,以下应该可以工作

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

    【讨论】:

    • 值得注意的是,使用更高版本的elastic(我在7.6)访问ES的用户需要view_index_metadata的权限。如果使用 Kibana,您可以通过management/security/roles 的索引权限部分访问这些选项,然后通过management/security/users 将角色分配给用户
    • $client->indices()->exists($indexParams); 将返回真或假
    【解决方案3】:

    这将返回真或假:

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

    【讨论】:

    • 真假或 1 或 0?
    【解决方案4】:

    使用 Facade 的其他方式:

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

    【讨论】:

      【解决方案5】:

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

      【讨论】:

        猜你喜欢
        • 2015-11-18
        • 1970-01-01
        • 2021-07-22
        • 2012-11-30
        • 2021-02-02
        • 2013-05-28
        • 2010-09-20
        • 2016-05-03
        • 1970-01-01
        相关资源
        最近更新 更多