如何使用 elastic.js 获取弹性搜索的所有索引?
How can i get all index on elastic search using elastic.js?
对于我的 angular 应用程序,我需要在弹性搜索上显示所有索引。我可以使用文档查询特定索引,但无法获取弹性搜索中的所有索引。
这是我的代码:
$scope.getallIndex = function(){
$scope.Indexes = ejs.Request().query(ejs.MatchAllQuery()
.doSearch().then(function (body) {
$scope.Indexes = body.hits.hits;
console.log($scope.Indexes);
}
我正在使用 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);
}) }]);
这将 return 所有索引。请参阅 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
});
elasticsearch JS API有一个查询实例上所有索引的方法:https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-cat-indices
唯一的问题是它并不是 return 可读的 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
});
我在 javascript 中进行了如下查询,它返回了索引列表
client.cat.indices({
h: ['index']
}).then(function (body) {
console.log(body);
});
const indices = await client.cat.indices({format: 'json'})
对于我的 angular 应用程序,我需要在弹性搜索上显示所有索引。我可以使用文档查询特定索引,但无法获取弹性搜索中的所有索引。
这是我的代码:
$scope.getallIndex = function(){
$scope.Indexes = ejs.Request().query(ejs.MatchAllQuery()
.doSearch().then(function (body) {
$scope.Indexes = body.hits.hits;
console.log($scope.Indexes);
}
我正在使用 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);
}) }]);
这将 return 所有索引。请参阅 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
});
elasticsearch JS API有一个查询实例上所有索引的方法:https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-cat-indices
唯一的问题是它并不是 return 可读的 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
});
我在 javascript 中进行了如下查询,它返回了索引列表
client.cat.indices({
h: ['index']
}).then(function (body) {
console.log(body);
});
const indices = await client.cat.indices({format: 'json'})