]> git.openstreetmap.org Git - nominatim.git/blobdiff - lib/Geocode.php
order search results by existance of housenumbers
[nominatim.git] / lib / Geocode.php
index d96b6cb8e7c29dd801456bbd2187baf32abaa072..2c736475be050fbe991c0bb8eb55b1a954d99c24 100644 (file)
                                                                                        // sanity check: if the housenumber is not mainly made
                                                                                        // up of numbers, add a penalty
                                                                                        if (preg_match_all("/[^0-9]/", $sToken, $aMatches) > 2) $aSearch['iSearchRank']++;
                                                                                        // sanity check: if the housenumber is not mainly made
                                                                                        // up of numbers, add a penalty
                                                                                        if (preg_match_all("/[^0-9]/", $sToken, $aMatches) > 2) $aSearch['iSearchRank']++;
+                                                                                       // also housenumbers should appear in the first or second phrase
+                                                                                       if ($iPhrase > 1) $aSearch['iSearchRank'] += 1;
                                                                                        if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
                                                                                        /*
                                                                                        // Fall back to not searching for this item (better than nothing)
                                                                                        if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
                                                                                        /*
                                                                                        // Fall back to not searching for this item (better than nothing)
                                                                        }
                                                                }
                                                        }
                                                                        }
                                                                }
                                                        }
-                                                       if (isset($aValidTokens[$sToken]))
+                                                       // Look for partial matches.
+                                                       // Note that there is no point in adding country terms here
+                                                       // because country are omitted in the address.
+                                                       if (isset($aValidTokens[$sToken]) && $sPhraseType != 'country')
                                                        {
                                                                // Allow searching for a word - but at extra cost
                                                                foreach($aValidTokens[$sToken] as $aSearchTerm)
                                                        {
                                                                // Allow searching for a word - but at extra cost
                                                                foreach($aValidTokens[$sToken] as $aSearchTerm)
                                                if (sizeof($aPhrases) > 1)
                                                {
                                                        $aFinalPhrase = end($aPhrases);
                                                if (sizeof($aPhrases) > 1)
                                                {
                                                        $aFinalPhrase = end($aPhrases);
-                                                       $aFinalPhrase['wordsets'] = getInverseWordSets($aFinalPhrase['words'], 0);
+                                                       $aPhrases[sizeof($aPhrases)-1]['wordsets'] = getInverseWordSets($aFinalPhrase['words'], 0);
                                                }
                                                $aReverseGroupedSearches = $this->getGroupedSearches($aSearches, null, $aPhrases, $aValidTokens, $aWordFrequencyScores, false);
 
                                                }
                                                $aReverseGroupedSearches = $this->getGroupedSearches($aSearches, null, $aPhrases, $aValidTokens, $aWordFrequencyScores, false);
 
                                                                        $sSQL = "select place_id from placex where calculated_country_code='".$aSearch['sCountryCode']."' and rank_search = 4";
                                                                        if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
                                     if ($bBoundingBoxSearch)
                                                                        $sSQL = "select place_id from placex where calculated_country_code='".$aSearch['sCountryCode']."' and rank_search = 4";
                                                                        if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
                                     if ($bBoundingBoxSearch)
-                                        $sSQL .= " and st_overlaps($this->sViewboxSmallSQL, geometry)";
+                                        $sSQL .= " and _st_intersects($this->sViewboxSmallSQL, geometry)";
                                                                        $sSQL .= " order by st_area(geometry) desc limit 1";
                                                                        if (CONST_Debug) var_dump($sSQL);
                                                                        $aPlaceIDs = $this->oDB->getCol($sSQL);
                                                                        $sSQL .= " order by st_area(geometry) desc limit 1";
                                                                        if (CONST_Debug) var_dump($sSQL);
                                                                        $aPlaceIDs = $this->oDB->getCol($sSQL);
                                                        $aTerms = array();
                                                        $aOrder = array();
 
                                                        $aTerms = array();
                                                        $aOrder = array();
 
+                                                       if ($aSearch['sHouseNumber'])
+                                                       {
+                                                               $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
+                                                               $aOrder[] = "exists(select place_id from placex where parent_place_id = search_name.place_id and transliteration(housenumber) ~* E'".$sHouseNumberRegex."' limit 1) desc";
+                                                       }
+
                                                        // TODO: filter out the pointless search terms (2 letter name tokens and less)
                                                        // they might be right - but they are just too darned expensive to run
                                                        if (sizeof($aSearch['aName'])) $aTerms[] = "name_vector @> ARRAY[".join($aSearch['aName'],",")."]";
                                                        // TODO: filter out the pointless search terms (2 letter name tokens and less)
                                                        // they might be right - but they are just too darned expensive to run
                                                        if (sizeof($aSearch['aName'])) $aTerms[] = "name_vector @> ARRAY[".join($aSearch['aName'],",")."]";
                                                                $sSQL .= " where ".join(' and ',$aTerms);
                                                                $sSQL .= " order by ".join(', ',$aOrder);
                                                                if ($aSearch['sHouseNumber'] || $aSearch['sClass'])
                                                                $sSQL .= " where ".join(' and ',$aTerms);
                                                                $sSQL .= " order by ".join(', ',$aOrder);
                                                                if ($aSearch['sHouseNumber'] || $aSearch['sClass'])
-                                                                       $sSQL .= " limit 50";
+                                                                       $sSQL .= " limit 20";
                                                                elseif (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && $aSearch['sClass'])
                                                                        $sSQL .= " limit 1";
                                                                else
                                                                elseif (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && $aSearch['sClass'])
                                                                        $sSQL .= " limit 1";
                                                                else
                        $bFirst = true;
                        foreach($aToFilter as $iResNum => $aResult)
                        {
                        $bFirst = true;
                        foreach($aToFilter as $iResNum => $aResult)
                        {
-                               if ($aResult['type'] == 'adminitrative') $aResult['type'] = 'administrative';
                                $this->aExcludePlaceIDs[$aResult['place_id']] = $aResult['place_id'];
                                if ($bFirst)
                                {
                                $this->aExcludePlaceIDs[$aResult['place_id']] = $aResult['place_id'];
                                if ($bFirst)
                                {