- $aPlaceIDs = array();
-
- // First we need a position, either aName or fLat or both
- $aTerms = array();
- $aOrder = array();
-
- if ($aSearch['sHouseNumber'] && sizeof($aSearch['aAddress'])) {
- $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
- $aOrder[] = "";
- $aOrder[0] = " (";
- $aOrder[0] .= " EXISTS(";
- $aOrder[0] .= " SELECT place_id ";
- $aOrder[0] .= " FROM placex ";
- $aOrder[0] .= " WHERE parent_place_id = search_name.place_id";
- $aOrder[0] .= " AND transliteration(housenumber) ~* E'".$sHouseNumberRegex."' ";
- $aOrder[0] .= " LIMIT 1";
- $aOrder[0] .= " ) ";
- // also housenumbers from interpolation lines table are needed
- $aOrder[0] .= " OR EXISTS(";
- $aOrder[0] .= " SELECT place_id ";
- $aOrder[0] .= " FROM location_property_osmline ";
- $aOrder[0] .= " WHERE parent_place_id = search_name.place_id";
- $aOrder[0] .= " AND startnumber is not NULL";
- $aOrder[0] .= " AND ".intval($aSearch['sHouseNumber']).">=startnumber ";
- $aOrder[0] .= " AND ".intval($aSearch['sHouseNumber'])."<=endnumber ";
- $aOrder[0] .= " LIMIT 1";
- $aOrder[0] .= " )";
- $aOrder[0] .= " )";
- $aOrder[0] .= " 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'], ",")."]";
- if (sizeof($aSearch['aNameNonSearch'])) $aTerms[] = "array_cat(name_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aNameNonSearch'], ",")."]";
- 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
- && $aWordFrequencyScores[$aSearch['aName'][reset($aSearch['aName'])]] < CONST_Search_NameOnlySearchFrequencyThreshold
- ) {
- $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join(array_merge($aSearch['aAddress'], $aSearch['aAddressNonSearch']), ",")."]";
- } else {
- $aTerms[] = "nameaddress_vector @> ARRAY[".join($aSearch['aAddress'], ",")."]";
- if (sizeof($aSearch['aAddressNonSearch'])) {
- $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddressNonSearch'], ",")."]";
- }
- }
- }
- if ($aSearch['sCountryCode']) $aTerms[] = "country_code = '".pg_escape_string($aSearch['sCountryCode'])."'";
- if ($aSearch['sHouseNumber']) {
- $aTerms[] = "address_rank between 16 and 27";
- } elseif (!$aSearch['sClass'] || $aSearch['sOperator'] == 'name') {
- if ($this->iMinAddressRank > 0) {
- $aTerms[] = "address_rank >= ".$this->iMinAddressRank;
- }
- if ($this->iMaxAddressRank < 30) {
- $aTerms[] = "address_rank <= ".$this->iMaxAddressRank;
- }
- }
- if ($aSearch['oNear']) {
- $aTerms[] = $aSearch['oNear']->withinSQL('centroid');
-
- $aOrder[] = $aSearch['oNear']->distanceSQL('centroid');
- } elseif ($aSearch['sPostcode']) {
- if (!sizeof($aSearch['aAddress'])) {
- $aTerms[] = "EXISTS(SELECT place_id FROM location_postcode p WHERE p.postcode = '".$aSearch['sPostcode']."' AND ST_DWithin(search_name.centroid, p.geometry, 0.1))";
- } else {
- $aOrder[] = "(SELECT min(ST_Distance(search_name.centroid, p.geometry)) FROM location_postcode p WHERE p.postcode = '".$aSearch['sPostcode']."')";
- }
- }
- if (sizeof($this->aExcludePlaceIDs)) {
- $aTerms[] = "place_id not in (".join(',', $this->aExcludePlaceIDs).")";
- }
- if ($sCountryCodesSQL) {
- $aTerms[] = "country_code in ($sCountryCodesSQL)";
- }
-
- if ($bBoundingBoxSearch) $aTerms[] = "centroid && $this->sViewboxSmallSQL";
- if ($oNearPoint) {
- $aOrder[] = $oNearPoint->distanceSQL('centroid');
- }
-
- if ($aSearch['sHouseNumber']) {
- $sImportanceSQL = '- abs(26 - address_rank) + 3';
- } else {
- $sImportanceSQL = '(CASE WHEN importance = 0 OR importance IS NULL THEN 0.75-(search_rank::float/40) ELSE importance END)';
- }
- if ($this->sViewboxSmallSQL) $sImportanceSQL .= " * CASE WHEN ST_Contains($this->sViewboxSmallSQL, centroid) THEN 1 ELSE 0.5 END";
- if ($this->sViewboxLargeSQL) $sImportanceSQL .= " * CASE WHEN ST_Contains($this->sViewboxLargeSQL, centroid) THEN 1 ELSE 0.5 END";
-
- $aOrder[] = "$sImportanceSQL DESC";
- if (sizeof($aSearch['aFullNameAddress'])) {
- $sExactMatchSQL = ' ( ';
- $sExactMatchSQL .= ' SELECT count(*) FROM ( ';
- $sExactMatchSQL .= ' SELECT unnest(ARRAY['.join($aSearch['aFullNameAddress'], ",").']) ';
- $sExactMatchSQL .= ' INTERSECT ';
- $sExactMatchSQL .= ' SELECT unnest(nameaddress_vector)';
- $sExactMatchSQL .= ' ) s';
- $sExactMatchSQL .= ') as exactmatch';
- $aOrder[] = 'exactmatch DESC';
- } else {
- $sExactMatchSQL = '0::int as exactmatch';
- }
-
- if (sizeof($aTerms)) {
- $sSQL = "SELECT place_id, ";
- $sSQL .= $sExactMatchSQL;
- $sSQL .= " FROM search_name";
- $sSQL .= " WHERE ".join(' and ', $aTerms);
- $sSQL .= " ORDER BY ".join(', ', $aOrder);
- if ($aSearch['sHouseNumber'] || $aSearch['sClass']) {
- $sSQL .= " LIMIT 20";
- } elseif (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && $aSearch['sClass']) {
- $sSQL .= " LIMIT 1";
- } else {
- $sSQL .= " LIMIT ".$this->iLimit;
- }
-
- if (CONST_Debug) var_dump($sSQL);
- $aViewBoxPlaceIDs = chksql(
- $this->oDB->getAll($sSQL),
- "Could not get places for search terms."
- );
- //var_dump($aViewBoxPlaceIDs);
- // Did we have an viewbox matches?
- $aPlaceIDs = array();
- $bViewBoxMatch = false;
- foreach ($aViewBoxPlaceIDs as $aViewBoxRow) {
- //if ($bViewBoxMatch == 1 && $aViewBoxRow['in_small'] == 'f') break;
- //if ($bViewBoxMatch == 2 && $aViewBoxRow['in_large'] == 'f') break;
- //if ($aViewBoxRow['in_small'] == 't') $bViewBoxMatch = 1;
- //else if ($aViewBoxRow['in_large'] == 't') $bViewBoxMatch = 2;
- $aPlaceIDs[] = $aViewBoxRow['place_id'];
- $this->exactMatchCache[$aViewBoxRow['place_id']] = $aViewBoxRow['exactmatch'];