Fix no results when no:guess used

Chris Pollett [2023-12-07 16:Dec:th]
Fix no results when no:guess used
Filename
src/library/index_bundle_iterators/UnionIterator.php
src/models/PhraseModel.php
diff --git a/src/library/index_bundle_iterators/UnionIterator.php b/src/library/index_bundle_iterators/UnionIterator.php
index f966d3f8c..82e4340cc 100644
--- a/src/library/index_bundle_iterators/UnionIterator.php
+++ b/src/library/index_bundle_iterators/UnionIterator.php
@@ -30,6 +30,7 @@
  */
 namespace seekquarry\yioop\library\index_bundle_iterators;

+use seekquarry\yioop\configs as C;
 use seekquarry\yioop\library\IndexManager;
 use seekquarry\yioop\library\PhraseParser;
 /**
@@ -110,26 +111,25 @@ class UnionIterator extends IndexBundleIterator
         */
         $this->num_iterators = count($index_bundle_iterators);
         $this->num_docs = 0;
-        $this->results_per_block = 0;
+        /*
+            result_per_block is at most the sum of
+            results_per_block of things we are iterating. Value
+            is already init'd in base class.
+         */
+        $this->results_per_block = C\MIN_RESULTS_TO_GROUP;
         $this->key_iterator_table = [];
         $this->seen_docs = 0;
         $this->seen_docs_unfiltered = 0;
         $this->index_name = $index_name;
         $this->total_num_docs = $total_num_docs;
         $this->low_scoring_terms = [];
-        for ($i = 0; $i < self::RESULTS_PER_BLOCK; $i++) {
+        for ($i = 0; $i < $this->results_per_block; $i++) {
             $this->results_heap[$i][self::SCORE] = 0;
         }
-        $this->initializeTermsHeap($this->terms_heap);
+        $this->initializeTermsHeap();
         for ($i = 0; $i < $this->num_iterators; $i++) {
+            $this->index_bundle_iterators[$i]->setResultsPerBlock(1);
             $this->num_docs += $this->index_bundle_iterators[$i]->num_docs;
-            /*
-                result_per_block is at most the sum of
-                results_per_block of things we are iterating. Value
-                is already init'd in base class.
-             */
-            $this->results_per_block +=
-                $this->index_bundle_iterators[$i]->results_per_block;
             $this->seen_docs += $this->index_bundle_iterators[$i]->seen_docs;
             if (isset($this->index_bundle_iterators[$i]->seen_docs_unfiltered)){
                 $this->seen_docs_unfiltered +=
@@ -208,7 +208,6 @@ class UnionIterator extends IndexBundleIterator
                 $this->getDocScore($this->low_scoring_terms, $relevance_score);
             $relevance_score += $additional_score;
         }
-
         if (!empty($doc) && $relevance_score >
             $this->results_heap[0][self::SCORE]) {
             // Update the document's scores
@@ -222,7 +221,7 @@ class UnionIterator extends IndexBundleIterator
         $found_top_results = $this->results_heap[0][self::SCORE] > 0;
         if (!$found_docs || $found_top_results) {
             $pages = ($this->results_heap[
-                self::RESULTS_PER_BLOCK - 1][self::SCORE] == 0) ?
+                $this->results_per_block - 1][self::SCORE] == 0) ?
                 -1 : $this->getResultsHeap();
         } else {
             $pages = [$doc];
@@ -251,7 +250,7 @@ class UnionIterator extends IndexBundleIterator
         }
         // Re-initialize the results heap for the next set of docs
         $initial_heap_item = [self::SCORE => 0, self::DOC_INFO => null];
-        $this->results_heap = array_fill(0, self::RESULTS_PER_BLOCK,
+        $this->results_heap = array_fill(0, $this->results_per_block,
             $initial_heap_item);
         if (empty($pages)) {
             $pages = -1;
@@ -364,22 +363,22 @@ class UnionIterator extends IndexBundleIterator
      * associated with the nested word iterators on the current
      * union iterator instance.
      *
-     * @param array $terms heap
      */
-    public function initializeTermsHeap(&$terms)
+    public function initializeTermsHeap()
     {
-        if (!empty($terms)) {
+        if (!empty($this->terms_heap)) {
             return;
         }
+        $this->terms_heap = [];
         $num_iterators = $this->num_iterators;
         for ($i = 0; $i < $num_iterators; $i++) {
             $iterator =  $this->index_bundle_iterators[$i];
-            $terms[] = [
+            $this->terms_heap[] = [
                 self::ITERATOR => $i,
                 self::MAX_SCORE => $iterator->getMaxScore(),
                 self::NEXT_DOC => $iterator->currentGenDocOffsetWithWord()
             ];
-            $this->heapifyUp($terms, true);
+            $this->heapifyUp($this->terms_heap, true);
         }
     }
     /**
diff --git a/src/models/PhraseModel.php b/src/models/PhraseModel.php
index 6eb1b2e16..b2b501087 100755
--- a/src/models/PhraseModel.php
+++ b/src/models/PhraseModel.php
@@ -1986,9 +1986,8 @@ class PhraseModel extends ParallelModel
             }
             $index = IndexManager::getIndex($actual_index_name);
             $index_info = $index->getArchiveInfo($index->dir_name);
-            $N = $index_info['VISITED_URLS_COUNT'];
             $union_iterator = new I\UnionIterator($iterators,
-                $actual_index_name, $N);
+                $actual_index_name, $index_info['VISITED_URLS_COUNT']);
         }
         $raw = intval($raw);
         if ($raw > 0) {
ViewGit