]> git.openstreetmap.org Git - nominatim.git/blobdiff - lib/SearchDescription.php
move complete search query code into SearchDescription
[nominatim.git] / lib / SearchDescription.php
index e6cdca632b5e45f4416ec9ec3d186f3a58021208..f3afaff2edc1d2e3ec5236161cb8eb5585345a4b 100644 (file)
@@ -59,11 +59,6 @@ class SearchDescription
         return $this->iSearchRank;
     }
 
-    public function getPostCode()
-    {
-        return $this->sPostcode;
-    }
-
     public function setPoiSearch($iOperator, $sClass, $sType)
     {
         $this->iOperator = $iOperator;
@@ -71,22 +66,6 @@ class SearchDescription
         $this->sType = $sType;
     }
 
-    public function isNamedSearch()
-    {
-        return sizeof($this->aName) > 0 || sizeof($this->aAddress) > 0;
-    }
-
-    public function isCountrySearch()
-    {
-        return $this->sCountryCode && sizeof($this->aName) == 0
-               && !$this->iOperator && !$this->oContext->hasNearPoint();
-    }
-
-    public function isPoiSearch()
-    {
-        return (bool) $this->sClass;
-    }
-
     public function looksLikeFullAddress()
     {
         return sizeof($this->aName)
@@ -94,28 +73,18 @@ class SearchDescription
                && preg_match('/[0-9]+/', $this->sHouseNumber);
     }
 
-    public function isOperator($iType)
-    {
-        return $this->iOperator == $iType;
-    }
-
-    public function hasHouseNumber()
-    {
-        return (bool) $this->sHouseNumber;
-    }
-
     private function poiTable()
     {
         return 'place_classtype_'.$this->sClass.'_'.$this->sType;
     }
 
-    public function countryCodeSQL($sVar, $sCountryList)
+    public function countryCodeSQL($sVar)
     {
         if ($this->sCountryCode) {
             return $sVar.' = \''.$this->sCountryCode."'";
         }
-        if ($sCountryList) {
-            return $sVar.' in ('.$sCountryList.')';
+        if ($this->oContext->sqlCountryList) {
+            return $sVar.' in '.$this->oContext->sqlCountryList;
         }
 
         return '';
@@ -366,8 +335,91 @@ class SearchDescription
 
     /////////// Query functions
 
+    public function query(&$oDB, &$aWordFrequencyScores, &$aExactMatchCache, $iMinRank, $iMaxRank, $iLimit)
+    {
+        $aPlaceIDs = array();
+        $iHousenumber = -1;
+
+        if ($this->sCountryCode
+            && !sizeof($this->aName)
+            && !$this->iOperator
+            && !$this->sClass
+            && !$this->oContext->hasNearPoint()
+        ) {
+            // Just looking for a country - look it up
+            if (4 >= $iMinRank && 4 <= $iMaxRank) {
+                $aPlaceIDs = $this->queryCountry($oDB);
+            }
+        } elseif (!sizeof($this->aName) && !sizeof($this->aAddress)) {
+            // Neither name nor address? Then we must be
+            // looking for a POI in a geographic area.
+            if ($this->oContext->isBoundedSearch()) {
+                $aPlaceIDs = $this->queryNearbyPoi($oDB, $iLimit);
+            }
+        } elseif ($this->iOperator == Operator::POSTCODE) {
+            // looking for postcode
+            $aPlaceIDs = $this->queryPostcode($oDB, $iLimit);
+        } else {
+            // Ordinary search:
+            // First search for places according to name and address.
+            $aNamedPlaceIDs = $this->queryNamedPlace(
+                $oDB,
+                $aWordFrequencyScores,
+                $iMinRank,
+                $iMaxRank,
+                $iLimit
+            );
+
+            if (sizeof($aNamedPlaceIDs)) {
+                foreach ($aNamedPlaceIDs as $aRow) {
+                    $aPlaceIDs[] = $aRow['place_id'];
+                    $aExactMatchCache[$aRow['place_id']] = $aRow['exactmatch'];
+                }
+            }
 
-    public function queryCountry(&$oDB)
+            //now search for housenumber, if housenumber provided
+            if ($this->sHouseNumber && sizeof($aPlaceIDs)) {
+                $aResult = $this->queryHouseNumber($oDB, $aPlaceIDs, $iLimit);
+
+                if (sizeof($aResult)) {
+                    $iHousenumber = $aResult['iHouseNumber'];
+                    $aPlaceIDs = $aResult['aPlaceIDs'];
+                } elseif (!$this->looksLikeFullAddress()) {
+                    $aPlaceIDs = array();
+                }
+            }
+
+            // finally get POIs if requested
+            if ($this->sClass && sizeof($aPlaceIDs)) {
+                $aPlaceIDs = $this->queryPoiByOperator($oDB, $aPlaceIDs, $iLimit);
+            }
+        }
+
+        if (CONST_Debug) {
+            echo "<br><b>Place IDs:</b> ";
+            var_Dump($aPlaceIDs);
+        }
+
+        if (sizeof($aPlaceIDs) && $this->sPostcode) {
+            $sSQL = 'SELECT place_id FROM placex';
+            $sSQL .= ' WHERE place_id in ('.join(',', $aPlaceIDs).')';
+            $sSQL .= " AND postcode = '".$this->sPostcode."'";
+            if (CONST_Debug) var_dump($sSQL);
+            $aFilteredPlaceIDs = chksql($oDB->getCol($sSQL));
+            if ($aFilteredPlaceIDs) {
+                $aPlaceIDs = $aFilteredPlaceIDs;
+                if (CONST_Debug) {
+                    echo "<br><b>Place IDs after postcode filtering:</b> ";
+                    var_Dump($aPlaceIDs);
+                }
+            }
+        }
+
+        return array('IDs' => $aPlaceIDs, 'houseNumber' => $iHousenumber);
+    }
+
+
+    private function queryCountry(&$oDB)
     {
         $sSQL = 'SELECT place_id FROM placex ';
         $sSQL .= "WHERE country_code='".$this->sCountryCode."'";
@@ -382,7 +434,7 @@ class SearchDescription
         return chksql($oDB->getCol($sSQL));
     }
 
-    public function queryNearbyPoi(&$oDB, $sCountryList, $sExcludeSQL, $iLimit)
+    private function queryNearbyPoi(&$oDB, $iLimit)
     {
         if (!$this->sClass) {
             return array();
@@ -393,7 +445,7 @@ class SearchDescription
         $sSQL = 'SELECT count(*) FROM pg_tables WHERE tablename = \''.$sPoiTable."'";
         if (chksql($oDB->getOne($sSQL))) {
             $sSQL = 'SELECT place_id FROM '.$sPoiTable.' ct';
-            if ($sCountryList) {
+            if ($this->oContext->sqlCountryList) {
                 $sSQL .= ' JOIN placex USING (place_id)';
             }
             if ($this->oContext->hasNearPoint()) {
@@ -401,12 +453,10 @@ class SearchDescription
             } else if ($this->oContext->bViewboxBounded) {
                 $sSQL .= ' WHERE ST_Contains('.$this->oContext->sqlViewboxSmall.', ct.centroid)';
             }
-            if ($sCountryList) {
-                $sSQL .= " AND country_code in ($sCountryList)";
-            }
-            if ($sExcludeSQL) {
-                $sSQL .= ' AND place_id not in ('.$sExcludeSQL.')';
+            if ($this->oContext->sqlCountryList) {
+                $sSQL .= ' AND country_code in '.$this->oContext->sqlCountryList;
             }
+            $sSQL .= $this->oContext->excludeSQL(' AND place_id');
             if ($this->oContext->sqlViewboxCentre) {
                 $sSQL .= ' ORDER BY ST_Distance(';
                 $sSQL .= $this->oContext->sqlViewboxCentre.', ct.centroid) ASC';
@@ -423,8 +473,8 @@ class SearchDescription
             $sSQL .= 'class=\''.$this->sClass."' and type='".$this->sType."'";
             $sSQL .= ' AND '.$this->oContext->withinSQL('geometry');
             $sSQL .= ' AND linked_place_id is null';
-            if ($sCountryList) {
-                $sSQL .= " AND country_code in ($sCountryList)";
+            if ($this->oContext->sqlCountryList) {
+                $sSQL .= ' AND country_code in '.$this->oContext->sqlCountryList;
             }
             $sSQL .= ' ORDER BY '.$this->oContext->distanceSQL('centroid')." ASC";
             $sSQL .= " LIMIT $iLimit";
@@ -435,7 +485,7 @@ class SearchDescription
         return array();
     }
 
-    public function queryPostcode(&$oDB, $sCountryList, $iLimit)
+    private function queryPostcode(&$oDB, $iLimit)
     {
         $sSQL = 'SELECT p.place_id FROM location_postcode p ';
 
@@ -449,10 +499,8 @@ class SearchDescription
         }
 
         $sSQL .= "p.postcode = '".reset($this->aName)."'";
-        $sCountryTerm = $this->countryCodeSQL('p.country_code', $sCountryList);
-        if ($sCountryTerm) {
-            $sSQL .= ' AND '.$sCountryTerm;
-        }
+        $sSQL .= $this->countryCodeSQL(' AND p.country_code');
+        $sSQL .= $this->oContext->excludeSQL(' AND p.place_id');
         $sSQL .= " LIMIT $iLimit";
 
         if (CONST_Debug) var_dump($sSQL);
@@ -460,7 +508,7 @@ class SearchDescription
         return chksql($oDB->getCol($sSQL));
     }
 
-    public function queryNamedPlace(&$oDB, $aWordFrequencyScores, $sCountryList, $iMinAddressRank, $iMaxAddressRank, $sExcludeSQL, $iLimit)
+    private function queryNamedPlace(&$oDB, $aWordFrequencyScores, $iMinAddressRank, $iMaxAddressRank, $iLimit)
     {
         $aTerms = array();
         $aOrder = array();
@@ -507,7 +555,7 @@ class SearchDescription
             }
         }
 
-        $sCountryTerm = $this->countryCodeSQL('country_code', $sCountryList);
+        $sCountryTerm = $this->countryCodeSQL('country_code');
         if ($sCountryTerm) {
             $aTerms[] = $sCountryTerm;
         }
@@ -534,8 +582,9 @@ class SearchDescription
             }
         }
 
+        $sExcludeSQL = $this->oContext->excludeSQL('place_id');
         if ($sExcludeSQL) {
-            $aTerms[] = 'place_id not in ('.$sExcludeSQL.')';
+            $aTerms[] = $sExcludeSQL;
         }
 
         if ($this->oContext->bViewboxBounded) {
@@ -589,8 +638,7 @@ class SearchDescription
         return array();
     }
 
-
-    public function queryHouseNumber(&$oDB, $aRoadPlaceIDs, $sExcludeSQL, $iLimit)
+    private function queryHouseNumber(&$oDB, $aRoadPlaceIDs, $iLimit)
     {
         $sPlaceIDs = join(',', $aRoadPlaceIDs);
 
@@ -598,9 +646,7 @@ class SearchDescription
         $sSQL = 'SELECT place_id FROM placex ';
         $sSQL .= 'WHERE parent_place_id in ('.$sPlaceIDs.')';
         $sSQL .= "  AND transliteration(housenumber) ~* E'".$sHouseNumberRegex."'";
-        if ($sExcludeSQL) {
-            $sSQL .= ' AND place_id not in ('.$sExcludeSQL.')';
-        }
+        $sSQL .= $this->oContext->excludeSQL(' AND place_id');
         $sSQL .= " LIMIT $iLimit";
 
         if (CONST_Debug) var_dump($sSQL);
@@ -629,10 +675,7 @@ class SearchDescription
             $sSQL .= " or interpolationtype='all') and ";
             $sSQL .= $iHousenumber.">=startnumber and ";
             $sSQL .= $iHousenumber."<=endnumber";
-
-            if ($sExcludeSQL) {
-                $sSQL .= ' AND place_id not in ('.$sExcludeSQL.')';
-            }
+            $sSQL .= $this->oContext->excludeSQL(' AND place_id');
             $sSQL .= " limit $iLimit";
 
             if (CONST_Debug) var_dump($sSQL);
@@ -649,9 +692,7 @@ class SearchDescription
             $sSQL = 'SELECT place_id FROM location_property_aux';
             $sSQL .= ' WHERE parent_place_id in ('.$sPlaceIDs.')';
             $sSQL .= " AND housenumber = '".$this->sHouseNumber."'";
-            if ($sExcludeSQL) {
-                $sSQL .= " AND place_id not in ($sExcludeSQL)";
-            }
+            $sSQL .= $this->oContext->excludeSQL(' AND place_id');
             $sSQL .= " limit $iLimit";
 
             if (CONST_Debug) var_dump($sSQL);
@@ -675,10 +716,7 @@ class SearchDescription
             $sSQL .= " or interpolationtype='all') and ";
             $sSQL .= $iHousenumber.">=startnumber and ";
             $sSQL .= $iHousenumber."<=endnumber";
-
-            if ($sExcludeSQL) {
-                $sSQL .= ' AND place_id not in ('.$sExcludeSQL.')';
-            }
+            $sSQL .= $this->oContext->excludeSQL(' AND place_id');
             $sSQL .= " limit $iLimit";
 
             if (CONST_Debug) var_dump($sSQL);
@@ -694,7 +732,7 @@ class SearchDescription
     }
 
 
-    public function queryPoiByOperator(&$oDB, $aParentIDs, $sExcludeSQL, $iLimit)
+    private function queryPoiByOperator(&$oDB, $aParentIDs, $iLimit)
     {
         $sPlaceIDs = join(',', $aParentIDs);
         $aClassPlaceIDs = array();
@@ -707,6 +745,7 @@ class SearchDescription
             $sSQL .= "   AND class='".$this->sClass."' ";
             $sSQL .= "   AND type='".$this->sType."'";
             $sSQL .= "   AND linked_place_id is null";
+            $sSQL .= $this->oContext->excludeSQL(' AND place_id');
             $sSQL .= " ORDER BY rank_search ASC ";
             $sSQL .= " LIMIT $iLimit";
 
@@ -783,9 +822,7 @@ class SearchDescription
                         $sSQL .= " WHERE ST_Contains('$sPlaceGeom', l.centroid)";
                     }
 
-                    if ($sExcludeSQL) {
-                        $sSQL .= ' AND l.place_id not in ('.$sExcludeSQL.')';
-                    }
+                    $sSQL .= $this->oContext->excludeSQL(' AND l.place_id');
                     $sSQL .= 'limit 300) i ';
                     if ($sOrderBySQL) {
                         $sSQL .= 'order by order_term asc';
@@ -816,9 +853,7 @@ class SearchDescription
                     $sSQL .= "  AND ST_DWithin(l.geometry, f.centroid, $fRange)";
                     $sSQL .= "  AND l.class='".$this->sClass."'";
                     $sSQL .= "  AND l.type='".$this->sType."'";
-                    if ($sExcludeSQL) {
-                        $sSQL .= " AND l.place_id not in (".$sExcludeSQL.")";
-                    }
+                    $sSQL .= $this->oContext->excludeSQL(' AND l.place_id');
                     if ($sOrderBySQL) {
                         $sSQL .= "ORDER BY orderterm ASC";
                     }