Skip to content

Commit

Permalink
Add support for housenumbers payload (fix #134 #133)
Browse files Browse the repository at this point in the history
  • Loading branch information
yohanboniface committed Dec 28, 2015
1 parent e52fa53 commit aca71f1
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 9 deletions.
5 changes: 4 additions & 1 deletion addok/config/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,13 @@
{'key': 'context'},
]


# Sometimes you only want to add some fields keeping the default ones.
EXTRA_FIELDS = []

# If you want to store extra fields with each housenumber. Those fields will
# not be searchable, but will be returned in the search result.
HOUSENUMBERS_PAYLOAD_FIELDS = []

# Weight of a document own importance:
IMPORTANCE_WEIGHT = 0.1

Expand Down
7 changes: 4 additions & 3 deletions addok/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def match_housenumber(self, tokens):
name_tokens = self.name.split()
for original in originals:
if original in self.housenumbers:
raw, lat, lon, *_id = self.housenumbers[original].split('|')
raw, lat, lon, *extra = self.housenumbers[original].split('|')
if raw in name_tokens and originals.count(original) != 2:
# Consider that user is not requesting a housenumber if
# token is also in name (ex. rue du 8 mai), unless this
Expand All @@ -146,8 +146,9 @@ def match_housenumber(self, tokens):
self.lat = lat
self.lon = lon
self.type = 'housenumber'
if _id:
self.id = _id[0]
if extra:
extra = zip(config.HOUSENUMBERS_PAYLOAD_FIELDS, extra)
self._cache.update(extra)
break

@property
Expand Down
7 changes: 3 additions & 4 deletions addok/index_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,8 @@ def index_housenumbers(pipe, housenumbers, doc, key, tokens, update_ngrams):
to_index = {}
for number, point in housenumbers.items():
vals = [number, point['lat'], point['lon']]
_id = point.get('id')
if _id:
vals.append(_id)
for field in config.HOUSENUMBERS_PAYLOAD_FIELDS:
vals.append(point.get(field, ''))
val = '|'.join(map(str, vals))
for hn in preprocess_housenumber(number):
doc[housenumber_field_key(hn)] = val
Expand All @@ -151,7 +150,7 @@ def deindex_housenumbers(key, doc, tokens):
field = field.decode()
if not field.startswith('h|'):
continue
number, lat, lon, *_id = value.decode().split('|')
number, lat, lon, *extra = value.decode().split('|')
hn = field[2:]
for token in tokens:
k = '|'.join(['didx', hn, token])
Expand Down
6 changes: 6 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ filter means bigger index.

FILTERS = ["type", "postcode"]

#### HOUSENUMBERS_PAYLOAD_FIELDS (list of keys)
If you want to store extra fields with each payload. Those fields will not
be searchable, but will be returned in the search result.

HOUSENUMBERS_PAYLOAD_FIELDS = ['key1', 'key2']

#### LICENCE (string or dict)
The licence of the data returned by the API. Can be a simple string, or a dict.

Expand Down
24 changes: 24 additions & 0 deletions tests/test_index_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,30 @@ def test_index_housenumber_uses_housenumber_preprocessors():
assert index[b'h|1b'] == b'1 bis|48.325451|2.25651'


def test_index_should_join_housenumbers_payload_fields(config):
config.HOUSENUMBERS_PAYLOAD_FIELDS = ['key', 'one']
doc = {
'id': 'xxxx',
'type': 'street',
'name': 'rue des Lilas',
'city': 'Paris',
'lat': '49.32545',
'lon': '4.2565',
'housenumbers': {
'1 bis': {
'lat': '48.325451',
'lon': '2.25651',
'key': 'myvalue',
'thisone': 'no',
'one': 'two',
}
}
}
index_document(doc)
index = DB.hgetall('d|xxxx')
assert index[b'h|1b'] == b'1 bis|48.325451|2.25651|myvalue|two'


def test_allow_list_values():
doc = {
'id': 'xxxx',
Expand Down
34 changes: 33 additions & 1 deletion tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,18 @@ def test_housenumber_are_not_computed_if_another_type_is_asked(factory):
assert results[0].type == "street"


def test_housenumber_id_is_used_when_given(factory):
def test_housenumbers_payload_fields_are_exported(config, factory):
config.HOUSENUMBERS_PAYLOAD_FIELDS = ['key']
factory(name="rue de Paris", type="street", id="123",
housenumbers={'1': {'lat': '48.32', 'lon': '2.25', 'key': 'abc'}})
results = search("rue de paris")
assert results[0].key == ''
results = search("1 rue de paris")
assert results[0].key == 'abc'


def test_id_is_overwritten_when_given_in_housenumber_payload(config, factory):
config.HOUSENUMBERS_PAYLOAD_FIELDS = ['id']
factory(name="rue de Paris", type="street", id="123",
housenumbers={'1': {'lat': '48.325', 'lon': '2.256', 'id': 'abc'}})
results = search("rue de paris")
Expand All @@ -249,6 +260,27 @@ def test_housenumber_id_is_used_when_given(factory):
assert results[0].id == 'abc'


def test_postcode_is_overwritten_when_in_housenumber_payload(config, factory):
config.HOUSENUMBERS_PAYLOAD_FIELDS = ['postcode']
factory(name="rue de Paris", type="street", id="123", postcode="12345",
housenumbers={'1': {'lat': '48.325', 'lon': '2.256',
'postcode': '54321'}})
results = search("rue de paris")
assert results[0].postcode == '12345'
results = search("1 rue de paris")
assert results[0].postcode == '54321'


def test_unknown_key_in_housenumber_payload_does_not_fail(config, factory):
config.HOUSENUMBERS_PAYLOAD_FIELDS = ['xxxyyy']
factory(name="rue de Paris", type="street", id="123", postcode="12345",
housenumbers={'1': {'lat': '48.325', 'lon': '2.256'}})
results = search("rue de paris")
assert results[0].id == '123'
results = search("1 rue de paris")
assert results[0].id == '123'


def test_from_id(factory):
factory(name="avenue de Paris", type="street", id="123")
doc = Result.from_id("123")
Expand Down

0 comments on commit aca71f1

Please sign in to comment.