]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/SearchDescription.php
factor out query position
[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 string  $sToken       Term for the token.
156      * @param object  $oSearchTerm  Description of the token.
157      * @param object  $oPosition    Description of the token position within
158                                     the query.
159      *
160      * @return SearchDescription[] List of derived search descriptions.
161      */
162     public function extendWithSearchTerm($sToken, $oSearchTerm, $oPosition)
163     {
164         $aNewSearches = array();
165
166         if ($oPosition->maybePhrase('country')
167             && is_a($oSearchTerm, '\Nominatim\Token\Country')
168         ) {
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;
178                 }
179                 $aNewSearches[] = $oSearch;
180             }
181         } elseif ($oPosition->maybePhrase('postalcode')
182                   && is_a($oSearchTerm, '\Nominatim\Token\Postcode')
183         ) {
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);
192                     $oSearch->aName =
193                         array($oSearchTerm->iId => $oSearchTerm->sPostcode);
194                     $aNewSearches[] = $oSearch;
195                 }
196
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))
201                 ) {
202                     $oSearch = clone $this;
203                     $oSearch->iSearchRank++;
204                     $oSearch->iNamePhrase = -1;
205                     if (strlen($oSearchTerm->sPostcode) < 4) {
206                         $oSearch->iSearchRank += 4 - strlen($oSearchTerm->sPostcode);
207                     }
208                     $oSearch->sPostcode = $oSearchTerm->sPostcode;
209                     $aNewSearches[] = $oSearch;
210                 }
211             }
212         } elseif ($oPosition->maybePhrase('street')
213                  && is_a($oSearchTerm, '\Nominatim\Token\HouseNumber')
214         ) {
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
218                 $iSearchCost = 1;
219                 if (preg_match('/\\d/', $oSearchTerm->sToken) === 0
220                     || preg_match_all('/[^0-9]/', $oSearchTerm->sToken, $aMatches) > 2) {
221                     $iSearchCost++;
222                 }
223                 if ($this->iOperator != Operator::NONE) {
224                     $iSearchCost++;
225                 }
226                 if (empty($oSearchTerm->iId)) {
227                     $iSearchCost++;
228                 }
229                 // also must not appear in the middle of the address
230                 if (!empty($this->aAddress)
231                     || (!empty($this->aAddressNonSearch))
232                     || $this->sPostcode
233                 ) {
234                     $iSearchCost++;
235                 }
236
237                 $oSearch = clone $this;
238                 $oSearch->iSearchRank += $iSearchCost;
239                 $oSearch->iNamePhrase = -1;
240                 $oSearch->sHouseNumber = $oSearchTerm->sToken;
241                 $aNewSearches[] = $oSearch;
242
243                 // Housenumbers may appear in the name when the place has its own
244                 // address terms.
245                 if ($oSearchTerm->iId !== null
246                     && ($this->iNamePhrase >= 0 || empty($this->aName))
247                     && empty($this->aAddress)
248                    ) {
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;
255                 }
256             }
257         } elseif ($oPosition->isPhrase('')
258                   && is_a($oSearchTerm, '\Nominatim\Token\SpecialTerm')
259         ) {
260             if ($this->iOperator == Operator::NONE) {
261                 $oSearch = clone $this;
262                 $oSearch->iSearchRank += 2;
263                 $oSearch->iNamePhrase = -1;
264
265                 $iOp = $oSearchTerm->iOperator;
266                 if ($iOp == Operator::NONE) {
267                     if (!empty($this->aName) || $this->oContext->isBoundedSearch()) {
268                         $iOp = Operator::NAME;
269                     } else {
270                         $iOp = Operator::NEAR;
271                     }
272                     $oSearch->iSearchRank += 2;
273                 } elseif (!$oPosition->isFirstToken() && !$oPosition->isLastToken()) {
274                     $oSearch->iSearchRank += 2;
275                 }
276                 if ($this->sHouseNumber) {
277                     $oSearch->iSearchRank++;
278                 }
279
280                 $oSearch->setPoiSearch(
281                     $iOp,
282                     $oSearchTerm->sClass,
283                     $oSearchTerm->sType
284                 );
285                 $aNewSearches[] = $oSearch;
286             }
287         } elseif (!$oPosition->isPhrase('country')
288                   && is_a($oSearchTerm, '\Nominatim\Token\Word')
289         ) {
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
298                 ) {
299                     $oSearch = clone $this;
300                     $oSearch->iNamePhrase = -1;
301                     $oSearch->iSearchRank += 1;
302                     $oSearch->aAddress[$iWordID] = $iWordID;
303                     $aNewSearches[] = $oSearch;
304                 }
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;
313                 }
314                 $aNewSearches[] = $oSearch;
315             }
316         } elseif (!$oPosition->isPhrase('country')
317                   && is_a($oSearchTerm, '\Nominatim\Token\Partial')
318                   && strpos($sToken, ' ') === false
319         ) {
320             $aNewSearches = $this->extendWithPartialTerm(
321                 $sToken,
322                 $oSearchTerm,
323                 $oPosition
324             );
325         }
326
327         return $aNewSearches;
328     }
329
330     /**
331      * Derive new searches by adding a partial term to the existing search.
332      *
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
336                                     the query.
337      *
338      * @return SearchDescription[] List of derived search descriptions.
339      */
340     private function extendWithPartialTerm($sToken, $oSearchTerm, $oPosition)
341     {
342         $aNewSearches = array();
343         $iWordID = $oSearchTerm->iId;
344
345         if (($oPosition->isPhrase('') || !$oPosition->isFirstPhrase())
346             && (!empty($this->aName))
347         ) {
348             $oSearch = clone $this;
349             $oSearch->iSearchRank++;
350             if (preg_match('#^[0-9 ]+$#', $sToken)) {
351                 $oSearch->iSearchRank++;
352             }
353             if ($oSearchTerm->iSearchNameCount < CONST_Max_Word_Frequency) {
354                 $oSearch->aAddress[$iWordID] = $iWordID;
355             } else {
356                 $oSearch->aAddressNonSearch[$iWordID] = $iWordID;
357             }
358             $aNewSearches[] = $oSearch;
359         }
360
361         if ((!$this->sPostcode && !$this->aAddress && !$this->aAddressNonSearch)
362             && ((empty($this->aName) && empty($this->aNameNonSearch))
363                 || $this->iNamePhrase == $oPosition->getPhrase())
364         ) {
365             $oSearch = clone $this;
366             $oSearch->iSearchRank++;
367             if (empty($this->aName) && empty($this->aNameNonSearch)) {
368                 $oSearch->iSearchRank++;
369             }
370             if (preg_match('#^[0-9 ]+$#', $sToken)) {
371                 $oSearch->iSearchRank++;
372             }
373             if ($oSearchTerm->iSearchNameCount < CONST_Max_Word_Frequency) {
374                 if (empty($this->aName)
375                     && CONST_Search_NameOnlySearchFrequencyThreshold
376                 ) {
377                     $oSearch->bRareName =
378                         $oSearchTerm->iSearchNameCount
379                           < CONST_Search_NameOnlySearchFrequencyThreshold;
380                 } else {
381                     $oSearch->bRareName = false;
382                 }
383                 $oSearch->aName[$iWordID] = $iWordID;
384             } else {
385                 $oSearch->aNameNonSearch[$iWordID] = $iWordID;
386             }
387             $oSearch->iNamePhrase = $oPosition->getPhrase();
388             $aNewSearches[] = $oSearch;
389         }
390
391         return $aNewSearches;
392     }
393
394     /////////// Query functions
395
396
397     /**
398      * Query database for places that match this search.
399      *
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.
404      *
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.
408      */
409     public function query(&$oDB, $iMinRank, $iMaxRank, $iLimit)
410     {
411         $aResults = array();
412
413         if ($this->sCountryCode
414             && empty($this->aName)
415             && !$this->iOperator
416             && !$this->sClass
417             && !$this->oContext->hasNearPoint()
418         ) {
419             // Just looking for a country - look it up
420             if (4 >= $iMinRank && 4 <= $iMaxRank) {
421                 $aResults = $this->queryCountry($oDB);
422             }
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);
428             }
429         } elseif ($this->iOperator == Operator::POSTCODE) {
430             // looking for postcode
431             $aResults = $this->queryPostcode($oDB, $iLimit);
432         } else {
433             // Ordinary search:
434             // First search for places according to name and address.
435             $aResults = $this->queryNamedPlace(
436                 $oDB,
437                 $iMinRank,
438                 $iMaxRank,
439                 $iLimit
440             );
441
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);
445
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++;
454                         } else {
455                             $oRes->iResultRank += 2;
456                         }
457                         $aHnResults[$oRes->iId] = $oRes;
458                     }
459                 }
460
461                 $aResults = $aHnResults;
462             }
463
464             // finally get POIs if requested
465             if ($this->sClass && !empty($aResults)) {
466                 $aResults = $this->queryPoiByOperator($oDB, $aResults, $iLimit);
467             }
468         }
469
470         Debug::printDebugTable('Place IDs', $aResults);
471
472         if (!empty($aResults) && $this->sPostcode) {
473             $sPlaceIds = Result::joinIdsByTable($aResults, Result::TABLE_PLACEX);
474             if ($sPlaceIds) {
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++;
483                     }
484                 }
485             }
486         }
487
488         return $aResults;
489     }
490
491
492     private function queryCountry(&$oDB)
493     {
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)';
499         }
500         $sSQL .= ' ORDER BY st_area(geometry) DESC LIMIT 1';
501
502         Debug::printSQL($sSQL);
503
504         $iPlaceId = $oDB->getOne($sSQL);
505
506         $aResults = array();
507         if ($iPlaceId) {
508             $aResults[$iPlaceId] = new Result($iPlaceId);
509         }
510
511         return $aResults;
512     }
513
514     private function queryNearbyPoi(&$oDB, $iLimit)
515     {
516         if (!$this->sClass) {
517             return array();
518         }
519
520         $aDBResults = array();
521         $sPoiTable = $this->poiTable();
522
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)';
527             }
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)';
532             }
533             if ($this->oContext->sqlCountryList) {
534                 $sSQL .= ' AND country_code in '.$this->oContext->sqlCountryList;
535             }
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';
542             }
543             $sSQL .= " LIMIT $iLimit";
544             Debug::printSQL($sSQL);
545             $aDBResults = $oDB->getCol($sSQL);
546         }
547
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;
555             }
556             $sSQL .= ' ORDER BY '.$this->oContext->distanceSQL('centroid').' ASC';
557             $sSQL .= " LIMIT $iLimit";
558             Debug::printSQL($sSQL);
559             $aDBResults = $oDB->getCol(
560                 $sSQL,
561                 array(':class' => $this->sClass, ':type' => $this->sType)
562             );
563         }
564
565         $aResults = array();
566         foreach ($aDBResults as $iPlaceId) {
567             $aResults[$iPlaceId] = new Result($iPlaceId);
568         }
569
570         return $aResults;
571     }
572
573     private function queryPostcode(&$oDB, $iLimit)
574     {
575         $sSQL = 'SELECT p.place_id FROM location_postcode p ';
576
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 ';
582         } else {
583             $sSQL .= 'WHERE ';
584         }
585
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)';
590         }
591         $sSQL .= $this->oContext->excludeSQL(' AND p.place_id');
592         $sSQL .= " LIMIT $iLimit";
593
594         Debug::printSQL($sSQL);
595
596         $aResults = array();
597         foreach ($oDB->getCol($sSQL) as $iPlaceId) {
598             $aResults[$iPlaceId] = new Result($iPlaceId, Result::TABLE_POSTCODE);
599         }
600
601         return $aResults;
602     }
603
604     private function queryNamedPlace(&$oDB, $iMinAddressRank, $iMaxAddressRank, $iLimit)
605     {
606         $aTerms = array();
607         $aOrder = array();
608
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';
615             $aOrder[] = ' (';
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';
622             $aOrder[0] .= ') ';
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';
634                 $aOrder[0] .= ')';
635             }
636             $aOrder[0] .= ') DESC';
637         }
638
639         if (!empty($this->aName)) {
640             $aTerms[] = 'name_vector @> '.$oDB->getArraySQL($this->aName);
641         }
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);
646             } else {
647                 $aTerms[] = 'nameaddress_vector @> '.$oDB->getArraySQL($this->aAddress);
648             }
649         }
650
651         $sCountryTerm = $this->countryCodeSQL('country_code');
652         if ($sCountryTerm) {
653             $aTerms[] = $sCountryTerm;
654         }
655
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))";
661             }
662         }
663
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))";
670             } else {
671                 $aOrder[] = "(SELECT min(ST_Distance(search_name.centroid, p.geometry)) FROM location_postcode p WHERE p.postcode = '".$this->sPostcode."')";
672             }
673         }
674
675         $sExcludeSQL = $this->oContext->excludeSQL('place_id');
676         if ($sExcludeSQL) {
677             $aTerms[] = $sExcludeSQL;
678         }
679
680         if ($this->oContext->bViewboxBounded) {
681             $aTerms[] = 'centroid && '.$this->oContext->sqlViewboxSmall;
682         }
683
684         if ($this->oContext->hasNearPoint()) {
685             $aOrder[] = $this->oContext->distanceSQL('centroid');
686         }
687
688         if ($this->sHouseNumber) {
689             $sImportanceSQL = '- abs(26 - address_rank) + 3';
690         } else {
691             $sImportanceSQL = '(CASE WHEN importance = 0 OR importance IS NULL THEN 0.75001-(search_rank::float/40) ELSE importance END)';
692         }
693         $sImportanceSQL .= $this->oContext->viewboxImportanceSQL('centroid');
694         $aOrder[] = "$sImportanceSQL DESC";
695
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';
706         } else {
707             $sExactMatchSQL = '0::int as exactmatch';
708         }
709
710         if ($this->sHouseNumber || $this->sClass) {
711             $iLimit = 40;
712         }
713
714         $aResults = array();
715
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;
722
723             Debug::printSQL($sSQL);
724
725             $aDBResults = $oDB->getAll($sSQL, null, 'Could not get places for search terms.');
726
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;
732             }
733         }
734
735         return $aResults;
736     }
737
738     private function queryHouseNumber(&$oDB, $aRoadPlaceIDs)
739     {
740         $aResults = array();
741         $sRoadPlaceIDs = Result::joinIdsByTableMaxRank(
742             $aRoadPlaceIDs,
743             Result::TABLE_PLACEX,
744             27
745         );
746         $sPOIPlaceIDs = Result::joinIdsByTableMinRank(
747             $aRoadPlaceIDs,
748             Result::TABLE_PLACEX,
749             30
750         );
751
752         $aIDCondition = array();
753         if ($sRoadPlaceIDs) {
754             $aIDCondition[] = 'parent_place_id in ('.$sRoadPlaceIDs.')';
755         }
756         if ($sPOIPlaceIDs) {
757             $aIDCondition[] = 'place_id in ('.$sPOIPlaceIDs.')';
758         }
759
760         if (empty($aIDCondition)) {
761             return $aResults;
762         }
763
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');
769
770         Debug::printSQL($sSQL);
771
772         // XXX should inherit the exactMatches from its parent
773         foreach ($oDB->getCol($sSQL) as $iPlaceId) {
774             $aResults[$iPlaceId] = new Result($iPlaceId);
775         }
776
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'";
788             } else {
789                 // Else look for housenumber with interpolationtype odd or all.
790                 $sSQL .= "interpolationtype='odd'";
791             }
792             $sSQL .= " or interpolationtype='all') and ";
793             $sSQL .= $iHousenumber.'>=startnumber and ';
794             $sSQL .= $iHousenumber.'<=endnumber';
795             $sSQL .= $this->oContext->excludeSQL(' AND place_id');
796
797             Debug::printSQL($sSQL);
798
799             foreach ($oDB->getCol($sSQL) as $iPlaceId) {
800                 $oResult = new Result($iPlaceId, Result::TABLE_OSMLINE);
801                 $oResult->iHouseNumber = $iHousenumber;
802                 $aResults[$iPlaceId] = $oResult;
803             }
804         }
805
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'";
812             } else {
813                 $sSQL .= "interpolationtype='odd'";
814             }
815             $sSQL .= " or interpolationtype='all') and ";
816             $sSQL .= $iHousenumber.'>=startnumber and ';
817             $sSQL .= $iHousenumber.'<=endnumber';
818             $sSQL .= $this->oContext->excludeSQL(' AND place_id');
819
820             Debug::printSQL($sSQL);
821
822             foreach ($oDB->getCol($sSQL) as $iPlaceId) {
823                 $oResult = new Result($iPlaceId, Result::TABLE_TIGER);
824                 $oResult->iHouseNumber = $iHousenumber;
825                 $aResults[$iPlaceId] = $oResult;
826             }
827         }
828
829         return $aResults;
830     }
831
832
833     private function queryPoiByOperator(&$oDB, $aParentIDs, $iLimit)
834     {
835         $aResults = array();
836         $sPlaceIDs = Result::joinIdsByTable($aParentIDs, Result::TABLE_PLACEX);
837
838         if (!$sPlaceIDs) {
839             return $aResults;
840         }
841
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";
853
854             Debug::printSQL($sSQL);
855
856             foreach ($oDB->getCol($sSQL) as $iPlaceId) {
857                 $aResults[$iPlaceId] = new Result($iPlaceId);
858             }
859         }
860
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);
865
866             $sSQL = "SELECT min(rank_search) FROM placex WHERE place_id in ($sPlaceIDs)";
867             Debug::printSQL($sSQL);
868             $iMaxRank = (int) $oDB->getOne($sSQL);
869
870             // For state / country level searches the normal radius search doesn't work very well
871             $sPlaceGeom = false;
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 ';
879                 $sSQL .= ' LIMIT 1';
880                 Debug::printSQL($sSQL);
881                 $sPlaceGeom = $oDB->getOne($sSQL);
882             }
883
884             if ($sPlaceGeom) {
885                 $sPlaceIDs = false;
886             } else {
887                 $iMaxRank += 5;
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);
893             }
894
895             if ($sPlaceIDs || $sPlaceGeom) {
896                 $fRange = 0.01;
897                 if ($bCacheTable) {
898                     // More efficient - can make the range bigger
899                     $fRange = 0.05;
900
901                     $sOrderBySQL = '';
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)";
908                     }
909
910                     $sSQL = 'SELECT distinct i.place_id';
911                     if ($sOrderBySQL) {
912                         $sSQL .= ', i.order_term';
913                     }
914                     $sSQL .= ' from (SELECT l.place_id';
915                     if ($sOrderBySQL) {
916                         $sSQL .= ','.$sOrderBySQL.' as order_term';
917                     }
918                     $sSQL .= ' from '.$sClassTable.' as l';
919
920                     if ($sPlaceIDs) {
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)";
926                     }
927
928                     $sSQL .= $this->oContext->excludeSQL(' AND l.place_id');
929                     $sSQL .= 'limit 300) i ';
930                     if ($sOrderBySQL) {
931                         $sSQL .= 'order by order_term asc';
932                     }
933                     $sSQL .= " limit $iLimit";
934
935                     Debug::printSQL($sSQL);
936
937                     foreach ($oDB->getCol($sSQL) as $iPlaceId) {
938                         $aResults[$iPlaceId] = new Result($iPlaceId);
939                     }
940                 } else {
941                     if ($this->oContext->hasNearPoint()) {
942                         $fRange = $this->oContext->nearRadius();
943                     }
944
945                     $sOrderBySQL = '';
946                     if ($this->oContext->hasNearPoint()) {
947                         $sOrderBySQL = $this->oContext->distanceSQL('l.geometry');
948                     } else {
949                         $sOrderBySQL = 'ST_Distance(l.geometry, f.geometry)';
950                     }
951
952                     $sSQL = 'SELECT distinct l.place_id';
953                     if ($sOrderBySQL) {
954                         $sSQL .= ','.$sOrderBySQL.' as orderterm';
955                     }
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');
962                     if ($sOrderBySQL) {
963                         $sSQL .= 'ORDER BY orderterm ASC';
964                     }
965                     $sSQL .= " limit $iLimit";
966
967                     Debug::printSQL($sSQL);
968
969                     foreach ($oDB->getCol($sSQL) as $iPlaceId) {
970                         $aResults[$iPlaceId] = new Result($iPlaceId);
971                     }
972                 }
973             }
974         }
975
976         return $aResults;
977     }
978
979     private function poiTable()
980     {
981         return 'place_classtype_'.$this->sClass.'_'.$this->sType;
982     }
983
984     private function countryCodeSQL($sVar)
985     {
986         if ($this->sCountryCode) {
987             return $sVar.' = \''.$this->sCountryCode."'";
988         }
989         if ($this->oContext->sqlCountryList) {
990             return $sVar.' in '.$this->oContext->sqlCountryList;
991         }
992
993         return '';
994     }
995
996     /////////// Sort functions
997
998
999     public static function bySearchRank($a, $b)
1000     {
1001         if ($a->iSearchRank == $b->iSearchRank) {
1002             return $a->iOperator + strlen($a->sHouseNumber)
1003                      - $b->iOperator - strlen($b->sHouseNumber);
1004         }
1005
1006         return $a->iSearchRank < $b->iSearchRank ? -1 : 1;
1007     }
1008
1009     //////////// Debugging functions
1010
1011
1012     public function debugInfo()
1013     {
1014         return array(
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
1027                );
1028     }
1029
1030     public function dumpAsHtmlTableRow(&$aWordIDs)
1031     {
1032         $kf = function ($k) use (&$aWordIDs) {
1033             return $aWordIDs[$k] ?? '['.$k.']';
1034         };
1035
1036         echo '<tr>';
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>';
1048
1049         echo '</tr>';
1050     }
1051 }