【问题标题】:How can i get all index on elastic search using elastic.js?如何使用 elastic.js 获取弹性搜索的所有索引?
【发布时间】:2015-03-20 19:08:48
【问题描述】:

使用我的 Angular 应用程序,我需要在弹性搜索中显示所有索引。我可以使用文档查询特定索引,但无法获取弹性搜索中的所有索引。

这里是Documentation

这是我的代码:

$scope.getallIndex = function(){
         $scope.Indexes =  ejs.Request().query(ejs.MatchAllQuery()
            .doSearch().then(function (body) {
         $scope.Indexes = body.hits.hits;
          console.log($scope.Indexes);       
        }

【问题讨论】:

标签: javascript json angularjs elasticsearch


【解决方案1】:
  const indices = await client.cat.indices({format: 'json'})

【讨论】:

    【解决方案2】:

    我正在使用 elasticsearch.js,它是用于 Elastic Search 的浏览器构建,可以在浏览器中使用。请参阅link 。我维护了一个工厂:

     .factory('ElasticService', [ 'esFactory', function (elasticsearch) {
        var client = elasticsearch({
          host: ip + ':' + port,
        });  
    
    client.cat.indices("b",function(r,q){
      console.log(r,q);
    }) }]);
    

    这将返回所有索引。完整配置请参阅link

    编辑: 下面是从特定索引中检索数据的完整工厂。

     .factory('ElasticService', ['$q', 'esFactory', '$location', function ($q, elasticsearch, $location) {
    var client = elasticsearch({
      host: ip + ':' + port
    });
    
    
    var search = function (index, body) {
      var deferred = $q.defer();
      client.search({
        index: index,
        type: type,
        body: body
      }).then(function (result) {
        var took = result.took;
        var size = result.hits.total;
        var ii = 0, hits_in, hits_out = [],aggs = [], highlight = [];
        hits_in = (result.hits || {}).hits || [];
        /* For the timebeing i have maintained this variable to save the aggregations */
        aggs = result.aggregations;
        for (; ii < hits_in.length; ii++) {
          hits_in[ii].fields.id = hits_in[ii]._id;
          hits_out.push(hits_in[ii].fields);
          // hits_out[hits_in[ii]._id]=hits_in[ii].fields: use this method if you wanna keep _id as the index
    
          // if there is a highlight coming along with the data we add a field highlight and push it to built an array
          if (hits_in[ii].highlight) {
            highlight.push(hits_in[ii].highlight);
          }
        }
        if (highlight.length) {
          deferred.resolve({hits: hits_out, highlight: highlight,aggs:aggs, size: size, took: took});
        }
        else {
          deferred.resolve({hits: hits_out, size: size,aggs:aggs, took: took});
        }
    
      }, deferred.reject);
      return deferred.promise;
    };
    return {search: search};  }]);
    

    所以控制器可以使用这个工厂从特定索引中获取数据

      ElasticService.search('<index>', <query>).then(function (resp) {
        // resp has the content
      });
    

    【讨论】:

    • 谢谢,是否可以在 angualr 中使用它来执行数据检索和插入?
    • @Sajeetharan:我已经用检索数据的功能对其进行了编辑。
    【解决方案3】:

    我在 javascript 中进行了如下查询,它返回了我的索引列表

    client.cat.indices({
      h: ['index']
    }).then(function (body) {
      console.log(body);
    });
    

    【讨论】:

      【解决方案4】:

      elasticsearch JS API 有一个查询实例上所有索引的方法:https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-cat-indices

      唯一的问题是它并没有真正以可读的 JSON 格式返回给您。所以我最终做的是类似以下的事情

      client.cat.indices({
        h: ['index', 'docs.count']
      }).then(function (body) {
        let lines = body.split('\n');
        let indices = lines.map(function (line) {
          let row = line.split(' ');
          return {name: row[0], count: row[1]};
        });
        indices.pop(); //the last line is empty by default
      });
      

      【讨论】:

        猜你喜欢
        • 2018-05-12
        • 1970-01-01
        • 1970-01-01
        • 2015-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多