0

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?

user3125823
  • 1,846
  • 2
  • 18
  • 46

1 Answers1

1

It looks like the JSON you have is not valid JSON as you have duplicate keys. You might want to reconsider the way you are generating the json values.

Here is a post on the similar lines :

https://stackoverflow.com/a/38267020/6794233

and what happens to your iterations, when you have duplicates keys is here.

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
      }
    }]
  ]
};
Community
  • 1
  • 1
Sreekanth
  • 3,110
  • 10
  • 22
  • thanks for having a look. I don't have duplicate keys, I just didn't want to type out all the real data that I receive. The property/keys are unique. – user3125823 Nov 11 '16 at 15:02