_binary From 9826bd8c389cc543848e4d86144177cda693b0e7 Mon Sep 17 00:00:00 2001
From: Gargi Sheguri <gargiegs@gmail.com>
Date: Sun, 12 Nov 2023 19:54:47 -0500
Subject: [PATCH] Fixes to disjunctive query creation logic

---
 src/models/PhraseModel.php | 35 +++++++++++++++++++++++++----------
 1 file changed, 25 insertions(+), 10 deletions(-)

diff --git a/src/models/PhraseModel.php b/src/models/PhraseModel.php
index d56b3320d..a5abccc4f 100755
--- a/src/models/PhraseModel.php
+++ b/src/models/PhraseModel.php
@@ -615,14 +615,25 @@ class PhraseModel extends ParallelModel
         $search_terms = array_merge($search_terms, $split_terms);
         $meta_words = [];
         // Extracts any meta tags specified in the search phrase
-        for ($i = 0; $i < count($search_terms); $i++) {
-            foreach (PhraseParser::$meta_words_list as $meta_word) {
-                if (strpos($search_terms[$i], $meta_word) === 0) {
-                    $meta_words[] = $search_terms[$i];
-                    array_splice($search_terms, $i, 1);
-                    break;
+        for ($i = 0; $i < count($search_terms);) {
+            $found_meta_word = false;
+            if (str_starts_with($search_terms[$i], '-i:') ||
+                str_starts_with($search_terms[$i], '-index:')) {
+                $found_meta_word = true;
+            } else {
+                foreach (PhraseParser::$meta_words_list as $meta_word) {
+                    if (str_starts_with($search_terms[$i], $meta_word)) {
+                        $found_meta_word = true;
+                        break;
+                    }
                 }
             }
+            if ($found_meta_word) {
+                $meta_words[] = $search_terms[$i];
+                array_splice($search_terms, $i, 1);
+            } else {
+                $i++;
+            }
         }
         $phrase = '';
         foreach ($search_terms as $search_term) {
@@ -631,7 +642,7 @@ class PhraseModel extends ParallelModel
                 $phrase .= $search_term . ' ';
             }
         }
-        $locale_tag = L\guessLocaleFromString($search_terms[0]);
+        $locale_tag = L\guessLocaleFromString($phrase);
         $new_terms = PhraseParser::extractPhrases($phrase, $locale_tag);
         foreach ($new_terms as $new_term) {
             $new_term = trim($new_term);
@@ -657,6 +668,10 @@ class PhraseModel extends ParallelModel
                 $search_terms[$i] = $query_part;
             }
         }
+
+        if (count($search_terms) == 0 && count($meta_words) > 0) {
+            $search_terms = [implode(' ', $meta_words)];
+        }
         /*
         The resultant array holds multiple strings, each signifying
         a disjunctive query
@@ -979,7 +994,7 @@ class PhraseModel extends ParallelModel
             $phrase = $this->beginMatch($phrase, "www.", "site:www.");
             $phrase = $this->beginMatch($phrase, "http:", "site:http:");
             // only rewrite info if longer than hash info record length
-            if($len > self::INFO_HASH_LEN) {
+            if ($len > self::INFO_HASH_LEN) {
                 $phrase = $this->beginMatch($phrase, "info:", "info:http://",
                     "/", ["/"]);
                 $phrase = $this->beginMatch($phrase, "info:", "info:http://",
@@ -1771,7 +1786,7 @@ class PhraseModel extends ParallelModel
                                 list(, , $sf, $image_hash) = $image_link_parts;
                             }
                         } else { //old style image link
-                            if(preg_match("/sf\=([^\&]+)/", $image_link,
+                            if (preg_match("/sf\=([^\&]+)/", $image_link,
                                 $sf_match) && preg_match("/n\=([^\&]+)/",
                                     $image_link, $n_match)) {
                                     $sf = $sf_match[1];
@@ -2043,4 +2058,4 @@ class PhraseModel extends ParallelModel
         }
         return $group_iterator;
     }
-}
+}
\ No newline at end of file
-- 
2.42.0

