From 4864bf150915c20cc7642c94fd183703ac14e343 Mon Sep 17 00:00:00 2001
From: Sarah Hoffmann <lonvia@denofr.de>
Date: Tue, 26 Oct 2021 10:23:55 +0200
Subject: [PATCH] disallow search for partials without address
Very frequent partial terms take too long to look up and
do not return any valuable results unless the search is
further narrowed down by an address.
---
 lib-php/SearchDescription.php | 16 +++++++++++++++-
 lib-php/TokenPartial.php      |  1 +
 2 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/lib-php/SearchDescription.php b/lib-php/SearchDescription.php
index ee8bbc0c..6eaf0d9a 100644
--- a/lib-php/SearchDescription.php
+++ b/lib-php/SearchDescription.php
@@ -19,6 +19,8 @@ class SearchDescription
     private $aName = array();
     /// True if the name is rare enough to force index use on name.
     private $bRareName = false;
+    /// True if the name requires to be accompanied by address terms.
+    private $bNameNeedsAddress = false;
     /// List of word ids making up the address of the object.
     private $aAddress = array();
     /// List of word ids that appear in the name but should be ignored.
@@ -113,6 +115,9 @@ class SearchDescription
                 return false;
             }
         }
+        if ($this->bNameNeedsAddress && empty($this->aAddress)) {
+            return false;
+        }
 
         return true;
     }
@@ -231,6 +236,7 @@ class SearchDescription
     {
         $this->aName[$iId] = $iId;
         $this->bRareName = $bRareName;
+        $this->bNeedsAddress = false;
     }
 
     /**
@@ -240,11 +246,19 @@ class SearchDescription
      * @param integer iID            ID of term to add.
      * @param bool bSearchable       Term should be used to search for result
      *                               (i.e. term is not a stop word).
+     * @param bool bNeedsAddress     True if the term is too unspecific to be used
+     *                               in a stand-alone search without an address
+     *                               to narrow down the search.
      * @param integer iPhraseNumber  Index of phrase, where the partial term
      *                               appears.
      */
-    public function addPartialNameToken($iId, $bSearchable, $iPhraseNumber)
+    public function addPartialNameToken($iId, $bSearchable, $bNeedsAddress, $iPhraseNumber)
     {
+        if (empty($this->aName)) {
+            $this->bNameNeedsAddress = $bNeedsAddress;
+        } else {
+            $this->bNameNeedsAddress |= $bNeedsAddress;
+        }
         if ($bSearchable) {
             $this->aName[$iId] = $iId;
         } else {
diff --git a/lib-php/TokenPartial.php b/lib-php/TokenPartial.php
index 131bb2a3..112154b2 100644
--- a/lib-php/TokenPartial.php
+++ b/lib-php/TokenPartial.php
@@ -90,6 +90,7 @@ class Partial
             $oNewSearch->addPartialNameToken(
                 $this->iId,
                 $this->iSearchNameCount < CONST_Max_Word_Frequency,
+                $this->iSearchNameCount > CONST_Search_NameOnlySearchFrequencyThreshold,
                 $oPosition->getPhrase()
             );
 
-- 
2.39.5