3  * SPDX-License-Identifier: GPL-2.0-only
 
   5  * This file is part of Nominatim. (https://nominatim.org)
 
   7  * Copyright (C) 2022 by the Nominatim developer community.
 
   8  * For a full list of authors see the git log.
 
  14  * A single result of a search operation or a reverse lookup.
 
  16  * This object only contains the id of the result. It does not yet
 
  17  * have any details needed to format the output document.
 
  21     const TABLE_PLACEX = 0;
 
  22     const TABLE_POSTCODE = 1;
 
  23     const TABLE_OSMLINE = 2;
 
  24     const TABLE_TIGER = 3;
 
  26     /// Database table that contains the result.
 
  30     /// House number (only for interpolation results).
 
  31     public $iHouseNumber = -1;
 
  32     /// Number of exact matches in address (address searches only).
 
  33     public $iExactMatches = 0;
 
  34     /// Subranking within the results (the higher the worse).
 
  35     public $iResultRank = 0;
 
  36     /// Address rank of the result.
 
  39     public function debugInfo()
 
  42                 'Table' => $this->iTable,
 
  44                 'House number' => $this->iHouseNumber,
 
  45                 'Exact Matches' => $this->iExactMatches,
 
  46                 'Result rank' => $this->iResultRank
 
  51     public function __construct($sId, $iTable = Result::TABLE_PLACEX)
 
  53         $this->iTable = $iTable;
 
  54         $this->iId = (int) $sId;
 
  57     public static function joinIdsByTable($aResults, $iTable)
 
  59         return join(',', array_keys(array_filter(
 
  61             function ($aValue) use ($iTable) {
 
  62                 return $aValue->iTable == $iTable;
 
  67     public static function joinIdsByTableMinRank($aResults, $iTable, $iMinAddressRank)
 
  69         return join(',', array_keys(array_filter(
 
  71             function ($aValue) use ($iTable, $iMinAddressRank) {
 
  72                 return $aValue->iTable == $iTable && $aValue->iAddressRank >= $iMinAddressRank;
 
  77     public static function joinIdsByTableMaxRank($aResults, $iTable, $iMaxAddressRank)
 
  79         return join(',', array_keys(array_filter(
 
  81             function ($aValue) use ($iTable, $iMaxAddressRank) {
 
  82                 return $aValue->iTable == $iTable && $aValue->iAddressRank <= $iMaxAddressRank;
 
  87     public static function sqlHouseNumberTable($aResults, $iTable)
 
  91         foreach ($aResults as $oResult) {
 
  92             if ($oResult->iTable == $iTable) {
 
  93                 $sHousenumbers .= $sSep.'('.$oResult->iId.',';
 
  94                 $sHousenumbers .= $oResult->iHouseNumber.')';
 
  99         return $sHousenumbers;
 
 103      * Split a result array into highest ranked result and the rest
 
 105      * @param object[] $aResults List of results to split.
 
 109     public static function splitResults($aResults)
 
 115         foreach ($aResults as $oRes) {
 
 116             if ($oRes->iResultRank < $iMinRank) {
 
 118                 $aHead = array($oRes->iId => $oRes);
 
 119                 $iMinRank = $oRes->iResultRank;
 
 120             } elseif ($oRes->iResultRank == $iMinRank) {
 
 121                 $aHead[$oRes->iId] = $oRes;
 
 123                 $aTail[$oRes->iId] = $oRes;
 
 127         return array('head' => $aHead, 'tail' => $aTail);