Can I iterate through this JSON response from ES (v1.5) with AngularJS forEach?
{response:[property: value,property: value, hits:{property: value, property: value}, hits: [{property:value,property:value, {property:value}}], hits:[{property:value,property:value, {property:value}}]]}
As you can see the response [] has 2 hits arrays, both hits arrays are full of objects. Trying iterate through them using angular.forEach ... but not having much luck
Do I need to break each hits array down and run it through its own 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
Is there a way to iterate through these with a nested forEach? If so, some example code would be great!
UPDATE
Here is a small sample of actual data returned from 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": [
{...
So as you can see in return object there is responses array, that contains 2 separate hits arrays and those hits arrays contains objects that hold the data for the 2 queries.
My search() that returns the results is like so
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;
}
}
The 2 for loops that you see are not the right tool for the job. Any suggestions?