使用我的角度应用程序,我需要在弹性搜索上显示所有索引。我能够使用文档查询特定索引,无法获得弹性搜索的所有索引。
这是我的代码:
$scope.getallIndex = function(){
$scope.Indexes = ejs.Request().query(ejs.MatchAllQuery()
.doSearch().then(function (body) {
$scope.Indexes = body.hits.hits;
console.log($scope.Indexes);
}
答案 0 :(得分: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
});
答案 1 :(得分:2)
我在javascript中进行了如下查询,它返回了索引列表
client.cat.indices({
h: ['index']
}).then(function (body) {
console.log(body);
});
答案 2 :(得分:1)
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
});
答案 3 :(得分:1)
const indices = await client.cat.indices({format: 'json'})
console.log('indices:', indices)