]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/SearchDescription.php
remove Token from explicit input for SearchDescription extension
[nominatim.git] / lib-php / SearchDescription.php
1 <?php
2
3 namespace Nominatim;
4
5 require_once(CONST_LibDir.'/SpecialSearchOperator.php');
6 require_once(CONST_LibDir.'/SearchContext.php');
7 require_once(CONST_LibDir.'/Result.php');
8
9 /**
10  * Description of a single interpretation of a search query.
11  */
12 class SearchDescription
13 {
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.
31     private $sClass = '';
32     /// Type of special feature to search for.
33     private $sType = '';
34     /// Housenumber of the object.
35     private $sHouseNumber = '';
36     /// Postcode for the object.
37     private $sPostcode = '';
38     /// Global search constraints.
39     private $oContext;
40
41     // Temporary values used while creating the search description.
42
43     /// Index of phrase currently processed.
44     private $iNamePhrase = -1;
45
46     /**
47      * Create an empty search description.
48      *
49      * @param object $oContext Global context to use. Will be inherited by
50      *                         all derived search objects.
51      */
52     public function __construct($oContext)
53     {
54         $this->oContext = $oContext;
55     }
56
57     /**
58      * Get current search rank.
59      *
60      * The higher the search rank the lower the likelihood that the
61      * search is a correct interpretation of the search query.
62      *
63      * @return integer Search rank.
64      */
65     public function getRank()
66     {
67         return $this->iSearchRank;
68     }
69
70     /**
71      * Make this search a POI search.
72      *
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).
75      *
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.
79      *
80      * @return void
81      */
82     public function setPoiSearch($iOperator, $sClass, $sType)
83     {
84         $this->iOperator = $iOperator;
85         $this->sClass = $sClass;
86         $this->sType = $sType;
87     }
88
89     /**
90      * Check if any operator is set.
91      *
92      * @return bool True, if this is a special search operation.
93      */
94     public function hasOperator()
95     {
96         return $this->iOperator != Operator::NONE;
97     }
98
99     /**
100      * Extract key/value pairs from a query.
101      *
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.
105      *
106      * @param string $sQuery Original query string.
107      *
108      * @return string The query string with the special search patterns removed.
109      */
110     public function extractKeyValuePairs($sQuery)
111     {
112         // Search for terms of kind [<key>=<value>].
113         preg_match_all(
114             '/\\[([\\w_]*)=([\\w_]*)\\]/',
115             $sQuery,
116             $aSpecialTermsRaw,
117             PREG_SET_ORDER
118         );
119
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]);
124             }
125         }
126
127         return $sQuery;
128     }
129
130     /**
131      * Check if the combination of parameters is sensible.
132      *
133      * @return bool True, if the search looks valid.
134      */
135     public function isValidSearch()
136     {
137         if (empty($this->aName)) {
138             if ($this->sHouseNumber) {
139                 return false;
140             }
141             if (!$this->sClass && !$this->sCountryCode) {
142                 return false;
143             }
144         }
145
146         return true;
147     }
148
149     /////////// Search building functions
150
151
152     /**
153      * Derive new searches by adding a full term to the existing search.
154      *
155      * @param object  $oSearchTerm  Description of the token.
156      * @param object  $oPosition    Description of the token position within
157                                     the query.
158      *
159      * @return SearchDescription[] List of derived search descriptions.
160      */
161     public function extendWithSearchTerm($oSearchTerm, $oPosition)
162     {
163         $aNewSearches = array();
164
165         if ($oPosition->maybePhrase('country')
166             && is_a($oSearchTerm, '\Nominatim\Token\Country')
167         ) {
168             if (!$this->sCountryCode) {
169                 $oSearch = clone $this;
170                 $oSearch->iSearchRank++;
171                 $oSearch->sCountryCode = $oSearchTerm->sCountryCode;
172                 // Country is almost always at the end of the string
173                 // - increase score for finding it anywhere else (optimisation)
174                 if (!$oPosition->isLastToken()) {
175                     $oSearch->iSearchRank += 5;
176                     $oSearch->iNamePhrase = -1;
177                 }
178                 $aNewSearches[] = $oSearch;
179             }
180         } elseif ($oPosition->maybePhrase('postalcode')
181                   && is_a($oSearchTerm, '\Nominatim\Token\Postcode')
182         ) {
183             if (!$this->sPostcode) {
184                 // If we have structured search or this is the first term,
185                 // make the postcode the primary search element.
186                 if ($this->iOperator == Operator::NONE && $oPosition->isFirstToken()) {
187                     $oSearch = clone $this;
188                     $oSearch->iSearchRank++;
189                     $oSearch->iOperator = Operator::POSTCODE;
190                     $oSearch->aAddress = array_merge($this->aAddress, $this->aName);
191                     $oSearch->aName =
192                         array($oSearchTerm->iId => $oSearchTerm->sPostcode);
193                     $aNewSearches[] = $oSearch;
194                 }
195
196                 // If we have a structured search or this is not the first term,
197                 // add the postcode as an addendum.
198                 if ($this->iOperator != Operator::POSTCODE
199                     && ($oPosition->isPhrase('postalcode') || !empty($this->aName))
200                 ) {
201                     $oSearch = clone $this;
202                     $oSearch->iSearchRank++;
203                     $oSearch->iNamePhrase = -1;
204                     if (strlen($oSearchTerm->sPostcode) < 4) {
205                         $oSearch->iSearchRank += 4 - strlen($oSearchTerm->sPostcode);
206                     }
207                     $oSearch->sPostcode = $oSearchTerm->sPostcode;
208                     $aNewSearches[] = $oSearch;
209                 }
210             }
211         } elseif ($oPosition->maybePhrase('street')
212                  && is_a($oSearchTerm, '\Nominatim\Token\HouseNumber')
213         ) {
214             if (!$this->sHouseNumber && $this->iOperator != Operator::POSTCODE) {
215                 // sanity check: if the housenumber is not mainly made
216                 // up of numbers, add a penalty
217                 $iSearchCost = 1;
218                 if (preg_match('/\\d/', $oSearchTerm->sToken) === 0
219                     || preg_match_all('/[^0-9]/', $oSearchTerm->sToken, $aMatches) > 2) {
220                     $iSearchCost++;
221                 }
222                 if ($this->iOperator != Operator::NONE) {
223                     $iSearchCost++;
224                 }
225                 if (empty($oSearchTerm->iId)) {
226                     $iSearchCost++;
227                 }
228                 // also must not appear in the middle of the address
229                 if (!empty($this->aAddress)
230                     || (!empty($this->aAddressNonSearch))
231                     || $this->sPostcode
232                 ) {
233                     $iSearchCost++;
234                 }
235
236                 $oSearch = clone $this;
237                 $oSearch->iSearchRank += $iSearchCost;
238                 $oSearch->iNamePhrase = -1;
239                 $oSearch->sHouseNumber = $oSearchTerm->sToken;
240                 $aNewSearches[] = $oSearch;
241
242                 // Housenumbers may appear in the name when the place has its own
243                 // address terms.
244                 if ($oSearchTerm->iId !== null
245                     && ($this->iNamePhrase >= 0 || empty($this->aName))
246                     && empty($this->aAddress)
247                    ) {
248                     $oSearch = clone $this;
249                     $oSearch->iSearchRank += $iSearchCost;
250                     $oSearch->aAddress = $this->aName;
251                     $oSearch->bRareName = false;
252                     $oSearch->aName = array($oSearchTerm->iId => $oSearchTerm->iId);
253                     $aNewSearches[] = $oSearch;
254                 }
255             }
256         } elseif ($oPosition->isPhrase('')
257                   && is_a($oSearchTerm, '\Nominatim\Token\SpecialTerm')
258         ) {
259             if ($this->iOperator == Operator::NONE) {
260                 $oSearch = clone $this;
261                 $oSearch->iSearchRank += 2;
262                 $oSearch->iNamePhrase = -1;
263
264                 $iOp = $oSearchTerm->iOperator;
265                 if ($iOp == Operator::NONE) {
266                     if (!empty($this->aName) || $this->oContext->isBoundedSearch()) {
267                         $iOp = Operator::NAME;
268                     } else {
269                         $iOp = Operator::NEAR;
270                     }
271                     $oSearch->iSearchRank += 2;
272                 } elseif (!$oPosition->isFirstToken() && !$oPosition->isLastToken()) {
273                     $oSearch->iSearchRank += 2;
274                 }
275                 if ($this->sHouseNumber) {
276                     $oSearch->iSearchRank++;
277                 }
278
279                 $oSearch->setPoiSearch(
280                     $iOp,
281                     $oSearchTerm->sClass,
282                     $oSearchTerm->sType
283                 );
284                 $aNewSearches[] = $oSearch;
285             }
286         } elseif (!$oPosition->isPhrase('country')
287                   && is_a($oSearchTerm, '\Nominatim\Token\Word')
288         ) {
289             $iWordID = $oSearchTerm->iId;
290             // Full words can only be a name if they appear at the beginning
291             // of the phrase. In structured search the name must forcably in
292             // the first phrase. In unstructured search it may be in a later
293             // phrase when the first phrase is a house number.
294             if (!empty($this->aName) || !($oPosition->isFirstPhrase() || $oPosition->isPhrase(''))) {
295                 if (($oPosition->isPhrase('') || !$oPosition->isFirstPhrase())
296                     && $oSearchTerm->iTermCount > 1
297                 ) {
298                     $oSearch = clone $this;
299                     $oSearch->iNamePhrase = -1;
300                     $oSearch->iSearchRank += 1;
301                     $oSearch->aAddress[$iWordID] = $iWordID;
302                     $aNewSearches[] = $oSearch;
303                 }
304             } elseif (empty($this->aNameNonSearch)) {
305                 $oSearch = clone $this;
306                 $oSearch->iSearchRank++;
307                 $oSearch->aName = array($iWordID => $iWordID);
308                 if (CONST_Search_NameOnlySearchFrequencyThreshold) {
309                     $oSearch->bRareName =
310                         $oSearchTerm->iSearchNameCount
311                           < CONST_Search_NameOnlySearchFrequencyThreshold;
312                 }
313                 $aNewSearches[] = $oSearch;
314             }
315         } elseif (!$oPosition->isPhrase('country')
316                   && is_a($oSearchTerm, '\Nominatim\Token\Partial')
317         ) {
318             $aNewSearches = $this->extendWithPartialTerm(
319                 $oSearchTerm,
320                 $oPosition
321             );
322         }
323
324         return $aNewSearches;
325     }
326
327     /**
328      * Derive new searches by adding a partial term to the existing search.
329      *
330      * @param object  $oSearchTerm  Description of the token.
331      * @param object  $oPosition    Description of the token position within
332                                     the query.
333      *
334      * @return SearchDescription[] List of derived search descriptions.
335      */
336     private function extendWithPartialTerm($oSearchTerm, $oPosition)
337     {
338         $aNewSearches = array();
339         $iWordID = $oSearchTerm->iId;
340
341         if (($oPosition->isPhrase('') || !$oPosition->isFirstPhrase())
342             && (!empty($this->aName))
343         ) {
344             $oSearch = clone $this;
345             $oSearch->iSearchRank++;
346             if (preg_match('#^[0-9 ]+$#', $oSearchTerm->sToken)) {
347                 $oSearch->iSearchRank++;
348             }
349             if ($oSearchTerm->iSearchNameCount < CONST_Max_Word_Frequency) {
350                 $oSearch->aAddress[$iWordID] = $iWordID;
351             } else {
352                 $oSearch->aAddressNonSearch[$iWordID] = $iWordID;
353             }
354             $aNewSearches[] = $oSearch;
355         }
356
357         if ((!$this->sPostcode && !$this->aAddress && !$this->aAddressNonSearch)
358             && ((empty($this->aName) && empty($this->aNameNonSearch))
359                 || $this->iNamePhrase == $oPosition->getPhrase())
360         ) {
361             $oSearch = clone $this;
362             $oSearch->iSearchRank++;
363             if (empty($this->aName) && empty($this->aNameNonSearch)) {
364                 $oSearch->iSearchRank++;
365             }
366             if (preg_match('#^[0-9 ]+$#', $oSearchTerm->sToken)) {
367                 $oSearch->iSearchRank++;
368             }
369             if ($oSearchTerm->iSearchNameCount < CONST_Max_Word_Frequency) {
370                 if (empty($this->aName)
371                     && CONST_Search_NameOnlySearchFrequencyThreshold
372                 ) {
373                     $oSearch->bRareName =
374                         $oSearchTerm->iSearchNameCount
375                           < CONST_Search_NameOnlySearchFrequencyThreshold;
376                 } else {
377                     $oSearch->bRareName = false;
378                 }
379                 $oSearch->aName[$iWordID] = $iWordID;
380             } else {
381                 $oSearch->aNameNonSearch[$iWordID] = $iWordID;
382             }
383             $oSearch->iNamePhrase = $oPosition->getPhrase();
384             $aNewSearches[] = $oSearch;
385         }
386
387         return $aNewSearches;
388     }
389
390     /////////// Query functions
391
392
393     /**
394      * Query database for places that match this search.
395      *
396      * @param object  $oDB      Nominatim::DB instance to use.
397      * @param integer $iMinRank Minimum address rank to restrict search to.
398      * @param integer $iMaxRank Maximum address rank to restrict search to.
399      * @param integer $iLimit   Maximum number of results.
400      *
401      * @return mixed[] An array with two fields: IDs contains the list of
402      *                 matching place IDs and houseNumber the houseNumber
403      *                 if appicable or -1 if not.
404      */
405     public function query(&$oDB, $iMinRank, $iMaxRank, $iLimit)
406     {
407         $aResults = array();
408
409         if ($this->sCountryCode
410             && empty($this->aName)
411             && !$this->iOperator
412             && !$this->sClass
413             && !$this->oContext->hasNearPoint()
414         ) {
415             // Just looking for a country - look it up
416             if (4 >= $iMinRank && 4 <= $iMaxRank) {
417                 $aResults = $this->queryCountry($oDB);
418             }
419         } elseif (empty($this->aName) && empty($this->aAddress)) {
420             // Neither name nor address? Then we must be
421             // looking for a POI in a geographic area.
422             if ($this->oContext->isBoundedSearch()) {
423                 $aResults = $this->queryNearbyPoi($oDB, $iLimit);
424             }
425         } elseif ($this->iOperator == Operator::POSTCODE) {
426             // looking for postcode
427             $aResults = $this->queryPostcode($oDB, $iLimit);
428         } else {
429             // Ordinary search:
430             // First search for places according to name and address.
431             $aResults = $this->queryNamedPlace(
432                 $oDB,
433                 $iMinRank,
434                 $iMaxRank,
435                 $iLimit
436             );
437
438             // Now search for housenumber, if housenumber provided. Can be zero.
439             if (($this->sHouseNumber || $this->sHouseNumber === '0') && !empty($aResults)) {
440                 $aHnResults = $this->queryHouseNumber($oDB, $aResults);
441
442                 // Downgrade the rank of the street results, they are missing
443                 // the housenumber. Also drop POI places (rank 30) here, they
444                 // cannot be a parent place and therefore must not be shown
445                 // as a result for a search with a missing housenumber.
446                 foreach ($aResults as $oRes) {
447                     if ($oRes->iAddressRank < 28) {
448                         if ($oRes->iAddressRank >= 26) {
449                             $oRes->iResultRank++;
450                         } else {
451                             $oRes->iResultRank += 2;
452                         }
453                         $aHnResults[$oRes->iId] = $oRes;
454                     }
455                 }
456
457                 $aResults = $aHnResults;
458             }
459
460             // finally get POIs if requested
461             if ($this->sClass && !empty($aResults)) {
462                 $aResults = $this->queryPoiByOperator($oDB, $aResults, $iLimit);
463             }
464         }
465
466         Debug::printDebugTable('Place IDs', $aResults);
467
468         if (!empty($aResults) && $this->sPostcode) {
469             $sPlaceIds = Result::joinIdsByTable($aResults, Result::TABLE_PLACEX);
470             if ($sPlaceIds) {
471                 $sSQL = 'SELECT place_id FROM placex';
472                 $sSQL .= ' WHERE place_id in ('.$sPlaceIds.')';
473                 $sSQL .= " AND postcode != '".$this->sPostcode."'";
474                 Debug::printSQL($sSQL);
475                 $aFilteredPlaceIDs = $oDB->getCol($sSQL);
476                 if ($aFilteredPlaceIDs) {
477                     foreach ($aFilteredPlaceIDs as $iPlaceId) {
478                         $aResults[$iPlaceId]->iResultRank++;
479                     }
480                 }
481             }
482         }
483
484         return $aResults;
485     }
486
487
488     private function queryCountry(&$oDB)
489     {
490         $sSQL = 'SELECT place_id FROM placex ';
491         $sSQL .= "WHERE country_code='".$this->sCountryCode."'";
492         $sSQL .= ' AND rank_search = 4';
493         if ($this->oContext->bViewboxBounded) {
494             $sSQL .= ' AND ST_Intersects('.$this->oContext->sqlViewboxSmall.', geometry)';
495         }
496         $sSQL .= ' ORDER BY st_area(geometry) DESC LIMIT 1';
497
498         Debug::printSQL($sSQL);
499
500         $iPlaceId = $oDB->getOne($sSQL);
501
502         $aResults = array();
503         if ($iPlaceId) {
504             $aResults[$iPlaceId] = new Result($iPlaceId);
505         }
506
507         return $aResults;
508     }
509
510     private function queryNearbyPoi(&$oDB, $iLimit)
511     {
512         if (!$this->sClass) {
513             return array();
514         }
515
516         $aDBResults = array();
517         $sPoiTable = $this->poiTable();
518
519         if ($oDB->tableExists($sPoiTable)) {
520             $sSQL = 'SELECT place_id FROM '.$sPoiTable.' ct';
521             if ($this->oContext->sqlCountryList) {
522                 $sSQL .= ' JOIN placex USING (place_id)';
523             }
524             if ($this->oContext->hasNearPoint()) {
525                 $sSQL .= ' WHERE '.$this->oContext->withinSQL('ct.centroid');
526             } elseif ($this->oContext->bViewboxBounded) {
527                 $sSQL .= ' WHERE ST_Contains('.$this->oContext->sqlViewboxSmall.', ct.centroid)';
528             }
529             if ($this->oContext->sqlCountryList) {
530                 $sSQL .= ' AND country_code in '.$this->oContext->sqlCountryList;
531             }
532             $sSQL .= $this->oContext->excludeSQL(' AND place_id');
533             if ($this->oContext->sqlViewboxCentre) {
534                 $sSQL .= ' ORDER BY ST_Distance(';
535                 $sSQL .= $this->oContext->sqlViewboxCentre.', ct.centroid) ASC';
536             } elseif ($this->oContext->hasNearPoint()) {
537                 $sSQL .= ' ORDER BY '.$this->oContext->distanceSQL('ct.centroid').' ASC';
538             }
539             $sSQL .= " LIMIT $iLimit";
540             Debug::printSQL($sSQL);
541             $aDBResults = $oDB->getCol($sSQL);
542         }
543
544         if ($this->oContext->hasNearPoint()) {
545             $sSQL = 'SELECT place_id FROM placex WHERE ';
546             $sSQL .= 'class = :class and type = :type';
547             $sSQL .= ' AND '.$this->oContext->withinSQL('geometry');
548             $sSQL .= ' AND linked_place_id is null';
549             if ($this->oContext->sqlCountryList) {
550                 $sSQL .= ' AND country_code in '.$this->oContext->sqlCountryList;
551             }
552             $sSQL .= ' ORDER BY '.$this->oContext->distanceSQL('centroid').' ASC';
553             $sSQL .= " LIMIT $iLimit";
554             Debug::printSQL($sSQL);
555             $aDBResults = $oDB->getCol(
556                 $sSQL,
557                 array(':class' => $this->sClass, ':type' => $this->sType)
558             );
559         }
560
561         $aResults = array();
562         foreach ($aDBResults as $iPlaceId) {
563             $aResults[$iPlaceId] = new Result($iPlaceId);
564         }
565
566         return $aResults;
567     }
568
569     private function queryPostcode(&$oDB, $iLimit)
570     {
571         $sSQL = 'SELECT p.place_id FROM location_postcode p ';
572
573         if (!empty($this->aAddress)) {
574             $sSQL .= ', search_name s ';
575             $sSQL .= 'WHERE s.place_id = p.parent_place_id ';
576             $sSQL .= 'AND array_cat(s.nameaddress_vector, s.name_vector)';
577             $sSQL .= '      @> '.$oDB->getArraySQL($this->aAddress).' AND ';
578         } else {
579             $sSQL .= 'WHERE ';
580         }
581
582         $sSQL .= "p.postcode = '".reset($this->aName)."'";
583         $sSQL .= $this->countryCodeSQL(' AND p.country_code');
584         if ($this->oContext->bViewboxBounded) {
585             $sSQL .= ' AND ST_Intersects('.$this->oContext->sqlViewboxSmall.', geometry)';
586         }
587         $sSQL .= $this->oContext->excludeSQL(' AND p.place_id');
588         $sSQL .= " LIMIT $iLimit";
589
590         Debug::printSQL($sSQL);
591
592         $aResults = array();
593         foreach ($oDB->getCol($sSQL) as $iPlaceId) {
594             $aResults[$iPlaceId] = new Result($iPlaceId, Result::TABLE_POSTCODE);
595         }
596
597         return $aResults;
598     }
599
600     private function queryNamedPlace(&$oDB, $iMinAddressRank, $iMaxAddressRank, $iLimit)
601     {
602         $aTerms = array();
603         $aOrder = array();
604
605         // Sort by existence of the requested house number but only if not
606         // too many results are expected for the street, i.e. if the result
607         // will be narrowed down by an address. Remeber that with ordering
608         // every single result has to be checked.
609         if ($this->sHouseNumber && ($this->bRareName || !empty($this->aAddress) || $this->sPostcode)) {
610             $sHouseNumberRegex = '\\\\m'.$this->sHouseNumber.'\\\\M';
611             $aOrder[] = ' (';
612             $aOrder[0] .= 'EXISTS(';
613             $aOrder[0] .= '  SELECT place_id';
614             $aOrder[0] .= '  FROM placex';
615             $aOrder[0] .= '  WHERE parent_place_id = search_name.place_id';
616             $aOrder[0] .= "    AND housenumber ~* E'".$sHouseNumberRegex."'";
617             $aOrder[0] .= '  LIMIT 1';
618             $aOrder[0] .= ') ';
619             // also housenumbers from interpolation lines table are needed
620             if (preg_match('/[0-9]+/', $this->sHouseNumber)) {
621                 $iHouseNumber = intval($this->sHouseNumber);
622                 $aOrder[0] .= 'OR EXISTS(';
623                 $aOrder[0] .= '  SELECT place_id ';
624                 $aOrder[0] .= '  FROM location_property_osmline ';
625                 $aOrder[0] .= '  WHERE parent_place_id = search_name.place_id';
626                 $aOrder[0] .= '    AND startnumber is not NULL';
627                 $aOrder[0] .= '    AND '.$iHouseNumber.'>=startnumber ';
628                 $aOrder[0] .= '    AND '.$iHouseNumber.'<=endnumber ';
629                 $aOrder[0] .= '  LIMIT 1';
630                 $aOrder[0] .= ')';
631             }
632             $aOrder[0] .= ') DESC';
633         }
634
635         if (!empty($this->aName)) {
636             $aTerms[] = 'name_vector @> '.$oDB->getArraySQL($this->aName);
637         }
638         if (!empty($this->aAddress)) {
639             // For infrequent name terms disable index usage for address
640             if ($this->bRareName) {
641                 $aTerms[] = 'array_cat(nameaddress_vector,ARRAY[]::integer[]) @> '.$oDB->getArraySQL($this->aAddress);
642             } else {
643                 $aTerms[] = 'nameaddress_vector @> '.$oDB->getArraySQL($this->aAddress);
644             }
645         }
646
647         $sCountryTerm = $this->countryCodeSQL('country_code');
648         if ($sCountryTerm) {
649             $aTerms[] = $sCountryTerm;
650         }
651
652         if ($this->sHouseNumber) {
653             $aTerms[] = 'address_rank between 16 and 30';
654         } elseif (!$this->sClass || $this->iOperator == Operator::NAME) {
655             if ($iMinAddressRank > 0) {
656                 $aTerms[] = "((address_rank between $iMinAddressRank and $iMaxAddressRank) or (search_rank between $iMinAddressRank and $iMaxAddressRank))";
657             }
658         }
659
660         if ($this->oContext->hasNearPoint()) {
661             $aTerms[] = $this->oContext->withinSQL('centroid');
662             $aOrder[] = $this->oContext->distanceSQL('centroid');
663         } elseif ($this->sPostcode) {
664             if (empty($this->aAddress)) {
665                 $aTerms[] = "EXISTS(SELECT place_id FROM location_postcode p WHERE p.postcode = '".$this->sPostcode."' AND ST_DWithin(search_name.centroid, p.geometry, 0.1))";
666             } else {
667                 $aOrder[] = "(SELECT min(ST_Distance(search_name.centroid, p.geometry)) FROM location_postcode p WHERE p.postcode = '".$this->sPostcode."')";
668             }
669         }
670
671         $sExcludeSQL = $this->oContext->excludeSQL('place_id');
672         if ($sExcludeSQL) {
673             $aTerms[] = $sExcludeSQL;
674         }
675
676         if ($this->oContext->bViewboxBounded) {
677             $aTerms[] = 'centroid && '.$this->oContext->sqlViewboxSmall;
678         }
679
680         if ($this->oContext->hasNearPoint()) {
681             $aOrder[] = $this->oContext->distanceSQL('centroid');
682         }
683
684         if ($this->sHouseNumber) {
685             $sImportanceSQL = '- abs(26 - address_rank) + 3';
686         } else {
687             $sImportanceSQL = '(CASE WHEN importance = 0 OR importance IS NULL THEN 0.75001-(search_rank::float/40) ELSE importance END)';
688         }
689         $sImportanceSQL .= $this->oContext->viewboxImportanceSQL('centroid');
690         $aOrder[] = "$sImportanceSQL DESC";
691
692         $aFullNameAddress = $this->oContext->getFullNameTerms();
693         if (!empty($aFullNameAddress)) {
694             $sExactMatchSQL = ' ( ';
695             $sExactMatchSQL .= ' SELECT count(*) FROM ( ';
696             $sExactMatchSQL .= '  SELECT unnest('.$oDB->getArraySQL($aFullNameAddress).')';
697             $sExactMatchSQL .= '    INTERSECT ';
698             $sExactMatchSQL .= '  SELECT unnest(nameaddress_vector)';
699             $sExactMatchSQL .= ' ) s';
700             $sExactMatchSQL .= ') as exactmatch';
701             $aOrder[] = 'exactmatch DESC';
702         } else {
703             $sExactMatchSQL = '0::int as exactmatch';
704         }
705
706         if ($this->sHouseNumber || $this->sClass) {
707             $iLimit = 40;
708         }
709
710         $aResults = array();
711
712         if (!empty($aTerms)) {
713             $sSQL = 'SELECT place_id, address_rank,'.$sExactMatchSQL;
714             $sSQL .= ' FROM search_name';
715             $sSQL .= ' WHERE '.join(' and ', $aTerms);
716             $sSQL .= ' ORDER BY '.join(', ', $aOrder);
717             $sSQL .= ' LIMIT '.$iLimit;
718
719             Debug::printSQL($sSQL);
720
721             $aDBResults = $oDB->getAll($sSQL, null, 'Could not get places for search terms.');
722
723             foreach ($aDBResults as $aResult) {
724                 $oResult = new Result($aResult['place_id']);
725                 $oResult->iExactMatches = $aResult['exactmatch'];
726                 $oResult->iAddressRank = $aResult['address_rank'];
727                 $aResults[$aResult['place_id']] = $oResult;
728             }
729         }
730
731         return $aResults;
732     }
733
734     private function queryHouseNumber(&$oDB, $aRoadPlaceIDs)
735     {
736         $aResults = array();
737         $sRoadPlaceIDs = Result::joinIdsByTableMaxRank(
738             $aRoadPlaceIDs,
739             Result::TABLE_PLACEX,
740             27
741         );
742         $sPOIPlaceIDs = Result::joinIdsByTableMinRank(
743             $aRoadPlaceIDs,
744             Result::TABLE_PLACEX,
745             30
746         );
747
748         $aIDCondition = array();
749         if ($sRoadPlaceIDs) {
750             $aIDCondition[] = 'parent_place_id in ('.$sRoadPlaceIDs.')';
751         }
752         if ($sPOIPlaceIDs) {
753             $aIDCondition[] = 'place_id in ('.$sPOIPlaceIDs.')';
754         }
755
756         if (empty($aIDCondition)) {
757             return $aResults;
758         }
759
760         $sHouseNumberRegex = '\\\\m'.$this->sHouseNumber.'\\\\M';
761         $sSQL = 'SELECT place_id FROM placex WHERE';
762         $sSQL .= "  housenumber ~* E'".$sHouseNumberRegex."'";
763         $sSQL .= ' AND ('.join(' OR ', $aIDCondition).')';
764         $sSQL .= $this->oContext->excludeSQL(' AND place_id');
765
766         Debug::printSQL($sSQL);
767
768         // XXX should inherit the exactMatches from its parent
769         foreach ($oDB->getCol($sSQL) as $iPlaceId) {
770             $aResults[$iPlaceId] = new Result($iPlaceId);
771         }
772
773         $bIsIntHouseNumber= (bool) preg_match('/[0-9]+/', $this->sHouseNumber);
774         $iHousenumber = intval($this->sHouseNumber);
775         if ($bIsIntHouseNumber && $sRoadPlaceIDs && empty($aResults)) {
776             // if nothing found, search in the interpolation line table
777             $sSQL = 'SELECT distinct place_id FROM location_property_osmline';
778             $sSQL .= ' WHERE startnumber is not NULL';
779             $sSQL .= '  AND parent_place_id in ('.$sRoadPlaceIDs.') AND (';
780             if ($iHousenumber % 2 == 0) {
781                 // If housenumber is even, look for housenumber in streets
782                 // with interpolationtype even or all.
783                 $sSQL .= "interpolationtype='even'";
784             } else {
785                 // Else look for housenumber with interpolationtype odd or all.
786                 $sSQL .= "interpolationtype='odd'";
787             }
788             $sSQL .= " or interpolationtype='all') and ";
789             $sSQL .= $iHousenumber.'>=startnumber and ';
790             $sSQL .= $iHousenumber.'<=endnumber';
791             $sSQL .= $this->oContext->excludeSQL(' AND place_id');
792
793             Debug::printSQL($sSQL);
794
795             foreach ($oDB->getCol($sSQL) as $iPlaceId) {
796                 $oResult = new Result($iPlaceId, Result::TABLE_OSMLINE);
797                 $oResult->iHouseNumber = $iHousenumber;
798                 $aResults[$iPlaceId] = $oResult;
799             }
800         }
801
802         // If nothing found then search in Tiger data (location_property_tiger)
803         if (CONST_Use_US_Tiger_Data && $sRoadPlaceIDs && $bIsIntHouseNumber && empty($aResults)) {
804             $sSQL = 'SELECT place_id FROM location_property_tiger';
805             $sSQL .= ' WHERE parent_place_id in ('.$sRoadPlaceIDs.') and (';
806             if ($iHousenumber % 2 == 0) {
807                 $sSQL .= "interpolationtype='even'";
808             } else {
809                 $sSQL .= "interpolationtype='odd'";
810             }
811             $sSQL .= " or interpolationtype='all') and ";
812             $sSQL .= $iHousenumber.'>=startnumber and ';
813             $sSQL .= $iHousenumber.'<=endnumber';
814             $sSQL .= $this->oContext->excludeSQL(' AND place_id');
815
816             Debug::printSQL($sSQL);
817
818             foreach ($oDB->getCol($sSQL) as $iPlaceId) {
819                 $oResult = new Result($iPlaceId, Result::TABLE_TIGER);
820                 $oResult->iHouseNumber = $iHousenumber;
821                 $aResults[$iPlaceId] = $oResult;
822             }
823         }
824
825         return $aResults;
826     }
827
828
829     private function queryPoiByOperator(&$oDB, $aParentIDs, $iLimit)
830     {
831         $aResults = array();
832         $sPlaceIDs = Result::joinIdsByTable($aParentIDs, Result::TABLE_PLACEX);
833
834         if (!$sPlaceIDs) {
835             return $aResults;
836         }
837
838         if ($this->iOperator == Operator::TYPE || $this->iOperator == Operator::NAME) {
839             // If they were searching for a named class (i.e. 'Kings Head pub')
840             // then we might have an extra match
841             $sSQL = 'SELECT place_id FROM placex ';
842             $sSQL .= " WHERE place_id in ($sPlaceIDs)";
843             $sSQL .= "   AND class='".$this->sClass."' ";
844             $sSQL .= "   AND type='".$this->sType."'";
845             $sSQL .= '   AND linked_place_id is null';
846             $sSQL .= $this->oContext->excludeSQL(' AND place_id');
847             $sSQL .= ' ORDER BY rank_search ASC ';
848             $sSQL .= " LIMIT $iLimit";
849
850             Debug::printSQL($sSQL);
851
852             foreach ($oDB->getCol($sSQL) as $iPlaceId) {
853                 $aResults[$iPlaceId] = new Result($iPlaceId);
854             }
855         }
856
857         // NEAR and IN are handled the same
858         if ($this->iOperator == Operator::TYPE || $this->iOperator == Operator::NEAR) {
859             $sClassTable = $this->poiTable();
860             $bCacheTable = $oDB->tableExists($sClassTable);
861
862             $sSQL = "SELECT min(rank_search) FROM placex WHERE place_id in ($sPlaceIDs)";
863             Debug::printSQL($sSQL);
864             $iMaxRank = (int) $oDB->getOne($sSQL);
865
866             // For state / country level searches the normal radius search doesn't work very well
867             $sPlaceGeom = false;
868             if ($iMaxRank < 9 && $bCacheTable) {
869                 // Try and get a polygon to search in instead
870                 $sSQL = 'SELECT geometry FROM placex';
871                 $sSQL .= " WHERE place_id in ($sPlaceIDs)";
872                 $sSQL .= "   AND rank_search < $iMaxRank + 5";
873                 $sSQL .= "   AND ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon')";
874                 $sSQL .= ' ORDER BY rank_search ASC ';
875                 $sSQL .= ' LIMIT 1';
876                 Debug::printSQL($sSQL);
877                 $sPlaceGeom = $oDB->getOne($sSQL);
878             }
879
880             if ($sPlaceGeom) {
881                 $sPlaceIDs = false;
882             } else {
883                 $iMaxRank += 5;
884                 $sSQL = 'SELECT place_id FROM placex';
885                 $sSQL .= " WHERE place_id in ($sPlaceIDs) and rank_search < $iMaxRank";
886                 Debug::printSQL($sSQL);
887                 $aPlaceIDs = $oDB->getCol($sSQL);
888                 $sPlaceIDs = join(',', $aPlaceIDs);
889             }
890
891             if ($sPlaceIDs || $sPlaceGeom) {
892                 $fRange = 0.01;
893                 if ($bCacheTable) {
894                     // More efficient - can make the range bigger
895                     $fRange = 0.05;
896
897                     $sOrderBySQL = '';
898                     if ($this->oContext->hasNearPoint()) {
899                         $sOrderBySQL = $this->oContext->distanceSQL('l.centroid');
900                     } elseif ($sPlaceIDs) {
901                         $sOrderBySQL = 'ST_Distance(l.centroid, f.geometry)';
902                     } elseif ($sPlaceGeom) {
903                         $sOrderBySQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
904                     }
905
906                     $sSQL = 'SELECT distinct i.place_id';
907                     if ($sOrderBySQL) {
908                         $sSQL .= ', i.order_term';
909                     }
910                     $sSQL .= ' from (SELECT l.place_id';
911                     if ($sOrderBySQL) {
912                         $sSQL .= ','.$sOrderBySQL.' as order_term';
913                     }
914                     $sSQL .= ' from '.$sClassTable.' as l';
915
916                     if ($sPlaceIDs) {
917                         $sSQL .= ',placex as f WHERE ';
918                         $sSQL .= "f.place_id in ($sPlaceIDs) ";
919                         $sSQL .= " AND ST_DWithin(l.centroid, f.centroid, $fRange)";
920                     } elseif ($sPlaceGeom) {
921                         $sSQL .= " WHERE ST_Contains('$sPlaceGeom', l.centroid)";
922                     }
923
924                     $sSQL .= $this->oContext->excludeSQL(' AND l.place_id');
925                     $sSQL .= 'limit 300) i ';
926                     if ($sOrderBySQL) {
927                         $sSQL .= 'order by order_term asc';
928                     }
929                     $sSQL .= " limit $iLimit";
930
931                     Debug::printSQL($sSQL);
932
933                     foreach ($oDB->getCol($sSQL) as $iPlaceId) {
934                         $aResults[$iPlaceId] = new Result($iPlaceId);
935                     }
936                 } else {
937                     if ($this->oContext->hasNearPoint()) {
938                         $fRange = $this->oContext->nearRadius();
939                     }
940
941                     $sOrderBySQL = '';
942                     if ($this->oContext->hasNearPoint()) {
943                         $sOrderBySQL = $this->oContext->distanceSQL('l.geometry');
944                     } else {
945                         $sOrderBySQL = 'ST_Distance(l.geometry, f.geometry)';
946                     }
947
948                     $sSQL = 'SELECT distinct l.place_id';
949                     if ($sOrderBySQL) {
950                         $sSQL .= ','.$sOrderBySQL.' as orderterm';
951                     }
952                     $sSQL .= ' FROM placex as l, placex as f';
953                     $sSQL .= " WHERE f.place_id in ($sPlaceIDs)";
954                     $sSQL .= "  AND ST_DWithin(l.geometry, f.centroid, $fRange)";
955                     $sSQL .= "  AND l.class='".$this->sClass."'";
956                     $sSQL .= "  AND l.type='".$this->sType."'";
957                     $sSQL .= $this->oContext->excludeSQL(' AND l.place_id');
958                     if ($sOrderBySQL) {
959                         $sSQL .= 'ORDER BY orderterm ASC';
960                     }
961                     $sSQL .= " limit $iLimit";
962
963                     Debug::printSQL($sSQL);
964
965                     foreach ($oDB->getCol($sSQL) as $iPlaceId) {
966                         $aResults[$iPlaceId] = new Result($iPlaceId);
967                     }
968                 }
969             }
970         }
971
972         return $aResults;
973     }
974
975     private function poiTable()
976     {
977         return 'place_classtype_'.$this->sClass.'_'.$this->sType;
978     }
979
980     private function countryCodeSQL($sVar)
981     {
982         if ($this->sCountryCode) {
983             return $sVar.' = \''.$this->sCountryCode."'";
984         }
985         if ($this->oContext->sqlCountryList) {
986             return $sVar.' in '.$this->oContext->sqlCountryList;
987         }
988
989         return '';
990     }
991
992     /////////// Sort functions
993
994
995     public static function bySearchRank($a, $b)
996     {
997         if ($a->iSearchRank == $b->iSearchRank) {
998             return $a->iOperator + strlen($a->sHouseNumber)
999                      - $b->iOperator - strlen($b->sHouseNumber);
1000         }
1001
1002         return $a->iSearchRank < $b->iSearchRank ? -1 : 1;
1003     }
1004
1005     //////////// Debugging functions
1006
1007
1008     public function debugInfo()
1009     {
1010         return array(
1011                 'Search rank' => $this->iSearchRank,
1012                 'Country code' => $this->sCountryCode,
1013                 'Name terms' => $this->aName,
1014                 'Name terms (stop words)' => $this->aNameNonSearch,
1015                 'Address terms' => $this->aAddress,
1016                 'Address terms (stop words)' => $this->aAddressNonSearch,
1017                 'Address terms (full words)' => $this->aFullNameAddress ?? '',
1018                 'Special search' => $this->iOperator,
1019                 'Class' => $this->sClass,
1020                 'Type' => $this->sType,
1021                 'House number' => $this->sHouseNumber,
1022                 'Postcode' => $this->sPostcode
1023                );
1024     }
1025
1026     public function dumpAsHtmlTableRow(&$aWordIDs)
1027     {
1028         $kf = function ($k) use (&$aWordIDs) {
1029             return $aWordIDs[$k] ?? '['.$k.']';
1030         };
1031
1032         echo '<tr>';
1033         echo "<td>$this->iSearchRank</td>";
1034         echo '<td>'.join(', ', array_map($kf, $this->aName)).'</td>';
1035         echo '<td>'.join(', ', array_map($kf, $this->aNameNonSearch)).'</td>';
1036         echo '<td>'.join(', ', array_map($kf, $this->aAddress)).'</td>';
1037         echo '<td>'.join(', ', array_map($kf, $this->aAddressNonSearch)).'</td>';
1038         echo '<td>'.$this->sCountryCode.'</td>';
1039         echo '<td>'.Operator::toString($this->iOperator).'</td>';
1040         echo '<td>'.$this->sClass.'</td>';
1041         echo '<td>'.$this->sType.'</td>';
1042         echo '<td>'.$this->sPostcode.'</td>';
1043         echo '<td>'.$this->sHouseNumber.'</td>';
1044
1045         echo '</tr>';
1046     }
1047 }