Skip to content

Commit 5fc15a6

Browse files
committed
fix(structured): handle housenumber-only queries
In the case where libpostal parses a query that it believes only has a housenumber, an 'empty' query was being generated. These take a long time to run.
1 parent 7ba9dc2 commit 5fc15a6

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

layout/StructuredFallbackQuery.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,41 @@ function addUnitAndHouseNumberAndStreet(vs) {
231231

232232
}
233233

234+
function addHouseNumber(vs) {
235+
var o = {
236+
bool: {
237+
_name: 'fallback.housenumber',
238+
must: [
239+
{
240+
match_phrase: {
241+
'address_parts.number': vs.var('input:housenumber').toString()
242+
}
243+
}
244+
],
245+
should: [],
246+
filter: {
247+
term: {
248+
layer: 'address'
249+
}
250+
}
251+
}
252+
};
253+
254+
if (vs.isset('boost:address')) {
255+
o.bool.boost = vs.var('boost:address');
256+
}
257+
258+
addSecPostCode(vs, o);
259+
addSecNeighbourhood(vs, o);
260+
addSecBorough(vs, o);
261+
addSecLocality(vs, o);
262+
addSecCounty(vs, o);
263+
addSecRegion(vs, o);
264+
addSecCountry(vs, o);
265+
266+
return o;
267+
}
268+
234269
function addHouseNumberAndStreet(vs) {
235270
var o = {
236271
bool: {
@@ -522,7 +557,10 @@ Layout.prototype.render = function( vs ){
522557
funcScoreShould.push(addHouseNumberAndStreet(vs));
523558
}
524559
funcScoreShould.push(addStreet(vs));
560+
} else if (vs.isset('input:housenumber')) {
561+
funcScoreShould.push(addHouseNumber(vs));
525562
}
563+
526564
if (vs.isset('input:postcode')) {
527565
funcScoreShould.push(addPostCode(vs));
528566
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"query": {
3+
"function_score": {
4+
"query": {
5+
"bool": {
6+
"minimum_should_match": 1,
7+
"should": [
8+
{
9+
"bool": {
10+
"_name": "fallback.housenumber",
11+
"boost": { "$": 19 },
12+
"must": [
13+
{
14+
"match_phrase": {
15+
"address_parts.number": "house number value"
16+
}
17+
}
18+
],
19+
"should": [],
20+
"filter": {
21+
"term": {
22+
"layer": "address"
23+
}
24+
}
25+
}
26+
}
27+
]
28+
}
29+
},
30+
"max_boost": 20,
31+
"functions": [],
32+
"score_mode": "avg",
33+
"boost_mode": "multiply"
34+
}
35+
},
36+
"size": { "$": "size value" },
37+
"track_scores": { "$": "track_scores value" },
38+
"sort": [
39+
"_score"
40+
]
41+
}

test/layout/StructuredFallbackQuery.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ module.exports.tests.base_render = function(test, common) {
8888

8989
});
9090

91+
test('VariableStore with only housenumber should create housenumber only query', function(t) {
92+
var query = new StructuredFallbackQuery();
93+
94+
var vs = new VariableStore();
95+
vs.var('size', 'size value');
96+
vs.var('input:housenumber', 'house number value');
97+
vs.var('track_scores', 'track_scores value');
98+
vs.var('boost:address', 19);
99+
100+
var actual = query.render(vs);
101+
var expected = require('../fixtures/structuredFallbackQuery/housenumber.json');
102+
103+
t.deepEquals(actual, expected);
104+
t.end();
105+
});
106+
91107
test('input:postcode set should include it at the address layer query', function(t) {
92108
var query = new StructuredFallbackQuery();
93109

0 commit comments

Comments
 (0)