From: Sarah Hoffmann Date: Fri, 13 Oct 2017 21:04:12 +0000 (+0200) Subject: move word recheck into token collection X-Git-Tag: v3.1.0~43^2~2 X-Git-Url: https://git.openstreetmap.org/nominatim.git/commitdiff_plain/00265af528652d6c1bb32cf0694d71e8c5603f39 move word recheck into token collection Drop tokens for special and postcode searches already when collecting them for ValidTokens when they cannot be found in the normalized query. --- diff --git a/lib/Geocode.php b/lib/Geocode.php index faf9e770..f7f97593 100644 --- a/lib/Geocode.php +++ b/lib/Geocode.php @@ -669,7 +669,7 @@ class Geocode return $aSearchResults; } - public function getGroupedSearches($aSearches, $aPhrases, $aValidTokens, $bIsStructured, $sNormQuery) + public function getGroupedSearches($aSearches, $aPhrases, $aValidTokens, $bIsStructured) { /* Calculate all searches using aValidTokens i.e. @@ -707,17 +707,8 @@ class Geocode // If the token is valid if (isset($aValidTokens[' '.$sToken])) { foreach ($aValidTokens[' '.$sToken] as $aSearchTerm) { - // Recheck if the original word shows up in the query. - $bWordInQuery = false; - if (isset($aSearchTerm['word']) && $aSearchTerm['word']) { - $bWordInQuery = strpos( - $sNormQuery, - $this->normTerm($aSearchTerm['word']) - ) !== false; - } $aNewSearches = $oCurrentSearch->extendWithFullTerm( $aSearchTerm, - $bWordInQuery, isset($aValidTokens[$sToken]) && strpos($sToken, ' ') === false, $sPhraseType, @@ -999,6 +990,14 @@ class Geocode continue; } + // Special terms need to appear in their normalized form. + if ($aToken['word'] && $aToken['class']) { + $sNormWord = $this->normTerm($aToken['word']); + if (strpos($sNormQuery, $sNormWord) === false) { + continue; + } + } + if (isset($aValidTokens[$aToken['word_token']])) { $aValidTokens[$aToken['word_token']][] = $aToken; } else { @@ -1035,7 +1034,7 @@ class Geocode // Any words that have failed completely? // TODO: suggestions - $aGroupedSearches = $this->getGroupedSearches($aSearches, $aPhrases, $aValidTokens, $bStructuredPhrases, $sNormQuery); + $aGroupedSearches = $this->getGroupedSearches($aSearches, $aPhrases, $aValidTokens, $bStructuredPhrases); if ($this->bReverseInPlan) { // Reverse phrase array and also reverse the order of the wordsets in @@ -1046,7 +1045,7 @@ class Geocode if (sizeof($aPhrases) > 1) { $aPhrases[sizeof($aPhrases)-1]->invertWordSets(); } - $aReverseGroupedSearches = $this->getGroupedSearches($aSearches, $aPhrases, $aValidTokens, false, $sNormQuery); + $aReverseGroupedSearches = $this->getGroupedSearches($aSearches, $aPhrases, $aValidTokens, false); foreach ($aGroupedSearches as $aSearches) { foreach ($aSearches as $aSearch) { diff --git a/lib/SearchDescription.php b/lib/SearchDescription.php index ea7c96ef..1325a463 100644 --- a/lib/SearchDescription.php +++ b/lib/SearchDescription.php @@ -178,8 +178,6 @@ class SearchDescription * Derive new searches by adding a full term to the existing search. * * @param mixed[] $aSearchTerm Description of the token. - * @param bool $bWordInQuery True, if the normalised version of the word - * is contained in the query. * @param bool $bHasPartial True if there are also tokens of partial terms * with the same name. * @param string $sPhraseType Type of phrase the token is contained in. @@ -193,7 +191,7 @@ class SearchDescription * * @return SearchDescription[] List of derived search descriptions. */ - public function extendWithFullTerm($aSearchTerm, $bWordInQuery, $bHasPartial, $sPhraseType, $bFirstToken, $bFirstPhrase, $bLastToken, &$iGlobalRank) + public function extendWithFullTerm($aSearchTerm, $bHasPartial, $sPhraseType, $bFirstToken, $bFirstPhrase, $bLastToken, &$iGlobalRank) { $aNewSearches = array(); @@ -224,7 +222,8 @@ class SearchDescription // We need to try the case where the postal code is the primary element // (i.e. no way to tell if it is (postalcode, city) OR (city, postalcode) // so try both. - if (!$this->sPostcode && $bWordInQuery + if (!$this->sPostcode + && $aSearchTerm['word'] && pg_escape_string($aSearchTerm['word']) == $aSearchTerm['word'] ) { // If we have structured search or this is the first term, @@ -273,16 +272,8 @@ class SearchDescription } $aNewSearches[] = $oSearch; } - } elseif ($sPhraseType == '' - && $aSearchTerm['class'] !== '' && $aSearchTerm['class'] !== null - ) { - // require a normalized exact match of the term - // if we have the normalizer version of the query - // available - if ($this->iOperator == Operator::NONE - && (isset($aSearchTerm['word']) && $aSearchTerm['word']) - && $bWordInQuery - ) { + } elseif ($sPhraseType == '' && $aSearchTerm['class']) { + if ($this->iOperator == Operator::NONE) { $oSearch = clone $this; $oSearch->iSearchRank++;