我可以使用AngularJS forEach从ES(v1.5)迭代这个JSON响应吗?
{response:[property: value,property: value, hits:{property: value, property: value}, hits: [{property:value,property:value, {property:value}}], hits:[{property:value,property:value, {property:value}}]]}
正如你所看到的,response []有2个命中数组,两个命中数组都有很多对象。尝试使用angular.forEach迭代它们......但没有太多运气
我是否需要打破每个命中数组并通过自己的forEach运行它?
var multi_match_results = es_return.responses[0].hits.hits;
angular.forEach(multi_match_results), function(value, key) {
...
}
var mlt_results = es_return.responses[1].hits.hits;
angular.forEach(mlt_results), function(value, key) {
...
}
OR
有没有办法用嵌套的forEach迭代这些?如果是这样,一些示例代码就会很棒!
更新
以下是从ES
返回的实际数据的一小部分示例 {
"responses": [
{
"took": 38,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 6,
"max_score": 2.8937743,
"hits": [
{
"_index": "query-index1",
"_type": "autocomplete",
"_id": "AVhO-9ifp_gdq4wcr69k",
"_score": 2.8937743,
"_source": {
"suggestions": "term term"
}
},
{
"_index": "query-index1",
"_type": "autocomplete",
"_id": "AVhO-9ifp_gdq4wcr69l",
"_score": 2.8937743,
"_source": {
"suggestions": "term1 term1"
}
}
]
}
},
{
"took": 51,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 317,
"max_score": 3.055048,
"hits": [
{...
因此,您可以在返回对象中看到响应数组,其中包含2个单独的命中数组,这些命中数组包含保存2个查询数据的对象。
我的搜索结果()返回结果
return searchService.search(vm.searchTerms, vm.currentPage).then(function(es_return) {
var results = es_return.responses;
var totalItems = es_return.responses[0].hits.total;
var totalTime = es_return.responses[0].took;
var numPages = Math.ceil(es_return.responses[0].hits.total / vm.itemsPerPage);
vm.results.pagination = [];
for (var i = 0; i < results.length; i++) {
console.log(results);
for (var j = 0; j < results.length; j++) {
console.log(results);
vm.results.totalItems = totalItems;
console.log(vm.results.totalItems);
vm.results.queryTime = totalTime;
vm.results.pagination = searchService.formatResults(es_return.responses[0].hits.hits);//load first 100 results
vm.results.documents = vm.results.pagination.slice(vm.currentPage, vm.itemsPerPage);
console.log(vm.results.documents);
vm.results.mlt = es_return.responses[1].hits.hits;
}
}
您看到的2 for循环不适合该作业。有什么建议吗?
答案 0 :(得分:1)
看起来你拥有的JSON不是有效的JSON,因为你有重复的密钥。您可能想重新考虑生成json值的方式。
这是一篇类似的帖子:
https://stackoverflow.com/a/38267020/6794233
当您有重复键时,迭代会发生什么 这里。
https://stackoverflow.com/a/5306792/6794233
var responseObj = {
response: [property: value, property: value, hits: {
property: value,
property: value
},
hits: [{
property: value,
property: value,
{
property: value
}
}],
hits: [{
property: value,
property: value,
{
property: value
}
}]
]
};
&#13;