Skip to content

Commit

Permalink
Merge pull request #91 from pelias/address_search_using_ids
Browse files Browse the repository at this point in the history
improved sorting for addresses with matching postcode
  • Loading branch information
orangejulius committed Nov 2, 2018
2 parents 4d408dd + 9ee7b3d commit 7ba9dc2
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
40 changes: 40 additions & 0 deletions layout/AddressesUsingIdsQuery.js
Expand Up @@ -70,6 +70,42 @@ function createUnitAndAddressShould(vs) {
return should;
}

function createPostcodeAndAddressShould(vs) {
const should = {
bool: {
_name: 'fallback.address',
must: [
{
match_phrase: {
'address_parts.zip': vs.var('input:postcode')
}
},
{
match_phrase: {
'address_parts.number': vs.var('input:housenumber')
}
},
{
match_phrase: {
'address_parts.street': vs.var('input:street')
}
}
],
filter: {
term: {
layer: 'address'
}
}
}
};

if (vs.isset('boost:address')) {
should.bool.boost = vs.var('boost:address');
}

return should;
}

function createStreetShould(vs) {
const should = {
bool: {
Expand Down Expand Up @@ -127,6 +163,10 @@ class AddressesUsingIdsQuery extends Query {
track_scores: vs.var('track_scores')
};

// add unit/housenumber/street if available
if (vs.isset('input:housenumber') && vs.isset('input:postcode')) {
base.query.function_score.query.bool.should.push(createPostcodeAndAddressShould(vs));
}
// add unit/housenumber/street if available
if (vs.isset('input:housenumber') && vs.isset('input:unit')) {
base.query.function_score.query.bool.should.push(createUnitAndAddressShould(vs));
Expand Down
27 changes: 27 additions & 0 deletions test/fixtures/addressesUsingIdsQuery/no_layers.json
Expand Up @@ -22,6 +22,33 @@
}
}
},
{
"bool": {
"_name": "fallback.address",
"must": [
{
"match_phrase": {
"address_parts.zip": "postcode value"
}
},
{
"match_phrase": {
"address_parts.number": "housenumber value"
}
},
{
"match_phrase": {
"address_parts.street": "street value"
}
}
],
"filter": {
"term": {
"layer": "address"
}
}
}
},
{
"bool": {
"_name": "fallback.address",
Expand Down
36 changes: 36 additions & 0 deletions test/layout/AddressesUsingIdsQuery.js
Expand Up @@ -12,6 +12,7 @@ module.exports.tests.base_render = (test, common) => {
vs.var('size', 'size value');
vs.var('track_scores', 'track_scores value');
vs.var('input:unit', 'unit value');
vs.var('input:postcode', 'postcode value');
vs.var('input:housenumber', 'housenumber value');
vs.var('input:street', 'street value');

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

});

test('VariableStore with housenumber and no postcode should not query on postcode, only housenumbers', (t) => {
const query = new AddressesUsingIdsQuery();

const vs = new VariableStore();
vs.var('size', 'size value');
vs.var('track_scores', 'track_scores value');
vs.var('input:housenumber', 'housenumber value');
vs.var('input:street', 'street value');

const actual = query.render(vs);
const expected = require('../fixtures/addressesUsingIdsQuery/housenumber_no_units.json');

// marshall/unmarshall to handle toString's internally
t.deepEquals(JSON.parse(JSON.stringify(actual)), expected);
t.end();

});
test('VariableStore with postcode and no housenumber should neither query on the postcode nor the housenumber, only the street', (t) => {
const query = new AddressesUsingIdsQuery();

const vs = new VariableStore();
vs.var('size', 'size value');
vs.var('track_scores', 'track_scores value');
vs.var('input:postcode', 'postcode value');
vs.var('input:street', 'street value');

const actual = query.render(vs);
const expected = require('../fixtures/addressesUsingIdsQuery/unit_no_housenumber.json');

// marshall/unmarshall to handle toString's internally
t.deepEquals(JSON.parse(JSON.stringify(actual)), expected);
t.end();

});
};

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

0 comments on commit 7ba9dc2

Please sign in to comment.