]> git.openstreetmap.org Git - nominatim.git/blobdiff - lib/SearchDescription.php
Merge branch 'vagrant-centos-with-selinux' of https://github.com/mtmail/Nominatim
[nominatim.git] / lib / SearchDescription.php
index 56d04478c2cd540564534e7859cd95db8d446266..2e72deccc4792908b8067c9cdd30f0a8762a60ad 100644 (file)
@@ -58,7 +58,7 @@ class SearchDescription
     /**
      * Get current search rank.
      *
-     * The higher the search rank the lower the likelyhood that the
+     * The higher the search rank the lower the likelihood that the
      * search is a correct interpretation of the search query.
      *
      * @return integer Search rank.
@@ -68,19 +68,6 @@ class SearchDescription
         return $this->iSearchRank;
     }
 
-    /**
-     * Increase the search rank.
-     *
-     * @param integer $iAddRank Number of ranks to increase.
-     *
-     * @return void
-     */
-    public function addToRank($iAddRank)
-    {
-        $this->iSearchRank += $iAddRank;
-        return $this->iSearchRank;
-    }
-
     /**
      * Make this search a POI search.
      *
@@ -187,12 +174,10 @@ class SearchDescription
      * @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 integer $iGlobalRank  Changable ranking of all searches in the
-     *                              batch.
      *
      * @return SearchDescription[] List of derived search descriptions.
      */
-    public function extendWithFullTerm($aSearchTerm, $bHasPartial, $sPhraseType, $bFirstToken, $bFirstPhrase, $bLastToken, &$iGlobalRank)
+    public function extendWithFullTerm($aSearchTerm, $bHasPartial, $sPhraseType, $bFirstToken, $bFirstPhrase, $bLastToken)
     {
         $aNewSearches = array();
 
@@ -210,12 +195,6 @@ class SearchDescription
                     $oSearch->iSearchRank += 5;
                 }
                 $aNewSearches[] = $oSearch;
-
-                // If it is at the beginning, we can be almost sure that
-                // the terms are in the wrong order. Increase score for all searches.
-                if ($bFirstToken) {
-                    $iGlobalRank++;
-                }
             }
         } elseif (($sPhraseType == '' || $sPhraseType == 'postalcode')
                   && $aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'postcode'
@@ -261,14 +240,17 @@ class SearchDescription
                 $oSearch->sHouseNumber = trim($aSearchTerm['word_token']);
                 // sanity check: if the housenumber is not mainly made
                 // up of numbers, add a penalty
-                if (preg_match_all("/[^0-9]/", $oSearch->sHouseNumber, $aMatches) > 2) {
+                if (preg_match_all('/[^0-9]/', $oSearch->sHouseNumber, $aMatches) > 2) {
                     $oSearch->iSearchRank++;
                 }
                 if (!isset($aSearchTerm['word_id'])) {
                     $oSearch->iSearchRank++;
                 }
                 // also must not appear in the middle of the address
-                if (sizeof($this->aAddress) || sizeof($this->aAddressNonSearch)) {
+                if (sizeof($this->aAddress)
+                    || sizeof($this->aAddressNonSearch)
+                    || $this->sPostcode
+                ) {
                     $oSearch->iSearchRank++;
                 }
                 $aNewSearches[] = $oSearch;
@@ -280,7 +262,7 @@ class SearchDescription
 
                 $iOp = Operator::NEAR; // near == in for the moment
                 if ($aSearchTerm['operator'] == '') {
-                    if (sizeof($this->aName)) {
+                    if (sizeof($this->aName) || $this->oContext->isBoundedSearch()) {
                         $iOp = Operator::NAME;
                     }
                     $oSearch->iSearchRank += 2;
@@ -294,11 +276,12 @@ class SearchDescription
                   && $sPhraseType != 'country'
         ) {
             $iWordID = $aSearchTerm['word_id'];
-            if (sizeof($this->aName)) {
-                if (($sPhraseType == '' || !$bFirstPhrase)
-                    && $sPhraseType != 'country'
-                    && !$bHasPartial
-                ) {
+            // Full words can only be a name if they appear at the beginning
+            // 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 (sizeof($this->aName) || !($bFirstPhrase || $sPhraseType == '')) {
+                if (($sPhraseType == '' || !$bFirstPhrase) && !$bHasPartial) {
                     $oSearch = clone $this;
                     $oSearch->iSearchRank++;
                     $oSearch->aAddress[$iWordID] = $iWordID;
@@ -344,7 +327,7 @@ class SearchDescription
         ) {
             if ($aSearchTerm['search_name_count'] + 1 < CONST_Max_Word_Frequency) {
                 $oSearch = clone $this;
-                $oSearch->iSearchRank++;
+                $oSearch->iSearchRank += 2;
                 $oSearch->aAddress[$iWordID] = $iWordID;
                 $aNewSearches[] = $oSearch;
             } else {
@@ -378,7 +361,7 @@ class SearchDescription
             && (!sizeof($this->aName) || $this->iNamePhrase == $iPhrase)
         ) {
             $oSearch = clone $this;
-            $oSearch->iSearchRank++;
+            $oSearch->iSearchRank += 2;
             if (!sizeof($this->aName)) {
                 $oSearch->iSearchRank += 1;
             }
@@ -468,7 +451,7 @@ class SearchDescription
         }
 
         if (CONST_Debug) {
-            echo "<br><b>Place IDs:</b> ";
+            echo '<br><b>Place IDs:</b> ';
             var_dump(array_keys($aResults));
         }
 
@@ -483,11 +466,11 @@ class SearchDescription
                 if ($aFilteredPlaceIDs) {
                     $aNewResults = array();
                     foreach ($aFilteredPlaceIDs as $iPlaceId) {
-                        $aNewResults[$iPlaceId] = $aResults[$iPLaceId];
+                        $aNewResults[$iPlaceId] = $aResults[$iPlaceId];
                     }
                     $aResults = $aNewResults;
                     if (CONST_Debug) {
-                        echo "<br><b>Place IDs after postcode filtering:</b> ";
+                        echo '<br><b>Place IDs after postcode filtering:</b> ';
                         var_dump(array_keys($aResults));
                     }
                 }
@@ -506,7 +489,7 @@ class SearchDescription
         if ($this->oContext->bViewboxBounded) {
             $sSQL .= ' AND ST_Intersects('.$this->oContext->sqlViewboxSmall.', geometry)';
         }
-        $sSQL .= " ORDER BY st_area(geometry) DESC LIMIT 1";
+        $sSQL .= ' ORDER BY st_area(geometry) DESC LIMIT 1';
 
         if (CONST_Debug) var_dump($sSQL);
 
@@ -561,7 +544,7 @@ class SearchDescription
             if ($this->oContext->sqlCountryList) {
                 $sSQL .= ' AND country_code in '.$this->oContext->sqlCountryList;
             }
-            $sSQL .= ' ORDER BY '.$this->oContext->distanceSQL('centroid')." ASC";
+            $sSQL .= ' ORDER BY '.$this->oContext->distanceSQL('centroid').' ASC';
             $sSQL .= " LIMIT $iLimit";
             if (CONST_Debug) var_dump($sSQL);
             $aDBResults = chksql($oDB->getCol($sSQL));
@@ -656,13 +639,13 @@ class SearchDescription
         }
 
         if ($this->sHouseNumber) {
-            $aTerms[] = "address_rank between 16 and 27";
+            $aTerms[] = 'address_rank between 16 and 27';
         } elseif (!$this->sClass || $this->iOperator == Operator::NAME) {
             if ($iMinAddressRank > 0) {
-                $aTerms[] = "address_rank >= ".$iMinAddressRank;
+                $aTerms[] = 'address_rank >= '.$iMinAddressRank;
             }
             if ($iMaxAddressRank < 30) {
-                $aTerms[] = "address_rank <= ".$iMaxAddressRank;
+                $aTerms[] = 'address_rank <= '.$iMaxAddressRank;
             }
         }
 
@@ -693,7 +676,7 @@ class SearchDescription
         if ($this->sHouseNumber) {
             $sImportanceSQL = '- abs(26 - address_rank) + 3';
         } else {
-            $sImportanceSQL = '(CASE WHEN importance = 0 OR importance IS NULL THEN 0.75-(search_rank::float/40) ELSE importance END)';
+            $sImportanceSQL = '(CASE WHEN importance = 0 OR importance IS NULL THEN 0.75001-(search_rank::float/40) ELSE importance END)';
         }
         $sImportanceSQL .= $this->oContext->viewboxImportanceSQL('centroid');
         $aOrder[] = "$sImportanceSQL DESC";
@@ -728,7 +711,7 @@ class SearchDescription
 
             $aDBResults = chksql(
                 $oDB->getAll($sSQL),
-                "Could not get places for search terms."
+                'Could not get places for search terms.'
             );
 
             foreach ($aDBResults as $aResult) {
@@ -780,8 +763,8 @@ class SearchDescription
                 $sSQL .= "interpolationtype='odd'";
             }
             $sSQL .= " or interpolationtype='all') and ";
-            $sSQL .= $iHousenumber.">=startnumber and ";
-            $sSQL .= $iHousenumber."<=endnumber";
+            $sSQL .= $iHousenumber.'>=startnumber and ';
+            $sSQL .= $iHousenumber.'<=endnumber';
             $sSQL .= $this->oContext->excludeSQL(' AND place_id');
             $sSQL .= " limit $iLimit";
 
@@ -819,8 +802,8 @@ class SearchDescription
                 $sSQL .= "interpolationtype='odd'";
             }
             $sSQL .= " or interpolationtype='all') and ";
-            $sSQL .= $iHousenumber.">=startnumber and ";
-            $sSQL .= $iHousenumber."<=endnumber";
+            $sSQL .= $iHousenumber.'>=startnumber and ';
+            $sSQL .= $iHousenumber.'<=endnumber';
             $sSQL .= $this->oContext->excludeSQL(' AND place_id');
             $sSQL .= " limit $iLimit";
 
@@ -853,9 +836,9 @@ class SearchDescription
             $sSQL .= " WHERE place_id in ($sPlaceIDs)";
             $sSQL .= "   AND class='".$this->sClass."' ";
             $sSQL .= "   AND type='".$this->sType."'";
-            $sSQL .= "   AND linked_place_id is null";
+            $sSQL .= '   AND linked_place_id is null';
             $sSQL .= $this->oContext->excludeSQL(' AND place_id');
-            $sSQL .= " ORDER BY rank_search ASC ";
+            $sSQL .= ' ORDER BY rank_search ASC ';
             $sSQL .= " LIMIT $iLimit";
 
             if (CONST_Debug) var_dump($sSQL);
@@ -883,8 +866,8 @@ class SearchDescription
                 $sSQL .= " WHERE place_id in ($sPlaceIDs)";
                 $sSQL .= "   AND rank_search < $iMaxRank + 5";
                 $sSQL .= "   AND ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon')";
-                $sSQL .= " ORDER BY rank_search ASC ";
-                $sSQL .= " LIMIT 1";
+                $sSQL .= ' ORDER BY rank_search ASC ';
+                $sSQL .= ' LIMIT 1';
                 if (CONST_Debug) var_dump($sSQL);
                 $sPlaceGeom = chksql($oDB->getOne($sSQL));
             }
@@ -910,7 +893,7 @@ class SearchDescription
                     if ($this->oContext->hasNearPoint()) {
                         $sOrderBySQL = $this->oContext->distanceSQL('l.centroid');
                     } elseif ($sPlaceIDs) {
-                        $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
+                        $sOrderBySQL = 'ST_Distance(l.centroid, f.geometry)';
                     } elseif ($sPlaceGeom) {
                         $sOrderBySQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
                     }
@@ -926,7 +909,7 @@ class SearchDescription
                     $sSQL .= ' from '.$sClassTable.' as l';
 
                     if ($sPlaceIDs) {
-                        $sSQL .= ",placex as f WHERE ";
+                        $sSQL .= ',placex as f WHERE ';
                         $sSQL .= "f.place_id in ($sPlaceIDs) ";
                         $sSQL .= " AND ST_DWithin(l.centroid, f.centroid, $fRange)";
                     } elseif ($sPlaceGeom) {
@@ -954,7 +937,7 @@ class SearchDescription
                     if ($this->oContext->hasNearPoint()) {
                         $sOrderBySQL = $this->oContext->distanceSQL('l.geometry');
                     } else {
-                        $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
+                        $sOrderBySQL = 'ST_Distance(l.geometry, f.geometry)';
                     }
 
                     $sSQL = 'SELECT distinct l.place_id';
@@ -968,7 +951,7 @@ class SearchDescription
                     $sSQL .= "  AND l.type='".$this->sType."'";
                     $sSQL .= $this->oContext->excludeSQL(' AND l.place_id');
                     if ($sOrderBySQL) {
-                        $sSQL .= "ORDER BY orderterm ASC";
+                        $sSQL .= 'ORDER BY orderterm ASC';
                     }
                     $sSQL .= " limit $iLimit";
 
@@ -1023,19 +1006,19 @@ class SearchDescription
             return $aWordIDs[$k];
         };
 
-        echo "<tr>";
+        echo '<tr>';
         echo "<td>$this->iSearchRank</td>";
-        echo "<td>".join(', ', array_map($kf, $this->aName))."</td>";
-        echo "<td>".join(', ', array_map($kf, $this->aNameNonSearch))."</td>";
-        echo "<td>".join(', ', array_map($kf, $this->aAddress))."</td>";
-        echo "<td>".join(', ', array_map($kf, $this->aAddressNonSearch))."</td>";
-        echo "<td>".$this->sCountryCode."</td>";
-        echo "<td>".Operator::toString($this->iOperator)."</td>";
-        echo "<td>".$this->sClass."</td>";
-        echo "<td>".$this->sType."</td>";
-        echo "<td>".$this->sPostcode."</td>";
-        echo "<td>".$this->sHouseNumber."</td>";
-
-        echo "</tr>";
+        echo '<td>'.join(', ', array_map($kf, $this->aName)).'</td>';
+        echo '<td>'.join(', ', array_map($kf, $this->aNameNonSearch)).'</td>';
+        echo '<td>'.join(', ', array_map($kf, $this->aAddress)).'</td>';
+        echo '<td>'.join(', ', array_map($kf, $this->aAddressNonSearch)).'</td>';
+        echo '<td>'.$this->sCountryCode.'</td>';
+        echo '<td>'.Operator::toString($this->iOperator).'</td>';
+        echo '<td>'.$this->sClass.'</td>';
+        echo '<td>'.$this->sType.'</td>';
+        echo '<td>'.$this->sPostcode.'</td>';
+        echo '<td>'.$this->sHouseNumber.'</td>';
+
+        echo '</tr>';
     }
 }