From aa8c6e5f135364779568b465a521b44e36058dcf Mon Sep 17 00:00:00 2001 From: Brian Quinion Date: Tue, 11 Jun 2013 14:56:50 +0100 Subject: [PATCH] Try alternative orderings of structured queries that include postal codes --- lib/lib.php | 3 ++- website/search.php | 31 ++++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/lib.php b/lib/lib.php index 1517b317..3b16b399 100644 --- a/lib/lib.php +++ b/lib/lib.php @@ -884,7 +884,7 @@ return $iPlaceID; } - function loadStructuredAddressElement(&$aStructuredQuery, &$iMinAddressRank, &$iMaxAddressRank, $aParams, $sKey, $iNewMinAddressRank, $iNewMaxAddressRank) + function loadStructuredAddressElement(&$aStructuredQuery, &$iMinAddressRank, &$iMaxAddressRank, &$aAddressRankList, $aParams, $sKey, $iNewMinAddressRank, $iNewMaxAddressRank, $aItemListValues) { if (!isset($_GET[$sKey])) return false; $sValue = trim($_GET[$sKey]); @@ -895,5 +895,6 @@ $iMinAddressRank = $iNewMinAddressRank; $iMaxAddressRank = $iNewMaxAddressRank; } + if ($aItemListValues) $aAddressRankList = array_merge($aAddressRankList, $aItemListValues); return true; } diff --git a/website/search.php b/website/search.php index 3eb50aa2..76daa98c 100755 --- a/website/search.php +++ b/website/search.php @@ -26,6 +26,7 @@ $iLimit = $iFinalLimit + min($iFinalLimit, 10); $iMinAddressRank = 0; $iMaxAddressRank = 30; + $aAddressRankList = array(); $sAllowedTypesSQLList = false; // Format for output @@ -140,19 +141,19 @@ // Structured query? $aStructuredOptions = array( - array('amenity', 26, 30), - array('street', 26, 30), - array('city', 14, 24), - array('county', 9, 13), - array('state', 8, 8), - array('country', 4, 4), - array('postalcode', 5, 11), + array('amenity', 26, 30, false), + array('street', 26, 30, false), + array('city', 14, 24, false), + array('postalcode', 5, 11, array(5, 11)), + array('county', 9, 13, false), + array('state', 8, 8, false), + array('country', 4, 4, false), ); $aStructuredQuery = array(); $sAllowedTypesSQLList = ''; foreach($aStructuredOptions as $aStructuredOption) { - loadStructuredAddressElement($aStructuredQuery, $iMinAddressRank, $iMaxAddressRank, $_GET, $aStructuredOption[0], $aStructuredOption[1], $aStructuredOption[2]); + loadStructuredAddressElement($aStructuredQuery, $iMinAddressRank, $iMaxAddressRank, $aAddressRankList, $_GET, $aStructuredOption[0], $aStructuredOption[1], $aStructuredOption[2], $aStructuredOption[3]); } if (sizeof($aStructuredQuery) > 0) { @@ -356,7 +357,6 @@ $bStructuredPhrases = false; } - // Convert each phrase to standard form // Create a list of standard words // Get all 'sets' of words @@ -530,6 +530,17 @@ if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aSearch; } } + elseif ($sPhraseType == 'postalcode') + { + // We need to try the case where the postal code is the primary element (i.e. no way to tell if it is (postalcode, city) OR (city, postalcode) so try both + if (sizeof($aSearch['aName'])) + { + $aSearch['aAddress'] = array_merge($aSearch['aAddress'], $aSearch['aName']); + $aSearch['aName'] = array(); + $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id']; + } + if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aSearch; + } elseif (($sPhraseType == '' || $sPhraseType == 'street') && $aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'house') { if ($aSearch['sHouseNumber'] === '') @@ -1167,6 +1178,7 @@ $sSQL .= "from placex where place_id in ($sPlaceIDs) "; $sSQL .= "and (placex.rank_address between $iMinAddressRank and $iMaxAddressRank "; if (14 >= $iMinAddressRank && 14 <= $iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'"; + if ($aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$aAddressRankList).")"; $sSQL .= ") "; if ($sAllowedTypesSQLList) $sSQL .= "and placex.class in $sAllowedTypesSQLList "; $sSQL .= "and linked_place_id is null "; @@ -1247,6 +1259,7 @@ $sSQL .= "from placex where place_id in ($sPlaceIDs) "; $sSQL .= "and (placex.rank_address between $iMinAddressRank and $iMaxAddressRank "; if (14 >= $iMinAddressRank && 14 <= $iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'"; + if ($aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$aAddressRankList).")"; $sSQL .= ") "; $sSQL .= "group by osm_type,osm_id,class,type,admin_level,rank_search,rank_address,calculated_country_code,importance"; if (!$bDeDupe) $sSQL .= ",place_id";