5 require_once(CONST_LibDir.'/SpecialSearchOperator.php');
6 require_once(CONST_LibDir.'/SearchContext.php');
7 require_once(CONST_LibDir.'/Result.php');
10 * Description of a single interpretation of a search query.
12 class SearchDescription
14 /// Ranking how well the description fits the query.
15 private $iSearchRank = 0;
16 /// Country code of country the result must belong to.
17 private $sCountryCode = '';
18 /// List of word ids making up the name of the object.
19 private $aName = array();
20 /// True if the name is rare enough to force index use on name.
21 private $bRareName = false;
22 /// List of word ids making up the address of the object.
23 private $aAddress = array();
24 /// List of word ids that appear in the name but should be ignored.
25 private $aNameNonSearch = array();
26 /// List of word ids that appear in the address but should be ignored.
27 private $aAddressNonSearch = array();
28 /// Kind of search for special searches, see Nominatim::Operator.
29 private $iOperator = Operator::NONE;
30 /// Class of special feature to search for.
32 /// Type of special feature to search for.
34 /// Housenumber of the object.
35 private $sHouseNumber = '';
36 /// Postcode for the object.
37 private $sPostcode = '';
38 /// Global search constraints.
41 // Temporary values used while creating the search description.
43 /// Index of phrase currently processed.
44 private $iNamePhrase = -1;
47 * Create an empty search description.
49 * @param object $oContext Global context to use. Will be inherited by
50 * all derived search objects.
52 public function __construct($oContext)
54 $this->oContext = $oContext;
58 * Get current search rank.
60 * The higher the search rank the lower the likelihood that the
61 * search is a correct interpretation of the search query.
63 * @return integer Search rank.
65 public function getRank()
67 return $this->iSearchRank;
71 * Make this search a POI search.
73 * In a POI search, objects are not (only) searched by their name
74 * but also by the primary OSM key/value pair (class and type in Nominatim).
76 * @param integer $iOperator Type of POI search
77 * @param string $sClass Class (or OSM tag key) of POI.
78 * @param string $sType Type (or OSM tag value) of POI.
82 public function setPoiSearch($iOperator, $sClass, $sType)
84 $this->iOperator = $iOperator;
85 $this->sClass = $sClass;
86 $this->sType = $sType;
90 * Check if any operator is set.
92 * @return bool True, if this is a special search operation.
94 public function hasOperator()
96 return $this->iOperator != Operator::NONE;
100 * Extract key/value pairs from a query.
102 * Key/value pairs are recognised if they are of the form [<key>=<value>].
103 * If multiple terms of this kind are found then all terms are removed
104 * but only the first is used for search.
106 * @param string $sQuery Original query string.
108 * @return string The query string with the special search patterns removed.
110 public function extractKeyValuePairs($sQuery)
112 // Search for terms of kind [<key>=<value>].
114 '/\\[([\\w_]*)=([\\w_]*)\\]/',
120 foreach ($aSpecialTermsRaw as $aTerm) {
121 $sQuery = str_replace($aTerm[0], ' ', $sQuery);
122 if (!$this->hasOperator()) {
123 $this->setPoiSearch(Operator::TYPE, $aTerm[1], $aTerm[2]);
131 * Check if the combination of parameters is sensible.
133 * @return bool True, if the search looks valid.
135 public function isValidSearch()
137 if (empty($this->aName)) {
138 if ($this->sHouseNumber) {
141 if (!$this->sClass && !$this->sCountryCode) {
149 /////////// Search building functions
153 * Derive new searches by adding a full term to the existing search.
155 * @param string $sToken Term for the token.
156 * @param object $oSearchTerm Description of the token.
157 * @param object $oPosition Description of the token position within
160 * @return SearchDescription[] List of derived search descriptions.
162 public function extendWithSearchTerm($sToken, $oSearchTerm, $oPosition)
164 $aNewSearches = array();
166 if ($oPosition->maybePhrase('country')
167 && is_a($oSearchTerm, '\Nominatim\Token\Country')
169 if (!$this->sCountryCode) {
170 $oSearch = clone $this;
171 $oSearch->iSearchRank++;
172 $oSearch->sCountryCode = $oSearchTerm->sCountryCode;
173 // Country is almost always at the end of the string
174 // - increase score for finding it anywhere else (optimisation)
175 if (!$oPosition->isLastToken()) {
176 $oSearch->iSearchRank += 5;
177 $oSearch->iNamePhrase = -1;
179 $aNewSearches[] = $oSearch;
181 } elseif ($oPosition->maybePhrase('postalcode')
182 && is_a($oSearchTerm, '\Nominatim\Token\Postcode')
184 if (!$this->sPostcode) {
185 // If we have structured search or this is the first term,
186 // make the postcode the primary search element.
187 if ($this->iOperator == Operator::NONE && $oPosition->isFirstToken()) {
188 $oSearch = clone $this;
189 $oSearch->iSearchRank++;
190 $oSearch->iOperator = Operator::POSTCODE;
191 $oSearch->aAddress = array_merge($this->aAddress, $this->aName);
193 array($oSearchTerm->iId => $oSearchTerm->sPostcode);
194 $aNewSearches[] = $oSearch;
197 // If we have a structured search or this is not the first term,
198 // add the postcode as an addendum.
199 if ($this->iOperator != Operator::POSTCODE
200 && ($oPosition->isPhrase('postalcode') || !empty($this->aName))
202 $oSearch = clone $this;
203 $oSearch->iSearchRank++;
204 $oSearch->iNamePhrase = -1;
205 if (strlen($oSearchTerm->sPostcode) < 4) {
206 $oSearch->iSearchRank += 4 - strlen($oSearchTerm->sPostcode);
208 $oSearch->sPostcode = $oSearchTerm->sPostcode;
209 $aNewSearches[] = $oSearch;
212 } elseif ($oPosition->maybePhrase('street')
213 && is_a($oSearchTerm, '\Nominatim\Token\HouseNumber')
215 if (!$this->sHouseNumber && $this->iOperator != Operator::POSTCODE) {
216 // sanity check: if the housenumber is not mainly made
217 // up of numbers, add a penalty
219 if (preg_match('/\\d/', $oSearchTerm->sToken) === 0
220 || preg_match_all('/[^0-9]/', $oSearchTerm->sToken, $aMatches) > 2) {
223 if ($this->iOperator != Operator::NONE) {
226 if (empty($oSearchTerm->iId)) {
229 // also must not appear in the middle of the address
230 if (!empty($this->aAddress)
231 || (!empty($this->aAddressNonSearch))
237 $oSearch = clone $this;
238 $oSearch->iSearchRank += $iSearchCost;
239 $oSearch->iNamePhrase = -1;
240 $oSearch->sHouseNumber = $oSearchTerm->sToken;
241 $aNewSearches[] = $oSearch;
243 // Housenumbers may appear in the name when the place has its own
245 if ($oSearchTerm->iId !== null
246 && ($this->iNamePhrase >= 0 || empty($this->aName))
247 && empty($this->aAddress)
249 $oSearch = clone $this;
250 $oSearch->iSearchRank += $iSearchCost;
251 $oSearch->aAddress = $this->aName;
252 $oSearch->bRareName = false;
253 $oSearch->aName = array($oSearchTerm->iId => $oSearchTerm->iId);
254 $aNewSearches[] = $oSearch;
257 } elseif ($oPosition->isPhrase('')
258 && is_a($oSearchTerm, '\Nominatim\Token\SpecialTerm')
260 if ($this->iOperator == Operator::NONE) {
261 $oSearch = clone $this;
262 $oSearch->iSearchRank += 2;
263 $oSearch->iNamePhrase = -1;
265 $iOp = $oSearchTerm->iOperator;
266 if ($iOp == Operator::NONE) {
267 if (!empty($this->aName) || $this->oContext->isBoundedSearch()) {
268 $iOp = Operator::NAME;
270 $iOp = Operator::NEAR;
272 $oSearch->iSearchRank += 2;
273 } elseif (!$oPosition->isFirstToken() && !$oPosition->isLastToken()) {
274 $oSearch->iSearchRank += 2;
276 if ($this->sHouseNumber) {
277 $oSearch->iSearchRank++;
280 $oSearch->setPoiSearch(
282 $oSearchTerm->sClass,
285 $aNewSearches[] = $oSearch;
287 } elseif (!$oPosition->isPhrase('country')
288 && is_a($oSearchTerm, '\Nominatim\Token\Word')
290 $iWordID = $oSearchTerm->iId;
291 // Full words can only be a name if they appear at the beginning
292 // of the phrase. In structured search the name must forcably in
293 // the first phrase. In unstructured search it may be in a later
294 // phrase when the first phrase is a house number.
295 if (!empty($this->aName) || !($oPosition->isFirstPhrase() || $oPosition->isPhrase(''))) {
296 if (($oPosition->isPhrase('') || !$oPosition->isFirstPhrase())
297 && $oSearchTerm->iTermCount > 1
299 $oSearch = clone $this;
300 $oSearch->iNamePhrase = -1;
301 $oSearch->iSearchRank += 1;
302 $oSearch->aAddress[$iWordID] = $iWordID;
303 $aNewSearches[] = $oSearch;
305 } elseif (empty($this->aNameNonSearch)) {
306 $oSearch = clone $this;
307 $oSearch->iSearchRank++;
308 $oSearch->aName = array($iWordID => $iWordID);
309 if (CONST_Search_NameOnlySearchFrequencyThreshold) {
310 $oSearch->bRareName =
311 $oSearchTerm->iSearchNameCount
312 < CONST_Search_NameOnlySearchFrequencyThreshold;
314 $aNewSearches[] = $oSearch;
316 } elseif (!$oPosition->isPhrase('country')
317 && is_a($oSearchTerm, '\Nominatim\Token\Partial')
318 && strpos($sToken, ' ') === false
320 $aNewSearches = $this->extendWithPartialTerm(
327 return $aNewSearches;
331 * Derive new searches by adding a partial term to the existing search.
333 * @param string $sToken Term for the token.
334 * @param object $oSearchTerm Description of the token.
335 * @param object $oPosition Description of the token position within
338 * @return SearchDescription[] List of derived search descriptions.
340 private function extendWithPartialTerm($sToken, $oSearchTerm, $oPosition)
342 $aNewSearches = array();
343 $iWordID = $oSearchTerm->iId;
345 if (($oPosition->isPhrase('') || !$oPosition->isFirstPhrase())
346 && (!empty($this->aName))
348 $oSearch = clone $this;
349 $oSearch->iSearchRank++;
350 if (preg_match('#^[0-9 ]+$#', $sToken)) {
351 $oSearch->iSearchRank++;
353 if ($oSearchTerm->iSearchNameCount < CONST_Max_Word_Frequency) {
354 $oSearch->aAddress[$iWordID] = $iWordID;
356 $oSearch->aAddressNonSearch[$iWordID] = $iWordID;
358 $aNewSearches[] = $oSearch;
361 if ((!$this->sPostcode && !$this->aAddress && !$this->aAddressNonSearch)
362 && ((empty($this->aName) && empty($this->aNameNonSearch))
363 || $this->iNamePhrase == $oPosition->getPhrase())
365 $oSearch = clone $this;
366 $oSearch->iSearchRank++;
367 if (empty($this->aName) && empty($this->aNameNonSearch)) {
368 $oSearch->iSearchRank++;
370 if (preg_match('#^[0-9 ]+$#', $sToken)) {
371 $oSearch->iSearchRank++;
373 if ($oSearchTerm->iSearchNameCount < CONST_Max_Word_Frequency) {
374 if (empty($this->aName)
375 && CONST_Search_NameOnlySearchFrequencyThreshold
377 $oSearch->bRareName =
378 $oSearchTerm->iSearchNameCount
379 < CONST_Search_NameOnlySearchFrequencyThreshold;
381 $oSearch->bRareName = false;
383 $oSearch->aName[$iWordID] = $iWordID;
385 $oSearch->aNameNonSearch[$iWordID] = $iWordID;
387 $oSearch->iNamePhrase = $oPosition->getPhrase();
388 $aNewSearches[] = $oSearch;
391 return $aNewSearches;
394 /////////// Query functions
398 * Query database for places that match this search.
400 * @param object $oDB Nominatim::DB instance to use.
401 * @param integer $iMinRank Minimum address rank to restrict search to.
402 * @param integer $iMaxRank Maximum address rank to restrict search to.
403 * @param integer $iLimit Maximum number of results.
405 * @return mixed[] An array with two fields: IDs contains the list of
406 * matching place IDs and houseNumber the houseNumber
407 * if appicable or -1 if not.
409 public function query(&$oDB, $iMinRank, $iMaxRank, $iLimit)
413 if ($this->sCountryCode
414 && empty($this->aName)
417 && !$this->oContext->hasNearPoint()
419 // Just looking for a country - look it up
420 if (4 >= $iMinRank && 4 <= $iMaxRank) {
421 $aResults = $this->queryCountry($oDB);
423 } elseif (empty($this->aName) && empty($this->aAddress)) {
424 // Neither name nor address? Then we must be
425 // looking for a POI in a geographic area.
426 if ($this->oContext->isBoundedSearch()) {
427 $aResults = $this->queryNearbyPoi($oDB, $iLimit);
429 } elseif ($this->iOperator == Operator::POSTCODE) {
430 // looking for postcode
431 $aResults = $this->queryPostcode($oDB, $iLimit);
434 // First search for places according to name and address.
435 $aResults = $this->queryNamedPlace(
442 // Now search for housenumber, if housenumber provided. Can be zero.
443 if (($this->sHouseNumber || $this->sHouseNumber === '0') && !empty($aResults)) {
444 $aHnResults = $this->queryHouseNumber($oDB, $aResults);
446 // Downgrade the rank of the street results, they are missing
447 // the housenumber. Also drop POI places (rank 30) here, they
448 // cannot be a parent place and therefore must not be shown
449 // as a result for a search with a missing housenumber.
450 foreach ($aResults as $oRes) {
451 if ($oRes->iAddressRank < 28) {
452 if ($oRes->iAddressRank >= 26) {
453 $oRes->iResultRank++;
455 $oRes->iResultRank += 2;
457 $aHnResults[$oRes->iId] = $oRes;
461 $aResults = $aHnResults;
464 // finally get POIs if requested
465 if ($this->sClass && !empty($aResults)) {
466 $aResults = $this->queryPoiByOperator($oDB, $aResults, $iLimit);
470 Debug::printDebugTable('Place IDs', $aResults);
472 if (!empty($aResults) && $this->sPostcode) {
473 $sPlaceIds = Result::joinIdsByTable($aResults, Result::TABLE_PLACEX);
475 $sSQL = 'SELECT place_id FROM placex';
476 $sSQL .= ' WHERE place_id in ('.$sPlaceIds.')';
477 $sSQL .= " AND postcode != '".$this->sPostcode."'";
478 Debug::printSQL($sSQL);
479 $aFilteredPlaceIDs = $oDB->getCol($sSQL);
480 if ($aFilteredPlaceIDs) {
481 foreach ($aFilteredPlaceIDs as $iPlaceId) {
482 $aResults[$iPlaceId]->iResultRank++;
492 private function queryCountry(&$oDB)
494 $sSQL = 'SELECT place_id FROM placex ';
495 $sSQL .= "WHERE country_code='".$this->sCountryCode."'";
496 $sSQL .= ' AND rank_search = 4';
497 if ($this->oContext->bViewboxBounded) {
498 $sSQL .= ' AND ST_Intersects('.$this->oContext->sqlViewboxSmall.', geometry)';
500 $sSQL .= ' ORDER BY st_area(geometry) DESC LIMIT 1';
502 Debug::printSQL($sSQL);
504 $iPlaceId = $oDB->getOne($sSQL);
508 $aResults[$iPlaceId] = new Result($iPlaceId);
514 private function queryNearbyPoi(&$oDB, $iLimit)
516 if (!$this->sClass) {
520 $aDBResults = array();
521 $sPoiTable = $this->poiTable();
523 if ($oDB->tableExists($sPoiTable)) {
524 $sSQL = 'SELECT place_id FROM '.$sPoiTable.' ct';
525 if ($this->oContext->sqlCountryList) {
526 $sSQL .= ' JOIN placex USING (place_id)';
528 if ($this->oContext->hasNearPoint()) {
529 $sSQL .= ' WHERE '.$this->oContext->withinSQL('ct.centroid');
530 } elseif ($this->oContext->bViewboxBounded) {
531 $sSQL .= ' WHERE ST_Contains('.$this->oContext->sqlViewboxSmall.', ct.centroid)';
533 if ($this->oContext->sqlCountryList) {
534 $sSQL .= ' AND country_code in '.$this->oContext->sqlCountryList;
536 $sSQL .= $this->oContext->excludeSQL(' AND place_id');
537 if ($this->oContext->sqlViewboxCentre) {
538 $sSQL .= ' ORDER BY ST_Distance(';
539 $sSQL .= $this->oContext->sqlViewboxCentre.', ct.centroid) ASC';
540 } elseif ($this->oContext->hasNearPoint()) {
541 $sSQL .= ' ORDER BY '.$this->oContext->distanceSQL('ct.centroid').' ASC';
543 $sSQL .= " LIMIT $iLimit";
544 Debug::printSQL($sSQL);
545 $aDBResults = $oDB->getCol($sSQL);
548 if ($this->oContext->hasNearPoint()) {
549 $sSQL = 'SELECT place_id FROM placex WHERE ';
550 $sSQL .= 'class = :class and type = :type';
551 $sSQL .= ' AND '.$this->oContext->withinSQL('geometry');
552 $sSQL .= ' AND linked_place_id is null';
553 if ($this->oContext->sqlCountryList) {
554 $sSQL .= ' AND country_code in '.$this->oContext->sqlCountryList;
556 $sSQL .= ' ORDER BY '.$this->oContext->distanceSQL('centroid').' ASC';
557 $sSQL .= " LIMIT $iLimit";
558 Debug::printSQL($sSQL);
559 $aDBResults = $oDB->getCol(
561 array(':class' => $this->sClass, ':type' => $this->sType)
566 foreach ($aDBResults as $iPlaceId) {
567 $aResults[$iPlaceId] = new Result($iPlaceId);
573 private function queryPostcode(&$oDB, $iLimit)
575 $sSQL = 'SELECT p.place_id FROM location_postcode p ';
577 if (!empty($this->aAddress)) {
578 $sSQL .= ', search_name s ';
579 $sSQL .= 'WHERE s.place_id = p.parent_place_id ';
580 $sSQL .= 'AND array_cat(s.nameaddress_vector, s.name_vector)';
581 $sSQL .= ' @> '.$oDB->getArraySQL($this->aAddress).' AND ';
586 $sSQL .= "p.postcode = '".reset($this->aName)."'";
587 $sSQL .= $this->countryCodeSQL(' AND p.country_code');
588 if ($this->oContext->bViewboxBounded) {
589 $sSQL .= ' AND ST_Intersects('.$this->oContext->sqlViewboxSmall.', geometry)';
591 $sSQL .= $this->oContext->excludeSQL(' AND p.place_id');
592 $sSQL .= " LIMIT $iLimit";
594 Debug::printSQL($sSQL);
597 foreach ($oDB->getCol($sSQL) as $iPlaceId) {
598 $aResults[$iPlaceId] = new Result($iPlaceId, Result::TABLE_POSTCODE);
604 private function queryNamedPlace(&$oDB, $iMinAddressRank, $iMaxAddressRank, $iLimit)
609 // Sort by existence of the requested house number but only if not
610 // too many results are expected for the street, i.e. if the result
611 // will be narrowed down by an address. Remeber that with ordering
612 // every single result has to be checked.
613 if ($this->sHouseNumber && ($this->bRareName || !empty($this->aAddress) || $this->sPostcode)) {
614 $sHouseNumberRegex = '\\\\m'.$this->sHouseNumber.'\\\\M';
616 $aOrder[0] .= 'EXISTS(';
617 $aOrder[0] .= ' SELECT place_id';
618 $aOrder[0] .= ' FROM placex';
619 $aOrder[0] .= ' WHERE parent_place_id = search_name.place_id';
620 $aOrder[0] .= " AND housenumber ~* E'".$sHouseNumberRegex."'";
621 $aOrder[0] .= ' LIMIT 1';
623 // also housenumbers from interpolation lines table are needed
624 if (preg_match('/[0-9]+/', $this->sHouseNumber)) {
625 $iHouseNumber = intval($this->sHouseNumber);
626 $aOrder[0] .= 'OR EXISTS(';
627 $aOrder[0] .= ' SELECT place_id ';
628 $aOrder[0] .= ' FROM location_property_osmline ';
629 $aOrder[0] .= ' WHERE parent_place_id = search_name.place_id';
630 $aOrder[0] .= ' AND startnumber is not NULL';
631 $aOrder[0] .= ' AND '.$iHouseNumber.'>=startnumber ';
632 $aOrder[0] .= ' AND '.$iHouseNumber.'<=endnumber ';
633 $aOrder[0] .= ' LIMIT 1';
636 $aOrder[0] .= ') DESC';
639 if (!empty($this->aName)) {
640 $aTerms[] = 'name_vector @> '.$oDB->getArraySQL($this->aName);
642 if (!empty($this->aAddress)) {
643 // For infrequent name terms disable index usage for address
644 if ($this->bRareName) {
645 $aTerms[] = 'array_cat(nameaddress_vector,ARRAY[]::integer[]) @> '.$oDB->getArraySQL($this->aAddress);
647 $aTerms[] = 'nameaddress_vector @> '.$oDB->getArraySQL($this->aAddress);
651 $sCountryTerm = $this->countryCodeSQL('country_code');
653 $aTerms[] = $sCountryTerm;
656 if ($this->sHouseNumber) {
657 $aTerms[] = 'address_rank between 16 and 30';
658 } elseif (!$this->sClass || $this->iOperator == Operator::NAME) {
659 if ($iMinAddressRank > 0) {
660 $aTerms[] = "((address_rank between $iMinAddressRank and $iMaxAddressRank) or (search_rank between $iMinAddressRank and $iMaxAddressRank))";
664 if ($this->oContext->hasNearPoint()) {
665 $aTerms[] = $this->oContext->withinSQL('centroid');
666 $aOrder[] = $this->oContext->distanceSQL('centroid');
667 } elseif ($this->sPostcode) {
668 if (empty($this->aAddress)) {
669 $aTerms[] = "EXISTS(SELECT place_id FROM location_postcode p WHERE p.postcode = '".$this->sPostcode."' AND ST_DWithin(search_name.centroid, p.geometry, 0.1))";
671 $aOrder[] = "(SELECT min(ST_Distance(search_name.centroid, p.geometry)) FROM location_postcode p WHERE p.postcode = '".$this->sPostcode."')";
675 $sExcludeSQL = $this->oContext->excludeSQL('place_id');
677 $aTerms[] = $sExcludeSQL;
680 if ($this->oContext->bViewboxBounded) {
681 $aTerms[] = 'centroid && '.$this->oContext->sqlViewboxSmall;
684 if ($this->oContext->hasNearPoint()) {
685 $aOrder[] = $this->oContext->distanceSQL('centroid');
688 if ($this->sHouseNumber) {
689 $sImportanceSQL = '- abs(26 - address_rank) + 3';
691 $sImportanceSQL = '(CASE WHEN importance = 0 OR importance IS NULL THEN 0.75001-(search_rank::float/40) ELSE importance END)';
693 $sImportanceSQL .= $this->oContext->viewboxImportanceSQL('centroid');
694 $aOrder[] = "$sImportanceSQL DESC";
696 $aFullNameAddress = $this->oContext->getFullNameTerms();
697 if (!empty($aFullNameAddress)) {
698 $sExactMatchSQL = ' ( ';
699 $sExactMatchSQL .= ' SELECT count(*) FROM ( ';
700 $sExactMatchSQL .= ' SELECT unnest('.$oDB->getArraySQL($aFullNameAddress).')';
701 $sExactMatchSQL .= ' INTERSECT ';
702 $sExactMatchSQL .= ' SELECT unnest(nameaddress_vector)';
703 $sExactMatchSQL .= ' ) s';
704 $sExactMatchSQL .= ') as exactmatch';
705 $aOrder[] = 'exactmatch DESC';
707 $sExactMatchSQL = '0::int as exactmatch';
710 if ($this->sHouseNumber || $this->sClass) {
716 if (!empty($aTerms)) {
717 $sSQL = 'SELECT place_id, address_rank,'.$sExactMatchSQL;
718 $sSQL .= ' FROM search_name';
719 $sSQL .= ' WHERE '.join(' and ', $aTerms);
720 $sSQL .= ' ORDER BY '.join(', ', $aOrder);
721 $sSQL .= ' LIMIT '.$iLimit;
723 Debug::printSQL($sSQL);
725 $aDBResults = $oDB->getAll($sSQL, null, 'Could not get places for search terms.');
727 foreach ($aDBResults as $aResult) {
728 $oResult = new Result($aResult['place_id']);
729 $oResult->iExactMatches = $aResult['exactmatch'];
730 $oResult->iAddressRank = $aResult['address_rank'];
731 $aResults[$aResult['place_id']] = $oResult;
738 private function queryHouseNumber(&$oDB, $aRoadPlaceIDs)
741 $sRoadPlaceIDs = Result::joinIdsByTableMaxRank(
743 Result::TABLE_PLACEX,
746 $sPOIPlaceIDs = Result::joinIdsByTableMinRank(
748 Result::TABLE_PLACEX,
752 $aIDCondition = array();
753 if ($sRoadPlaceIDs) {
754 $aIDCondition[] = 'parent_place_id in ('.$sRoadPlaceIDs.')';
757 $aIDCondition[] = 'place_id in ('.$sPOIPlaceIDs.')';
760 if (empty($aIDCondition)) {
764 $sHouseNumberRegex = '\\\\m'.$this->sHouseNumber.'\\\\M';
765 $sSQL = 'SELECT place_id FROM placex WHERE';
766 $sSQL .= " housenumber ~* E'".$sHouseNumberRegex."'";
767 $sSQL .= ' AND ('.join(' OR ', $aIDCondition).')';
768 $sSQL .= $this->oContext->excludeSQL(' AND place_id');
770 Debug::printSQL($sSQL);
772 // XXX should inherit the exactMatches from its parent
773 foreach ($oDB->getCol($sSQL) as $iPlaceId) {
774 $aResults[$iPlaceId] = new Result($iPlaceId);
777 $bIsIntHouseNumber= (bool) preg_match('/[0-9]+/', $this->sHouseNumber);
778 $iHousenumber = intval($this->sHouseNumber);
779 if ($bIsIntHouseNumber && $sRoadPlaceIDs && empty($aResults)) {
780 // if nothing found, search in the interpolation line table
781 $sSQL = 'SELECT distinct place_id FROM location_property_osmline';
782 $sSQL .= ' WHERE startnumber is not NULL';
783 $sSQL .= ' AND parent_place_id in ('.$sRoadPlaceIDs.') AND (';
784 if ($iHousenumber % 2 == 0) {
785 // If housenumber is even, look for housenumber in streets
786 // with interpolationtype even or all.
787 $sSQL .= "interpolationtype='even'";
789 // Else look for housenumber with interpolationtype odd or all.
790 $sSQL .= "interpolationtype='odd'";
792 $sSQL .= " or interpolationtype='all') and ";
793 $sSQL .= $iHousenumber.'>=startnumber and ';
794 $sSQL .= $iHousenumber.'<=endnumber';
795 $sSQL .= $this->oContext->excludeSQL(' AND place_id');
797 Debug::printSQL($sSQL);
799 foreach ($oDB->getCol($sSQL) as $iPlaceId) {
800 $oResult = new Result($iPlaceId, Result::TABLE_OSMLINE);
801 $oResult->iHouseNumber = $iHousenumber;
802 $aResults[$iPlaceId] = $oResult;
806 // If nothing found then search in Tiger data (location_property_tiger)
807 if (CONST_Use_US_Tiger_Data && $sRoadPlaceIDs && $bIsIntHouseNumber && empty($aResults)) {
808 $sSQL = 'SELECT place_id FROM location_property_tiger';
809 $sSQL .= ' WHERE parent_place_id in ('.$sRoadPlaceIDs.') and (';
810 if ($iHousenumber % 2 == 0) {
811 $sSQL .= "interpolationtype='even'";
813 $sSQL .= "interpolationtype='odd'";
815 $sSQL .= " or interpolationtype='all') and ";
816 $sSQL .= $iHousenumber.'>=startnumber and ';
817 $sSQL .= $iHousenumber.'<=endnumber';
818 $sSQL .= $this->oContext->excludeSQL(' AND place_id');
820 Debug::printSQL($sSQL);
822 foreach ($oDB->getCol($sSQL) as $iPlaceId) {
823 $oResult = new Result($iPlaceId, Result::TABLE_TIGER);
824 $oResult->iHouseNumber = $iHousenumber;
825 $aResults[$iPlaceId] = $oResult;
833 private function queryPoiByOperator(&$oDB, $aParentIDs, $iLimit)
836 $sPlaceIDs = Result::joinIdsByTable($aParentIDs, Result::TABLE_PLACEX);
842 if ($this->iOperator == Operator::TYPE || $this->iOperator == Operator::NAME) {
843 // If they were searching for a named class (i.e. 'Kings Head pub')
844 // then we might have an extra match
845 $sSQL = 'SELECT place_id FROM placex ';
846 $sSQL .= " WHERE place_id in ($sPlaceIDs)";
847 $sSQL .= " AND class='".$this->sClass."' ";
848 $sSQL .= " AND type='".$this->sType."'";
849 $sSQL .= ' AND linked_place_id is null';
850 $sSQL .= $this->oContext->excludeSQL(' AND place_id');
851 $sSQL .= ' ORDER BY rank_search ASC ';
852 $sSQL .= " LIMIT $iLimit";
854 Debug::printSQL($sSQL);
856 foreach ($oDB->getCol($sSQL) as $iPlaceId) {
857 $aResults[$iPlaceId] = new Result($iPlaceId);
861 // NEAR and IN are handled the same
862 if ($this->iOperator == Operator::TYPE || $this->iOperator == Operator::NEAR) {
863 $sClassTable = $this->poiTable();
864 $bCacheTable = $oDB->tableExists($sClassTable);
866 $sSQL = "SELECT min(rank_search) FROM placex WHERE place_id in ($sPlaceIDs)";
867 Debug::printSQL($sSQL);
868 $iMaxRank = (int) $oDB->getOne($sSQL);
870 // For state / country level searches the normal radius search doesn't work very well
872 if ($iMaxRank < 9 && $bCacheTable) {
873 // Try and get a polygon to search in instead
874 $sSQL = 'SELECT geometry FROM placex';
875 $sSQL .= " WHERE place_id in ($sPlaceIDs)";
876 $sSQL .= " AND rank_search < $iMaxRank + 5";
877 $sSQL .= " AND ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon')";
878 $sSQL .= ' ORDER BY rank_search ASC ';
880 Debug::printSQL($sSQL);
881 $sPlaceGeom = $oDB->getOne($sSQL);
888 $sSQL = 'SELECT place_id FROM placex';
889 $sSQL .= " WHERE place_id in ($sPlaceIDs) and rank_search < $iMaxRank";
890 Debug::printSQL($sSQL);
891 $aPlaceIDs = $oDB->getCol($sSQL);
892 $sPlaceIDs = join(',', $aPlaceIDs);
895 if ($sPlaceIDs || $sPlaceGeom) {
898 // More efficient - can make the range bigger
902 if ($this->oContext->hasNearPoint()) {
903 $sOrderBySQL = $this->oContext->distanceSQL('l.centroid');
904 } elseif ($sPlaceIDs) {
905 $sOrderBySQL = 'ST_Distance(l.centroid, f.geometry)';
906 } elseif ($sPlaceGeom) {
907 $sOrderBySQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
910 $sSQL = 'SELECT distinct i.place_id';
912 $sSQL .= ', i.order_term';
914 $sSQL .= ' from (SELECT l.place_id';
916 $sSQL .= ','.$sOrderBySQL.' as order_term';
918 $sSQL .= ' from '.$sClassTable.' as l';
921 $sSQL .= ',placex as f WHERE ';
922 $sSQL .= "f.place_id in ($sPlaceIDs) ";
923 $sSQL .= " AND ST_DWithin(l.centroid, f.centroid, $fRange)";
924 } elseif ($sPlaceGeom) {
925 $sSQL .= " WHERE ST_Contains('$sPlaceGeom', l.centroid)";
928 $sSQL .= $this->oContext->excludeSQL(' AND l.place_id');
929 $sSQL .= 'limit 300) i ';
931 $sSQL .= 'order by order_term asc';
933 $sSQL .= " limit $iLimit";
935 Debug::printSQL($sSQL);
937 foreach ($oDB->getCol($sSQL) as $iPlaceId) {
938 $aResults[$iPlaceId] = new Result($iPlaceId);
941 if ($this->oContext->hasNearPoint()) {
942 $fRange = $this->oContext->nearRadius();
946 if ($this->oContext->hasNearPoint()) {
947 $sOrderBySQL = $this->oContext->distanceSQL('l.geometry');
949 $sOrderBySQL = 'ST_Distance(l.geometry, f.geometry)';
952 $sSQL = 'SELECT distinct l.place_id';
954 $sSQL .= ','.$sOrderBySQL.' as orderterm';
956 $sSQL .= ' FROM placex as l, placex as f';
957 $sSQL .= " WHERE f.place_id in ($sPlaceIDs)";
958 $sSQL .= " AND ST_DWithin(l.geometry, f.centroid, $fRange)";
959 $sSQL .= " AND l.class='".$this->sClass."'";
960 $sSQL .= " AND l.type='".$this->sType."'";
961 $sSQL .= $this->oContext->excludeSQL(' AND l.place_id');
963 $sSQL .= 'ORDER BY orderterm ASC';
965 $sSQL .= " limit $iLimit";
967 Debug::printSQL($sSQL);
969 foreach ($oDB->getCol($sSQL) as $iPlaceId) {
970 $aResults[$iPlaceId] = new Result($iPlaceId);
979 private function poiTable()
981 return 'place_classtype_'.$this->sClass.'_'.$this->sType;
984 private function countryCodeSQL($sVar)
986 if ($this->sCountryCode) {
987 return $sVar.' = \''.$this->sCountryCode."'";
989 if ($this->oContext->sqlCountryList) {
990 return $sVar.' in '.$this->oContext->sqlCountryList;
996 /////////// Sort functions
999 public static function bySearchRank($a, $b)
1001 if ($a->iSearchRank == $b->iSearchRank) {
1002 return $a->iOperator + strlen($a->sHouseNumber)
1003 - $b->iOperator - strlen($b->sHouseNumber);
1006 return $a->iSearchRank < $b->iSearchRank ? -1 : 1;
1009 //////////// Debugging functions
1012 public function debugInfo()
1015 'Search rank' => $this->iSearchRank,
1016 'Country code' => $this->sCountryCode,
1017 'Name terms' => $this->aName,
1018 'Name terms (stop words)' => $this->aNameNonSearch,
1019 'Address terms' => $this->aAddress,
1020 'Address terms (stop words)' => $this->aAddressNonSearch,
1021 'Address terms (full words)' => $this->aFullNameAddress ?? '',
1022 'Special search' => $this->iOperator,
1023 'Class' => $this->sClass,
1024 'Type' => $this->sType,
1025 'House number' => $this->sHouseNumber,
1026 'Postcode' => $this->sPostcode
1030 public function dumpAsHtmlTableRow(&$aWordIDs)
1032 $kf = function ($k) use (&$aWordIDs) {
1033 return $aWordIDs[$k] ?? '['.$k.']';
1037 echo "<td>$this->iSearchRank</td>";
1038 echo '<td>'.join(', ', array_map($kf, $this->aName)).'</td>';
1039 echo '<td>'.join(', ', array_map($kf, $this->aNameNonSearch)).'</td>';
1040 echo '<td>'.join(', ', array_map($kf, $this->aAddress)).'</td>';
1041 echo '<td>'.join(', ', array_map($kf, $this->aAddressNonSearch)).'</td>';
1042 echo '<td>'.$this->sCountryCode.'</td>';
1043 echo '<td>'.Operator::toString($this->iOperator).'</td>';
1044 echo '<td>'.$this->sClass.'</td>';
1045 echo '<td>'.$this->sType.'</td>';
1046 echo '<td>'.$this->sPostcode.'</td>';
1047 echo '<td>'.$this->sHouseNumber.'</td>';