forked from hans/Nominatim
tweak postcode search
Give a preference to left-right reading, i.e <postcode>,<address> prefers a postcode search while <address>,<postcode> rather does an address search. Also exclude non-addressables, countries and state from results when a postcode is contained in the query.
This commit is contained in:
@@ -178,7 +178,7 @@ class HTMLLogger(BaseLogger):
|
||||
self._write(f"rank={res.rank_address}, ")
|
||||
self._write(f"osm={format_osm(res.osm_object)}, ")
|
||||
self._write(f'cc={res.country_code}, ')
|
||||
self._write(f'importance={res.importance or -1:.5f})</dd>')
|
||||
self._write(f'importance={res.importance or float("nan"):.5f})</dd>')
|
||||
total += 1
|
||||
self._write(f'</dl><b>TOTAL:</b> {total}</p>')
|
||||
|
||||
|
||||
@@ -141,12 +141,14 @@ class SearchBuilder:
|
||||
yield dbs.CountrySearch(sdata)
|
||||
|
||||
if sdata.postcodes and (is_category or self.configured_for_postcode):
|
||||
penalty = 0.0 if sdata.countries else 0.1
|
||||
if address:
|
||||
sdata.lookups = [dbf.FieldLookup('nameaddress_vector',
|
||||
[t.token for r in address
|
||||
for t in self.query.get_partials_list(r)],
|
||||
'restrict')]
|
||||
yield dbs.PostcodeSearch(0.4, sdata)
|
||||
penalty += 0.2
|
||||
yield dbs.PostcodeSearch(penalty, sdata)
|
||||
|
||||
|
||||
def build_housenumber_search(self, sdata: dbf.SearchData, hnrs: List[Token],
|
||||
|
||||
@@ -562,6 +562,8 @@ class PlaceSearch(AbstractSearch):
|
||||
sql = sql.where(tsearch.c.country_code.in_(self.countries.values))
|
||||
|
||||
if self.postcodes:
|
||||
# if a postcode is given, don't search for state or country level objects
|
||||
sql = sql.where(tsearch.c.address_rank > 9)
|
||||
tpc = conn.t.postcode
|
||||
if self.expected_count > 1000:
|
||||
# Many results expected. Restrict by postcode.
|
||||
|
||||
@@ -270,7 +270,12 @@ class _TokenSequence:
|
||||
if (base.postcode.start == 0 and self.direction != -1)\
|
||||
or (base.postcode.end == query.num_token_slots() and self.direction != 1):
|
||||
log().comment('postcode search')
|
||||
yield dataclasses.replace(base, penalty=self.penalty)
|
||||
# <address>,<postcode> should give preference to address search
|
||||
if base.postcode.start == 0:
|
||||
penalty = self.penalty
|
||||
else:
|
||||
penalty = self.penalty + 0.1
|
||||
yield dataclasses.replace(base, penalty=penalty)
|
||||
|
||||
# Postcode or country-only search
|
||||
if not base.address:
|
||||
@@ -278,6 +283,9 @@ class _TokenSequence:
|
||||
log().comment('postcode/country search')
|
||||
yield dataclasses.replace(base, penalty=self.penalty)
|
||||
else:
|
||||
# <postcode>,<address> should give preference to postcode search
|
||||
if base.postcode and base.postcode.start == 0:
|
||||
self.penalty += 0.1
|
||||
# Use entire first word as name
|
||||
if self.direction != -1:
|
||||
log().comment('first word = name')
|
||||
|
||||
@@ -253,7 +253,7 @@ def test_postcode_with_designation():
|
||||
(BreakType.PHRASE, PhraseType.NONE, [(2, TokenType.PARTIAL)]))
|
||||
|
||||
check_assignments(yield_token_assignments(q),
|
||||
TokenAssignment(name=TokenRange(1, 2),
|
||||
TokenAssignment(penalty=0.1, name=TokenRange(1, 2),
|
||||
postcode=TokenRange(0, 1)),
|
||||
TokenAssignment(postcode=TokenRange(0, 1),
|
||||
address=[TokenRange(1, 2)]))
|
||||
@@ -266,7 +266,7 @@ def test_postcode_with_designation_backwards():
|
||||
check_assignments(yield_token_assignments(q),
|
||||
TokenAssignment(name=TokenRange(0, 1),
|
||||
postcode=TokenRange(1, 2)),
|
||||
TokenAssignment(postcode=TokenRange(1, 2),
|
||||
TokenAssignment(penalty=0.1, postcode=TokenRange(1, 2),
|
||||
address=[TokenRange(0, 1)]))
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user