我想要从 ElasticSearch 服务器获取 json 响应的子集。我是 ElasticSearch 和 JavaScript 的新手。请看一看。 json 文件是示例文件 here来自 ElasticSearch 文档。这是一个示例:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1000,
"max_score" : 1.0,
"hits" : [ {
"_index" : "bank",
"_type" : "account",
"_id" : "4",
"_score" : 1.0,
"_source":{"account_number":4,"balance":27658,"firstname":"Rodriquez","lastname":"Flores","age":31,"gender":"F","address":"986 Wyckoff Avenue","employer":"Tourmania","email":"rodriquezflores@tourmania.com","city":"Eastvale","state":"HI"}
}, {
"_index" : "bank",
"_type" : "account",
"_id" : "9",
"_score" : 1.0,
"_source":{"account_number":9,"balance":24776,"firstname":"Opal","lastname":"Meadows","age":39,"gender":"M","address":"963 Neptune Avenue","employer":"Cedward","email":"opalmeadows@cedward.com","city":"Olney","state":"OH"}
}
................................
]
}
}
如何提取 account_number、余额和姓氏的数组,例如 [{"account_number":9,"balance":24776,"lastname":"Meadows"},{"account_number":4,"余额":27658,"姓氏":"弗洛雷斯"},……]
.
这是我使用的代码:
var deferred = $q.defer();
$http.get("http://localhost:9200/bank/_search?q=*").success(function(data){
var country=data.hits.hists.map( function (count) {
return {
account_number: count._source.account_number,
balance: count._source.balance,
lastname: count._source.lastname
};
});
deferred.resolve(country);
});
return deferred.promise;
我从类似的代码中获得了灵感one用于 REST 调用。看来我得到的数据
是未定义
。 ES 端需要配置吗?
最佳答案
您可以使用 source filtering 指示 ES 仅返回这些字段:
$http.get("http://localhost:9200/bank/_search?_source=account_number,balance,lastname&q=*").success(function(data){
这样您就不必自己过滤字段。
那么下一行有一个拼写错误:hists
应该读取 hits
var country=data.hits.hits.map( function (count) {
请尝试一下。
关于javascript - 从 ElasticSearch 获取 json 的子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32098089/