]> git.openstreetmap.org Git - nominatim.git/blobdiff - lib-php/SearchDescription.php
remove Token from explicit input for SearchDescription extension
[nominatim.git] / lib-php / SearchDescription.php
index 3c572f2fc872e1e41a47580b80d156834870b964..b4a78eb8e70a9e7274460340e4c76e23db53f91c 100644 (file)
@@ -152,21 +152,17 @@ class SearchDescription
     /**
      * Derive new searches by adding a full term to the existing search.
      *
-     * @param object $oSearchTerm  Description of the token.
-     * @param string $sPhraseType  Type of phrase the token is contained in.
-     * @param bool   $bFirstToken  True if the token is at the beginning of the
-     *                             query.
-     * @param bool   $bFirstPhrase True if the token is in the first phrase of
-     *                             the query.
-     * @param bool   $bLastToken   True if the token is at the end of the query.
+     * @param object  $oSearchTerm  Description of the token.
+     * @param object  $oPosition    Description of the token position within
+                                    the query.
      *
      * @return SearchDescription[] List of derived search descriptions.
      */
-    public function extendWithFullTerm($oSearchTerm, $sPhraseType, $bFirstToken, $bFirstPhrase, $bLastToken)
+    public function extendWithSearchTerm($oSearchTerm, $oPosition)
     {
         $aNewSearches = array();
 
-        if (($sPhraseType == '' || $sPhraseType == 'country')
+        if ($oPosition->maybePhrase('country')
             && is_a($oSearchTerm, '\Nominatim\Token\Country')
         ) {
             if (!$this->sCountryCode) {
@@ -175,19 +171,19 @@ class SearchDescription
                 $oSearch->sCountryCode = $oSearchTerm->sCountryCode;
                 // Country is almost always at the end of the string
                 // - increase score for finding it anywhere else (optimisation)
-                if (!$bLastToken) {
+                if (!$oPosition->isLastToken()) {
                     $oSearch->iSearchRank += 5;
                     $oSearch->iNamePhrase = -1;
                 }
                 $aNewSearches[] = $oSearch;
             }
-        } elseif (($sPhraseType == '' || $sPhraseType == 'postalcode')
+        } elseif ($oPosition->maybePhrase('postalcode')
                   && is_a($oSearchTerm, '\Nominatim\Token\Postcode')
         ) {
             if (!$this->sPostcode) {
                 // If we have structured search or this is the first term,
                 // make the postcode the primary search element.
-                if ($this->iOperator == Operator::NONE && $bFirstToken) {
+                if ($this->iOperator == Operator::NONE && $oPosition->isFirstToken()) {
                     $oSearch = clone $this;
                     $oSearch->iSearchRank++;
                     $oSearch->iOperator = Operator::POSTCODE;
@@ -200,7 +196,7 @@ class SearchDescription
                 // If we have a structured search or this is not the first term,
                 // add the postcode as an addendum.
                 if ($this->iOperator != Operator::POSTCODE
-                    && ($sPhraseType == 'postalcode' || !empty($this->aName))
+                    && ($oPosition->isPhrase('postalcode') || !empty($this->aName))
                 ) {
                     $oSearch = clone $this;
                     $oSearch->iSearchRank++;
@@ -212,7 +208,7 @@ class SearchDescription
                     $aNewSearches[] = $oSearch;
                 }
             }
-        } elseif (($sPhraseType == '' || $sPhraseType == 'street')
+        } elseif ($oPosition->maybePhrase('street')
                  && is_a($oSearchTerm, '\Nominatim\Token\HouseNumber')
         ) {
             if (!$this->sHouseNumber && $this->iOperator != Operator::POSTCODE) {
@@ -257,7 +253,7 @@ class SearchDescription
                     $aNewSearches[] = $oSearch;
                 }
             }
-        } elseif ($sPhraseType == ''
+        } elseif ($oPosition->isPhrase('')
                   && is_a($oSearchTerm, '\Nominatim\Token\SpecialTerm')
         ) {
             if ($this->iOperator == Operator::NONE) {
@@ -273,7 +269,7 @@ class SearchDescription
                         $iOp = Operator::NEAR;
                     }
                     $oSearch->iSearchRank += 2;
-                } elseif (!$bFirstToken && !$bLastToken) {
+                } elseif (!$oPosition->isFirstToken() && !$oPosition->isLastToken()) {
                     $oSearch->iSearchRank += 2;
                 }
                 if ($this->sHouseNumber) {
@@ -287,7 +283,7 @@ class SearchDescription
                 );
                 $aNewSearches[] = $oSearch;
             }
-        } elseif ($sPhraseType != 'country'
+        } elseif (!$oPosition->isPhrase('country')
                   && is_a($oSearchTerm, '\Nominatim\Token\Word')
         ) {
             $iWordID = $oSearchTerm->iId;
@@ -295,8 +291,10 @@ class SearchDescription
             // of the phrase. In structured search the name must forcably in
             // the first phrase. In unstructured search it may be in a later
             // phrase when the first phrase is a house number.
-            if (!empty($this->aName) || !($bFirstPhrase || $sPhraseType == '')) {
-                if (($sPhraseType == '' || !$bFirstPhrase) && $oSearchTerm->iTermCount > 1) {
+            if (!empty($this->aName) || !($oPosition->isFirstPhrase() || $oPosition->isPhrase(''))) {
+                if (($oPosition->isPhrase('') || !$oPosition->isFirstPhrase())
+                    && $oSearchTerm->iTermCount > 1
+                ) {
                     $oSearch = clone $this;
                     $oSearch->iNamePhrase = -1;
                     $oSearch->iSearchRank += 1;
@@ -314,6 +312,13 @@ class SearchDescription
                 }
                 $aNewSearches[] = $oSearch;
             }
+        } elseif (!$oPosition->isPhrase('country')
+                  && is_a($oSearchTerm, '\Nominatim\Token\Partial')
+        ) {
+            $aNewSearches = $this->extendWithPartialTerm(
+                $oSearchTerm,
+                $oPosition
+            );
         }
 
         return $aNewSearches;
@@ -322,55 +327,43 @@ class SearchDescription
     /**
      * Derive new searches by adding a partial term to the existing search.
      *
-     * @param string  $sToken             Term for the token.
-     * @param object  $oSearchTerm        Description of the token.
-     * @param bool    $bStructuredPhrases True if the search is structured.
-     * @param integer $iPhrase            Number of the phrase the token is in.
-     * @param array[] $aFullTokens        List of full term tokens with the
-     *                                    same name.
+     * @param object  $oSearchTerm  Description of the token.
+     * @param object  $oPosition    Description of the token position within
+                                    the query.
      *
      * @return SearchDescription[] List of derived search descriptions.
      */
-    public function extendWithPartialTerm($sToken, $oSearchTerm, $bStructuredPhrases, $iPhrase, $aFullTokens)
+    private function extendWithPartialTerm($oSearchTerm, $oPosition)
     {
-        // Only allow name terms.
-        if (!(is_a($oSearchTerm, '\Nominatim\Token\Word'))
-            || strpos($sToken, ' ') !== false
-        ) {
-            return array();
-        }
-
         $aNewSearches = array();
         $iWordID = $oSearchTerm->iId;
 
-        if ((!$bStructuredPhrases || $iPhrase > 0)
+        if (($oPosition->isPhrase('') || !$oPosition->isFirstPhrase())
             && (!empty($this->aName))
         ) {
             $oSearch = clone $this;
             $oSearch->iSearchRank++;
-            if (preg_match('#^[0-9 ]+$#', $sToken)) {
+            if (preg_match('#^[0-9 ]+$#', $oSearchTerm->sToken)) {
                 $oSearch->iSearchRank++;
             }
             if ($oSearchTerm->iSearchNameCount < CONST_Max_Word_Frequency) {
                 $oSearch->aAddress[$iWordID] = $iWordID;
             } else {
                 $oSearch->aAddressNonSearch[$iWordID] = $iWordID;
-                if (!empty($aFullTokens)) {
-                    $oSearch->iSearchRank++;
-                }
             }
             $aNewSearches[] = $oSearch;
         }
 
         if ((!$this->sPostcode && !$this->aAddress && !$this->aAddressNonSearch)
-            && ((empty($this->aName) && empty($this->aNameNonSearch)) || $this->iNamePhrase == $iPhrase)
+            && ((empty($this->aName) && empty($this->aNameNonSearch))
+                || $this->iNamePhrase == $oPosition->getPhrase())
         ) {
             $oSearch = clone $this;
             $oSearch->iSearchRank++;
             if (empty($this->aName) && empty($this->aNameNonSearch)) {
                 $oSearch->iSearchRank++;
             }
-            if (preg_match('#^[0-9 ]+$#', $sToken)) {
+            if (preg_match('#^[0-9 ]+$#', $oSearchTerm->sToken)) {
                 $oSearch->iSearchRank++;
             }
             if ($oSearchTerm->iSearchNameCount < CONST_Max_Word_Frequency) {
@@ -385,12 +378,9 @@ class SearchDescription
                 }
                 $oSearch->aName[$iWordID] = $iWordID;
             } else {
-                if (!empty($aFullTokens)) {
-                    $oSearch->iSearchRank++;
-                }
                 $oSearch->aNameNonSearch[$iWordID] = $iWordID;
             }
-            $oSearch->iNamePhrase = $iPhrase;
+            $oSearch->iNamePhrase = $oPosition->getPhrase();
             $aNewSearches[] = $oSearch;
         }
 
@@ -415,7 +405,6 @@ class SearchDescription
     public function query(&$oDB, $iMinRank, $iMaxRank, $iLimit)
     {
         $aResults = array();
-        $iHousenumber = -1;
 
         if ($this->sCountryCode
             && empty($this->aName)