_binary From 8e475f6fde5a0004fda23ad050be9cff343fe43f 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 | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)

diff --git a/src/models/PhraseModel.php b/src/models/PhraseModel.php
index d56b3320d..85fcf03c9 100755
--- a/src/models/PhraseModel.php
+++ b/src/models/PhraseModel.php
@@ -584,7 +584,7 @@ class PhraseModel extends ParallelModel
      * @return array of disjunct search phrases
      */
     public function parseWordStructDisjunctiveQuery(&$search_phrase,
-                                                    $guess_semantics)
+        $guess_semantics)
     {
         $phrase = $search_phrase;
         $search_terms = [];
@@ -612,16 +612,18 @@ class PhraseModel extends ParallelModel
             $split_terms[] = $s;
             $s = '';
         }
-        $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;
-                }
+        $meta_pattern = '/(' . implode('|', array_map('preg_quote',
+                PhraseParser::$meta_words_list)) . ')/';
+        foreach ($split_phrase as $word) {
+            if (preg_match($meta_pattern, $word)) {
+                $meta_words[] = $word;
+            }
+        }
+        foreach ($split_terms as $term) {
+            if (!in_array($term, $meta_words)) {
+                $search_terms[] = $term;
             }
         }
         $phrase = '';
@@ -631,7 +633,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 +659,9 @@ 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 +984,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://",
-- 
2.42.0

