Fixes #988.
// Do we have anything that looks like a lat/lon pair?
$sQuery = $oCtx->setNearPointFromQuery($sQuery);
- $aResults = array();
if ($sQuery || $this->aStructuredQuery) {
// Start with a single blank search
$aSearches = array(new SearchDescription($oCtx));
// Start the search process
$iGroupLoop = 0;
$iQueryLoop = 0;
+ $aNextResults = array();
foreach ($aGroupedSearches as $iGroupedRank => $aSearches) {
$iGroupLoop++;
+ $aResults = $aNextResults;
foreach ($aSearches as $oSearch) {
$iQueryLoop++;
if ($iQueryLoop > 20) break;
}
+ if (!empty($aResults)) {
+ $aSplitResults = Result::splitResults($aResults);
+ Debug::printVar('Split results', $aSplitResults);
+ if ($iGroupLoop <= 4 && empty($aSplitResults['tail'])
+ && reset($aSplitResults['head'])->iResultRank > 0) {
+ // Haven't found an exact match for the query yet.
+ // Therefore add result from the next group level.
+ $aNextResults = $aSplitResults['head'];
+ foreach ($aNextResults as $oRes) {
+ $oRes->iResultRank--;
+ }
+ $aResults = array();
+ } else {
+ $aResults = $aSplitResults['head'];
+ }
+ }
+
if (!empty($aResults) && ($this->iMinAddressRank != 0 || $this->iMaxAddressRank != 30)) {
// Need to verify passes rank limits before dropping out of the loop (yuk!)
// reduces the number of place ids, like a filter
return $sHousenumbers;
}
+
+ /**
+ * Split a result array into highest ranked result and the rest
+ *
+ * @param object[] $aResults List of results to split.
+ *
+ * @return array[]
+ */
+ public static function splitResults($aResults)
+ {
+ $aHead = array();
+ $aTail = array();
+ $iMinRank = 10000;
+
+ foreach ($aResults as $oRes) {
+ if ($oRes->iResultRank < $iMinRank) {
+ $aTail = array_merge($aTail, $aHead);
+ $aHead = array($oRes->iId => $oRes);
+ $iMinRank = $oRes->iResultRank;
+ } elseif ($oRes->iResultRank == $iMinRank) {
+ $aHead[$oRes->iId] = $oRes;
+ } else {
+ $aTail[$oRes->iId] = $oRes;
+ }
+ }
+
+ return array('head' => $aHead, 'tail' => $aTail);
+ }
}
if (empty($aResults) && $this->looksLikeFullAddress()) {
$aResults = $aNamedPlaceIDs;
+ foreach ($aResults as $oRes) {
+ $oRes->iResultRank++;
+ }
}
}
if ($sPlaceIds) {
$sSQL = 'SELECT place_id FROM placex';
$sSQL .= ' WHERE place_id in ('.$sPlaceIds.')';
- $sSQL .= " AND postcode = '".$this->sPostcode."'";
+ $sSQL .= " AND postcode != '".$this->sPostcode."'";
Debug::printSQL($sSQL);
$aFilteredPlaceIDs = chksql($oDB->getCol($sSQL));
if ($aFilteredPlaceIDs) {
- $aNewResults = array();
foreach ($aFilteredPlaceIDs as $iPlaceId) {
- $aNewResults[$iPlaceId] = $aResults[$iPlaceId];
+ $aResults[$iPlaceId]->iResultRank++;
}
- $aResults = $aNewResults;
- Debug::printVar('Place IDs after postcode filtering', $aResults);
}
}
}