]> git.openstreetmap.org Git - nominatim.git/blobdiff - website/search.php
Merge branch 'master' of github.com:twain47/Nominatim
[nominatim.git] / website / search.php
index d0b183eab3c44c4722959d732e12d124d336a9ce..5ba2571bb5a221236db542b92312abd6e3d2951d 100755 (executable)
@@ -1,8 +1,11 @@
 <?php
+       @define('CONST_ConnectionBucket_PageType', 'Search');
+
        require_once(dirname(dirname(__FILE__)).'/lib/init-website.php');
        require_once(CONST_BasePath.'/lib/log.php');
 
        ini_set('memory_limit', '200M');
+
        $oDB =& getDB();
 
        // Display defaults
        $sSuggestion = $sSuggestionURL = false;
        $bDeDupe = isset($_GET['dedupe'])?(bool)$_GET['dedupe']:true;
        $bReverseInPlan = false;
-       $iLimit = isset($_GET['limit'])?(int)$_GET['limit']:10;
+       $iFinalLimit = isset($_GET['limit'])?(int)$_GET['limit']:10;
        $iOffset = isset($_GET['offset'])?(int)$_GET['offset']:0;
        $iMaxRank = 20;
-       if ($iLimit > 100) $iLimit = 100;
+       if ($iFinalLimit > 50) $iFinalLimit = 50;
+       $iLimit = $iFinalLimit + min($iFinalLimit, 10);
        $iMinAddressRank = 0;
        $iMaxAddressRank = 30;
+       $sAllowedTypesSQLList = false;
 
        // Format for output
        if (isset($_GET['format']) && ($_GET['format'] == 'html' || $_GET['format'] == 'xml' || $_GET['format'] == 'json' ||  $_GET['format'] == 'jsonv2'))
                                array('postalcode', 16, 25),
                                );
        $aStructuredQuery = array();
+       $sAllowedTypesSQLList = '';
        foreach($aStructuredOptions as $aStructuredOption)
        {
                loadStructuredAddressElement($aStructuredQuery, $iMinAddressRank, $iMaxAddressRank, $_GET, $aStructuredOption[0], $aStructuredOption[1], $aStructuredOption[2]);
        }
        if (sizeof($aStructuredQuery) > 0) {
                $sQuery = join(', ', $aStructuredQuery);
+               $sAllowedTypesSQLList = '(\'place\',\'boundary\')';
        }
 
        if ($sQuery)
                                        }
                                }
 
+                               if (CONST_Search_TryDroppedAddressTerms && sizeof($aStructuredQuery) > 0)
+                               {
+                                       $aCopyGroupedSearches = $aGroupedSearches;
+                                       foreach($aCopyGroupedSearches as $iGroup => $aSearches)
+                                       {
+                                               foreach($aSearches as $iSearch => $aSearch)
+                                               {
+                                                       $aReductionsList = array($aSearch['aAddress']);
+                                                       $iSearchRank = $aSearch['iSearchRank'];
+                                                       while(sizeof($aReductionsList) > 0)
+                                                       {
+                                                               $iSearchRank += 5;
+                                                               if ($iSearchRank > iMaxRank) break 3;
+                                                               $aNewReductionsList = array();
+                                                               foreach($aReductionsList as $aReductionsWordList)
+                                                               {
+                                                                       for ($iReductionWord = 0; $iReductionWord < sizeof($aReductionsWordList); $iReductionWord++)
+                                                                       {
+                                                                               $aReductionsWordListResult = array_merge(array_slice($aReductionsWordList, 0, $iReductionWord), array_slice($aReductionsWordList, $iReductionWord+1));
+                                                                               $aReverseSearch = $aSearch;
+                                                                               $aSearch['aAddress'] = $aReductionsWordListResult;
+                                                                               $aSearch['iSearchRank'] = $iSearchRank;
+                                                                               $aGroupedSearches[$iSearchRank][] = $aReverseSearch;
+                                                                               if (sizeof($aReductionsWordListResult) > 0)
+                                                                               {
+                                                                                       $aNewReductionsList[] = $aReductionsWordListResult;
+                                                                               }
+                                                                       }
+                                                               }
+                                                               $aReductionsList = $aNewReductionsList;
+                                                       }
+                                               }
+                                       }
+                                       ksort($aGroupedSearches);
+                               }
+
                                // Filter out duplicate searches
                                $aSearchHash = array();
                                foreach($aGroupedSearches as $iGroup => $aSearches)
                                                // Must have a location term
                                                if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['fLon'])
                                                {
-                                                       if ($aSearch['sCountryCode'] && !$aSearch['sClass'])
+                                                       if ($aSearch['sCountryCode'] && !$aSearch['sClass'] && !$aSearch['sHouseNumber'])
                                                        {
                                                                if (4 >= $iMinAddressRank && 4 <= $iMaxAddressRank)
                                                                {
                                                        if ($bBoundingBoxSearch) $aTerms[] = "centroid && $sViewboxSmallSQL";
                                                        if ($sNearPointSQL) $aOrder[] = "ST_Distance($sNearPointSQL, centroid) asc";
 
-                                                       $sImportanceSQL = 'case when importance = 0 OR importance IS NULL then 0.92-(search_rank::float/33) else importance end';
+                                                       $sImportanceSQL = 'case when importance = 0 OR importance IS NULL then 0.75-(search_rank::float/40) else importance end';
 
                                                        if ($sViewboxSmallSQL) $sImportanceSQL .= " * case when ST_Contains($sViewboxSmallSQL, centroid) THEN 1 ELSE 0.5 END";
                                                        if ($sViewboxLargeSQL) $sImportanceSQL .= " * case when ST_Contains($sViewboxLargeSQL, centroid) THEN 1 ELSE 0.5 END";
                                                                else
                                                                        $sSQL .= " limit ".$iLimit;
 
-                                                               if (CONST_Debug) var_dump($sSQL);
+                                                               if (CONST_Debug) { var_dump($sSQL); }
                                                                $aViewBoxPlaceIDs = $oDB->getAll($sSQL);
                                                                if (PEAR::IsError($aViewBoxPlaceIDs))
                                                                {
                                                        {
                                                                $sPlaceIDs = join(',',$aPlaceIDs);
 
-                                                               $aResultPlaceIDs = array();
+                                                               $aClassPlaceIDs = array();
 
                                                                if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'name')
                                                                {
                                                                        if ($sCountryCodesSQL) $sSQL .= " and country_code in ($sCountryCodesSQL)";                                                             
                                                                        $sSQL .= " order by rank_search asc limit $iLimit";
                                                                        if (CONST_Debug) var_dump($sSQL);
-                                                                       $aResultPlaceIDs = $oDB->getCol($sSQL);
+                                                                       $aClassPlaceIDs = $oDB->getCol($sSQL);
                                                                }
                                                                
                                                                if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'near') // & in
                                                                        if ($iMaxRank < 9 && $bCacheTable)
                                                                        {
                                                                                // Try and get a polygon to search in instead
-       $sSQL = "select geometry from placex where place_id in ($sPlaceIDs) and rank_search < $iMaxRank + 5 and st_geometrytype(geometry) in ('ST_Polygon','ST_MultiPolygon') order by rank_search asc limit 1";
-       if (CONST_Debug) var_dump($sSQL);
-       $sPlaceGeom = $oDB->getOne($sSQL);
+                                                                               $sSQL = "select geometry from placex where place_id in ($sPlaceIDs) and rank_search < $iMaxRank + 5 and st_geometrytype(geometry) in ('ST_Polygon','ST_MultiPolygon') order by rank_search asc limit 1";
+                                                                               if (CONST_Debug) var_dump($sSQL);
+                                                                               $sPlaceGeom = $oDB->getOne($sSQL);
                                                                        }
                                                                        
                                                                        if ($sPlaceGeom)
                                                                                if ($iOffset) $sSQL .= " offset $iOffset";
                                                                                $sSQL .= " limit $iLimit";
                                                                                if (CONST_Debug) var_dump($sSQL);
-                                                                               $aResultPlaceIDs = array_merge($aResultPlaceIDs, $oDB->getCol($sSQL));
+                                                                               $aClassPlaceIDs = array_merge($aClassPlaceIDs, $oDB->getCol($sSQL));
                                                                        }
                                                                        else
                                                                        {
                                                                                if ($iOffset) $sSQL .= " offset $iOffset";
                                                                                $sSQL .= " limit $iLimit";
                                                                                if (CONST_Debug) var_dump($sSQL);
-                                                                               $aResultPlaceIDs = array_merge($aResultPlaceIDs, $oDB->getCol($sSQL));
+                                                                               $aClassPlaceIDs = array_merge($aClassPlaceIDs, $oDB->getCol($sSQL));
                                                                        }
                                                                        }
                                                                }
 
-                                                               $aPlaceIDs = $aResultPlaceIDs;
+                                                               $aPlaceIDs = $aClassPlaceIDs;
 
                                                        }
                                                
                                                }
                                                if ($iQueryLoop > 20) break;
                                        }
+
                                        //exit;
                                        if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs)) break;
                                        if ($iGroupLoop > 4) break;
                                        $sSQL .= "get_name_by_language(name, ARRAY['ref']) as ref,";
                                        $sSQL .= "avg(ST_X(ST_Centroid(geometry))) as lon,avg(ST_Y(ST_Centroid(geometry))) as lat, ";
 //                                     $sSQL .= $sOrderSQL." as porder, ";
-                                       $sSQL .= "coalesce(importance,0.9-(rank_search::float/30)) as importance ";
+                                       $sSQL .= "coalesce(importance,0.75-(rank_search::float/40)) as importance ";
                                        $sSQL .= "from placex where place_id in ($sPlaceIDs) ";
                                        $sSQL .= "and placex.rank_address between $iMinAddressRank and $iMaxAddressRank ";
+                                       if ($sAllowedTypesSQLList) $sSQL .= "and placex.class in $sAllowedTypesSQLList ";
                                        $sSQL .= "and linked_place_id is null ";
                                        $sSQL .= "group by osm_type,osm_id,class,type,admin_level,rank_search,rank_address,country_code,importance";
                                        if (!$bDeDupe) $sSQL .= ",place_id";
                                        $sSQL .= "get_name_by_language(name, ARRAY['ref']) as ref,";
                                        $sSQL .= "avg(ST_X(ST_Centroid(geometry))) as lon,avg(ST_Y(ST_Centroid(geometry))) as lat, ";
 //                                     $sSQL .= $sOrderSQL." as porder, ";
-                                       $sSQL .= "coalesce(importance,0.9-(rank_search::float/30)) as importance ";
+                                       $sSQL .= "coalesce(importance,0.75-(rank_search::float/40)) as importance ";
                                        $sSQL .= "from placex where place_id in ($sPlaceIDs) ";
                                        $sSQL .= "and placex.rank_address between $iMinAddressRank and $iMaxAddressRank ";
                                        $sSQL .= "group by osm_type,osm_id,class,type,admin_level,rank_search,rank_address,country_code,importance";
        }
        uasort($aSearchResults, 'byImportance');
 
-//var_dump($aSearchResults);exit;
-       
        $aOSMIDDone = array();
        $aClassTypeNameDone = array();
        $aToFilter = $aSearchResults;
                }
 
                // Absolute limit on number of results
-               if (sizeof($aSearchResults) >= $iLimit) break;
+               if (sizeof($aSearchResults) >= $iFinalLimit) break;
        }
 
        $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '1 day'::interval,'YYYY/MM/DD') from import_status limit 1");
                logEnd($oDB, $hLog, sizeof($aToFilter));
        }
        $sMoreURL = CONST_Website_BaseURL.'search?format='.urlencode($sOutputFormat).'&exclude_place_ids='.join(',',$aExcludePlaceIDs);
-       $sMoreURL .= '&accept-language='.$_SERVER["HTTP_ACCEPT_LANGUAGE"];
+       if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) $sMoreURL .= '&accept-language='.$_SERVER["HTTP_ACCEPT_LANGUAGE"];
        if ($bShowPolygons) $sMoreURL .= '&polygon=1';
        if ($bShowAddressDetails) $sMoreURL .= '&addressdetails=1';
        if (isset($_GET['viewbox']) && $_GET['viewbox']) $sMoreURL .= '&viewbox='.urlencode($_GET['viewbox']);