]> git.openstreetmap.org Git - nominatim.git/blobdiff - website/search.php
clean up some of the debug output
[nominatim.git] / website / search.php
index 265f20115105825a4388f8a8465369ac1ebcc1af..70b51e1009f97570b446b6f24e52de15d724f7a1 100755 (executable)
        }
        if (sizeof($aStructuredQuery) > 0) {
                $sQuery = join(', ', $aStructuredQuery);
-               $sAllowedTypesSQLList = '(\'place\',\'boundary\')';
+               if ($iMaxAddressRank < 30)
+               {
+                       $sAllowedTypesSQLList = '(\'place\',\'boundary\')';
+               }
        }
 
        if ($sQuery)
                                {
                                        foreach($aSearchWords as $aSearchTerm)
                                        {
-                                               $aNewSearch = $aSearch;                 
+                                               $aNewSearch = $aSearch;
                                                if ($aSearchTerm['country_code'])
                                                {
                                                        $aNewSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
                        {
 
                        // Check which tokens we have, get the ID numbers                       
-                       $sSQL = 'select word_id,word_token, word, class, type, location, country_code, operator';
+                       $sSQL = 'select word_id,word_token, word, class, type, location, country_code, operator, search_name_count';
                        $sSQL .= ' from word where word_token in ('.join(',',array_map("getDBQuoted",$aTokens)).')';
                        $sSQL .= ' and search_name_count < '.CONST_Max_Word_Frequency;
 //                     $sSQL .= ' group by word_token, word, class, type, location, country_code';
                                {
                                        $aValidTokens[$aToken['word_token']] = array($aToken);
                                }
-                               if ($aToken['word_token'][0]==' ' && !$aToken['class'] && !$aToken['country_code']) $aPossibleMainWordIDs[$aToken['word_id']] = 1;
+                               if ($aToken['word_token'][0]==' ' && !$aToken['class'] && !$aToken['country_code']) $aPossibleMainWordIDs[$aToken['word_id']] = 1 + $aToken['search_name_count'];
                        }
                        if (CONST_Debug) var_Dump($aPhrases, $aValidTokens);
 
                                                                        {
                                                                                $aSearch = $aCurrentSearch;
                                                                                $aSearch['iSearchRank']++;
-                                                                               if (($sPhraseType == '' || $sPhraseType == 'country') && $aSearchTerm['country_code'] !== null && $aSearchTerm['country_code'] != '0')
+                                                                               if (($sPhraseType == '' || $sPhraseType == 'country') && !empty($aSearchTerm['country_code']) && $aSearchTerm['country_code'] != '0')
                                                                                {
                                                                                        if ($aSearch['sCountryCode'] === false)
                                                                                        {
                                                                                                $aSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
                                                                                                // Country is almost always at the end of the string - increase score for finding it anywhere else (optimisation)
-                                                                                               if ($iToken+1 != sizeof($aWordset) || $iPhrase+1 != sizeof($aPhrases)) $aSearch['iSearchRank'] += 5;
+                                                                                               // If reverse order is enabled, it may appear at the beginning as well.
+                                                                                               if (($iToken+1 != sizeof($aWordset) || $iPhrase+1 != sizeof($aPhrases)) &&
+                                                                                                               (!$bReverseInPlan || $iToken > 0 || $iPhrase > 0))
+                                                                                               {
+                                                                                                       $aSearch['iSearchRank'] += 5;
+                                                                                               }
                                                                                                if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aSearch;
                                                                                        }
                                                                                }
                                        {
                                                $iQueryLoop++;
 
+                                               if (CONST_Debug) { echo "<hr><b>Search Loop, group $iGroupLoop, loop $iQueryLoop</b>"; }
+                                               if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);     
+
+
                                                // Must have a location term
                                                if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['fLon'])
                                                {
                                                                        $sSQL = "select place_id from placex where country_code='".$aSearch['sCountryCode']."' and rank_search = 4";
                                                                        if ($sCountryCodesSQL) $sSQL .= " and country_code in ($sCountryCodesSQL)";                                                             
                                                                        $sSQL .= " order by st_area(geometry) desc limit 1";
+                                                                       if (CONST_Debug) var_dump($sSQL);
                                                                        $aPlaceIDs = $oDB->getCol($sSQL);
                                                                }
                                                        }
                                                        {
                                                                if (!$bBoundingBoxSearch && !$aSearch['fLon']) continue;
                                                                if (!$aSearch['sClass']) continue;
-                                                               if (CONST_Debug) var_dump('<hr>',$aSearch);
-                                                               if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);     
                                                                $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
                                                                if ($oDB->getOne($sSQL))
                                                                {
                                                                if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
                                                                $sSQL .= " where st_contains($sViewboxSmallSQL, ct.centroid)";
                                                                if ($sCountryCodesSQL) $sSQL .= " and country_code in ($sCountryCodesSQL)";                                                             
+                                                               if (sizeof($aExcludePlaceIDs))
+                                                               {
+                                                                       $sSQL .= " and place_id not in (".join(',',$aExcludePlaceIDs).")";
+                                                               }
                                                                if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
                                                                $sSQL .= " limit $iLimit";
                                                                if (CONST_Debug) var_dump($sSQL);
                                                                $aPlaceIDs = $oDB->getCol($sSQL);
 
-                                                               if (!sizeof($aPlaceIDs))
+                                                               // If excluded place IDs are given, it is fair to assume that
+                                                               // there have been results in the small box, so no further
+                                                               // expansion in that case.
+                                                               if (!sizeof($aPlaceIDs) && !sizeof($aExcludePlaceIDs))
                                                                {
                                                                        $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
                                                                        if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
                                                }
                                                else
                                                {
-                                                       if (CONST_Debug) var_dump('<hr>',$aSearch);
-                                                       if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);     
                                                        $aPlaceIDs = array();
-                                               
+
                                                        // First we need a position, either aName or fLat or both
                                                        $aTerms = array();
                                                        $aOrder = array();
                                                        // 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'],",")."]";
-                                                       if (sizeof($aSearch['aAddress']) && $aSearch['aName'] != $aSearch['aAddress']) $aTerms[] = "nameaddress_vector @> ARRAY[".join($aSearch['aAddress'],",")."]";
+                                                       if (sizeof($aSearch['aAddress']) && $aSearch['aName'] != $aSearch['aAddress']) 
+                                                       {
+                                                               // For infrequent name terms disable index usage for address
+                                                               if (CONST_Search_NameOnlySearchFrequencyThreshold && 
+                                                                       sizeof($aSearch['aName']) == 1 && 
+                                                                       $aPossibleMainWordIDs[$aSearch['aName'][reset($aSearch['aName'])]] < CONST_Search_NameOnlySearchFrequencyThreshold)
+                                                               {
+                                                                       $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddress'],",")."]";
+                                                               }
+                                                               else
+                                                               {
+                                                                       $aTerms[] = "nameaddress_vector @> ARRAY[".join($aSearch['aAddress'],",")."]";
+                                                               }
+                                                       }
                                                        if ($aSearch['sCountryCode']) $aTerms[] = "country_code = '".pg_escape_string($aSearch['sCountryCode'])."'";
                                                        if ($aSearch['sHouseNumber']) $aTerms[] = "address_rank in (26,27)";
                                                        if ($aSearch['fLon'] && $aSearch['fLat'])
                                                        failInternalError("Could not get place IDs from tokens." ,$sSQL, $aPlaceIDs);
                                                }
 
-                                               if (CONST_Debug) var_Dump($aPlaceIDs);
+                                               if (CONST_Debug) { echo "<br><b>Place IDs:</b> "; var_Dump($aPlaceIDs); }
 
                                                foreach($aPlaceIDs as $iPlaceID)
                                                {
                                        if ($iGroupLoop > 4) break;
                                        if ($iQueryLoop > 30) break;
                                }
-//exit;
+
                                // Did we find anything?        
                                if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs))
                                {
                                        $sSQL .= ",get_address_by_language(place_id, $sLanguagePrefArraySQL) ";
                                        $sSQL .= "order by importance desc";
 //                                     $sSQL .= "order by rank_search,rank_address,porder asc";
-                                       if (CONST_Debug) var_dump('<hr>',$sSQL);
+                                       if (CONST_Debug) { echo "<hr>"; var_dump($sSQL); }
                                        $aSearchResults = $oDB->getAll($sSQL);
 //var_dump($sSQL,$aSearchResults);exit;
 
                                        $sSQL .= ",get_address_by_language(place_id, $sLanguagePrefArraySQL) ";
                                        $sSQL .= "order by importance desc";
 //                                     $sSQL .= "order by rank_search,rank_address,porder asc";
-                                       if (CONST_Debug) var_dump('<hr>',$sSQL);
+                                       if (CONST_Debug) { echo "<hr>", var_dump($sSQL); }
                                        $aSearchResults = $oDB->getAll($sSQL);
 //var_dump($sSQL,$aSearchResults);exit;