Iterate through an array that contains 2 more arrays - full of objects
<p>I'm using Elasticsearch (v1.5) JS (AngularJS) client msearch() API call to make 1 search request with 2 types of queries - a multi_match query and mlt query. Everything works EXCEPT paginating through the results for the multi_match query.</p>

<p>ES returns a responses[array] that contains the results for for both queries in their own hits [array]. So basically you have </p>

<pre><code>responses: [[hits],[hits]]
</code></pre>

<p>The first hits array is for the multi_match query the second hits array is for the results for mlt query. The multi_match query returns 100 results and the mlt query returns a max of 8.</p>

<p>This is how I'm processing the responses [] in my search() in my controller</p>

<pre><code>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 = [];
    vm.results.queries = [];
    for (var i = 0; i &lt; results.length; i++) {
      for (var j = 0; j &lt; results.length; j++) {
      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;
      }
    }
</code></pre>

<p>So as you can see I'm using nested for loop to process the <code>responses array []</code> - is that the right approach for this? I'm loading the first 100 results and displaying 10 results at a time.</p>

<p>I'm using AngularJS UI Boostrap pagination directive. My ng-change="changePage" - simply calls paginate() which does this</p>

<pre><code>vm.paginate = function() {
    var begin = ((vm.currentPage - 1) * vm.itemsPerPage);
    var end = vm.currentPage * vm.itemsPerPage;
    vm.results.documents._source = vm.results.pagination.slice(begin, end);
    console.log(vm.results.documents._source);
  };
</code></pre>

<p>The ONLY thing that is not working as expected is the pagination for the <code>multi_match</code> query and it started when I switched from <code>esClient.search()</code> to <code>esClient.msearch()</code> and had to process the <code>responses[]</code>. I'm not quite sure how else to process the responses array?</p>

<p><strong>UPDATE</strong> Ok, I think I'm starting to understand all this a bit better. I'm really not just dealing with a JSON response like so, response:[[hits],][hits]]</p>

<p>Its more like </p>

<pre><code>{response:property: value,property: value, hits:{property: value, property: value}, hits: [{property:value,property:value}]... }
</code></pre>

<p>So both hits arrays are full of objects. How do I iterate through this properly so that pagination works as well?</p>

<p><strong>UPDATE 2</strong> Ok, so after some more reading (and hopefully better understanding)... I think I have this. I finally came upon this <a href="https://stackoverflow.com/questions/29953198/foreach-loop-in-angularjs">SO answer</a> and the data I'm dealing with is very similar so it looks like angular.forEach will work for this... I hope. I've noticed that my question is very similar to lots of others regarding arrays and iteration and so forth - I'm guessing thats why THIS question is not getting much traction. However, when I get this working, I will post here with any relevant info.</p>