Skip to content

Commit

Permalink
feat: Merge pull request #53 from pelias/add-boosts-to-structured-query
Browse files Browse the repository at this point in the history
added address/street boosts to structured query
  • Loading branch information
trescube authored Dec 2, 2016
2 parents c90460c + c183594 commit 7107a99
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 55 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports.layout = {
FilteredBooleanQuery: require('./layout/FilteredBooleanQuery'),
FallbackQuery: require('./layout/FallbackQuery'),
GeodisambiguationQuery: require('./layout/GeodisambiguationQuery'),
ComponentFallbackQuery: require('./layout/ComponentFallbackQuery')
StructuredFallbackQuery: require('./layout/StructuredFallbackQuery')
};

module.exports.view = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ function addHouseNumberAndStreet(vs) {
}
};

// THIS VALUE IS OPEN TO DEBATE AND CONFIGURATION
// it will probably go away once we get libpostal parsing the street field
o.bool.boost = 50;
if (vs.isset('boost:address')) {
o.bool.boost = vs.var('boost:address');
}

addSecPostCode(vs, o);
addSecNeighbourhood(vs, o);
Expand Down Expand Up @@ -245,9 +245,9 @@ function addStreet(vs) {
}
};

// THIS VALUE IS OPEN TO DEBATE AND CONFIGURATION
// it will probably go away once we get libpostal parsing the street field
o.bool.boost = 100;
if (vs.isset('boost:street')) {
o.bool.boost = vs.var('boost:street');
}

addSecPostCode(vs, o);
addSecNeighbourhood(vs, o);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{
"bool": {
"_name": "fallback.address",
"boost": { "$": 19 },
"must": [
{
"match_phrase": {
Expand Down Expand Up @@ -94,13 +95,13 @@
"term": {
"layer": "address"
}
},
"boost": 50
}
}
},
{
"bool": {
"_name": "fallback.street",
"boost": { "$": 17 },
"must": [
{
"match_phrase": {
Expand Down Expand Up @@ -181,8 +182,7 @@
"term": {
"layer": "street"
}
},
"boost": 100
}
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{
"bool": {
"_name": "fallback.address",
"boost": { "$": 19 },
"must": [
{
"match_phrase": {
Expand All @@ -32,13 +33,13 @@
"term": {
"layer": "address"
}
},
"boost": 50
}
}
},
{
"bool": {
"_name": "fallback.street",
"boost": { "$": 17 },
"must": [
{
"match_phrase": {
Expand All @@ -57,8 +58,7 @@
"term": {
"layer": "street"
}
},
"boost": 100
}
}
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
var ComponentFallbackQuery = require('../../layout/ComponentFallbackQuery');
var StructuredFallbackQuery = require('../../layout/StructuredFallbackQuery');
var VariableStore = require('../../lib/VariableStore');
var diff = require('deep-diff').diff;

module.exports.tests = {};

module.exports.tests.base_render = function(test, common) {
test('instance with nothing set should render to base request', function(t) {
var query = new ComponentFallbackQuery();
var query = new StructuredFallbackQuery();

var vs = new VariableStore();
vs.var('size', 'size value');
Expand All @@ -21,7 +21,7 @@ module.exports.tests.base_render = function(test, common) {
});

test('VariableStore with neighbourhood-only should only include neighbourhood parts and no fallbacks', function(t) {
var query = new ComponentFallbackQuery();
var query = new StructuredFallbackQuery();

var vs = new VariableStore();
vs.var('size', 'size value');
Expand All @@ -37,7 +37,7 @@ module.exports.tests.base_render = function(test, common) {
});

test('VariableStore with address and less granular fields should include all others', function(t) {
var query = new ComponentFallbackQuery();
var query = new StructuredFallbackQuery();

var vs = new VariableStore();
vs.var('size', 'size value');
Expand All @@ -50,51 +50,48 @@ module.exports.tests.base_render = function(test, common) {
vs.var('input:county', 'county value');
vs.var('input:region', 'region value');
vs.var('input:country', 'country value');
vs.var('boost:address', 19);
vs.var('boost:street', 17);

var actual = query.render(vs);
var expected = require('../fixtures/componentFallbackQuery/address.json');
var expected = require('../fixtures/structuredFallbackQuery/address.json');

t.deepEquals(actual, expected);
t.end();

});

test('input:postcode set should include it at the address layer query', function(t) {
var query = new ComponentFallbackQuery();
var query = new StructuredFallbackQuery();

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

var fs = require('fs');
vs.var('boost:address', 19);
vs.var('boost:street', 17);

var actual = query.render(vs);
var expected = require('../fixtures/componentFallbackQuery/address_with_postcode.json');
var expected = require('../fixtures/structuredFallbackQuery/address_with_postcode.json');

t.deepEquals(actual, expected);
t.end();

});

test('vs with locality but not borough should add borough check', function(t) {
var query = new ComponentFallbackQuery();
var query = new StructuredFallbackQuery();

var vs = new VariableStore();
vs.var('size', 'size value');
vs.var('track_scores', 'track_scores value');
vs.var('input:locality', 'locality value');
vs.var('input:region', 'region value');

var fs = require('fs');

var actual = query.render(vs);
var expected = require('../fixtures/componentFallbackQuery/locality_as_borough.json');

// var fs = require('fs');
// fs.writeFileSync('componentFallbackQuery_address_with_postcode.json', JSON.stringify(actual, null, 2));
var expected = require('../fixtures/structuredFallbackQuery/locality_as_borough.json');

t.deepEquals(actual, expected);
t.end();
Expand All @@ -103,31 +100,31 @@ module.exports.tests.base_render = function(test, common) {

};

// module.exports.tests.boosts = function(test, common) {
// test('boost:street and boost:address missing from vs should not be include empty string', function(t) {
// var query = new ComponentFallbackQuery();
//
// var vs = new VariableStore();
// vs.var('size', 'size value');
// vs.var('track_scores', 'track_scores value');
// vs.var('input:housenumber', 'house number value');
// vs.var('input:street', 'street value');
//
// var actual = query.render(vs);
//
// t.false(actual.query.function_score.query.filtered.query.bool.should[0].bool.hasOwnProperty('boost'));
// t.false(actual.query.function_score.query.filtered.query.bool.should[1].bool.hasOwnProperty('boost'));
// t.end();
//
// });
//
// };
//
module.exports.tests.boosts = function(test, common) {
test('boost:street and boost:address missing from vs should not be include empty string', function(t) {
var query = new StructuredFallbackQuery();

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

var actual = query.render(vs);

t.false(actual.query.function_score.query.filtered.query.bool.should[0].bool.hasOwnProperty('boost'));
t.false(actual.query.function_score.query.filtered.query.bool.should[1].bool.hasOwnProperty('boost'));
t.end();

});

};

module.exports.tests.scores = function(test, common) {
test('scores rendering to falsy values should not be added', function(t) {
var score_views_called = 0;

var query = new ComponentFallbackQuery();
var query = new StructuredFallbackQuery();

[
{ 'score field 1': 'score value 1' },
Expand Down Expand Up @@ -174,7 +171,7 @@ module.exports.tests.filter = function(test, common) {
// guarantee that it was actually passed
var filter_views_called = 0;

var query = new ComponentFallbackQuery();
var query = new StructuredFallbackQuery();

[
{ 'filter field 1': 'filter value 1' },
Expand Down Expand Up @@ -210,7 +207,7 @@ module.exports.tests.filter = function(test, common) {

module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('ComponentFallbackQuery ' + name, testFunction);
return tape('StructuredFallbackQuery ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
Expand Down
2 changes: 1 addition & 1 deletion test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var tests = [
require('./layout/GeodisambiguationQuery.js'),
require('./layout/FallbackQuery.js'),
require('./layout/FilteredBooleanQuery.js'),
require('./layout/ComponentFallbackQuery.js'),
require('./layout/StructuredFallbackQuery.js'),
require('./lib/Variable.js'),
require('./lib/VariableStore.js'),
require('./view/address.js'),
Expand Down

0 comments on commit 7107a99

Please sign in to comment.