Skip to content

Commit 9ee7b3d

Browse files
missinglinkorangejulius
authored andcommitted
feat(address_search_using_ids): add an extra query clause to address queries which prioritises addresses with a matching postcode over others
1 parent 4d408dd commit 9ee7b3d

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

layout/AddressesUsingIdsQuery.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,42 @@ function createUnitAndAddressShould(vs) {
7070
return should;
7171
}
7272

73+
function createPostcodeAndAddressShould(vs) {
74+
const should = {
75+
bool: {
76+
_name: 'fallback.address',
77+
must: [
78+
{
79+
match_phrase: {
80+
'address_parts.zip': vs.var('input:postcode')
81+
}
82+
},
83+
{
84+
match_phrase: {
85+
'address_parts.number': vs.var('input:housenumber')
86+
}
87+
},
88+
{
89+
match_phrase: {
90+
'address_parts.street': vs.var('input:street')
91+
}
92+
}
93+
],
94+
filter: {
95+
term: {
96+
layer: 'address'
97+
}
98+
}
99+
}
100+
};
101+
102+
if (vs.isset('boost:address')) {
103+
should.bool.boost = vs.var('boost:address');
104+
}
105+
106+
return should;
107+
}
108+
73109
function createStreetShould(vs) {
74110
const should = {
75111
bool: {
@@ -127,6 +163,10 @@ class AddressesUsingIdsQuery extends Query {
127163
track_scores: vs.var('track_scores')
128164
};
129165

166+
// add unit/housenumber/street if available
167+
if (vs.isset('input:housenumber') && vs.isset('input:postcode')) {
168+
base.query.function_score.query.bool.should.push(createPostcodeAndAddressShould(vs));
169+
}
130170
// add unit/housenumber/street if available
131171
if (vs.isset('input:housenumber') && vs.isset('input:unit')) {
132172
base.query.function_score.query.bool.should.push(createUnitAndAddressShould(vs));

test/fixtures/addressesUsingIdsQuery/no_layers.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,33 @@
2222
}
2323
}
2424
},
25+
{
26+
"bool": {
27+
"_name": "fallback.address",
28+
"must": [
29+
{
30+
"match_phrase": {
31+
"address_parts.zip": "postcode value"
32+
}
33+
},
34+
{
35+
"match_phrase": {
36+
"address_parts.number": "housenumber value"
37+
}
38+
},
39+
{
40+
"match_phrase": {
41+
"address_parts.street": "street value"
42+
}
43+
}
44+
],
45+
"filter": {
46+
"term": {
47+
"layer": "address"
48+
}
49+
}
50+
}
51+
},
2552
{
2653
"bool": {
2754
"_name": "fallback.address",

test/layout/AddressesUsingIdsQuery.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports.tests.base_render = (test, common) => {
1212
vs.var('size', 'size value');
1313
vs.var('track_scores', 'track_scores value');
1414
vs.var('input:unit', 'unit value');
15+
vs.var('input:postcode', 'postcode value');
1516
vs.var('input:housenumber', 'housenumber value');
1617
vs.var('input:street', 'street value');
1718

@@ -133,6 +134,41 @@ module.exports.tests.base_render = (test, common) => {
133134
t.end();
134135

135136
});
137+
138+
test('VariableStore with housenumber and no postcode should not query on postcode, only housenumbers', (t) => {
139+
const query = new AddressesUsingIdsQuery();
140+
141+
const vs = new VariableStore();
142+
vs.var('size', 'size value');
143+
vs.var('track_scores', 'track_scores value');
144+
vs.var('input:housenumber', 'housenumber value');
145+
vs.var('input:street', 'street value');
146+
147+
const actual = query.render(vs);
148+
const expected = require('../fixtures/addressesUsingIdsQuery/housenumber_no_units.json');
149+
150+
// marshall/unmarshall to handle toString's internally
151+
t.deepEquals(JSON.parse(JSON.stringify(actual)), expected);
152+
t.end();
153+
154+
});
155+
test('VariableStore with postcode and no housenumber should neither query on the postcode nor the housenumber, only the street', (t) => {
156+
const query = new AddressesUsingIdsQuery();
157+
158+
const vs = new VariableStore();
159+
vs.var('size', 'size value');
160+
vs.var('track_scores', 'track_scores value');
161+
vs.var('input:postcode', 'postcode value');
162+
vs.var('input:street', 'street value');
163+
164+
const actual = query.render(vs);
165+
const expected = require('../fixtures/addressesUsingIdsQuery/unit_no_housenumber.json');
166+
167+
// marshall/unmarshall to handle toString's internally
168+
t.deepEquals(JSON.parse(JSON.stringify(actual)), expected);
169+
t.end();
170+
171+
});
136172
};
137173

138174
module.exports.tests.render_with_scores = (test, common) => {

0 commit comments

Comments
 (0)