mirror of https://github.com/pelias/api.git
Browse Source
This adds support for custom boosts to the addressit-style search queries. The newer libpostal based queries do not include this functionality since they can only query for addresses.cb_sorting
Julian Simioni
6 years ago
4 changed files with 170 additions and 0 deletions
@ -0,0 +1,107 @@
|
||||
{ |
||||
"type": "original", |
||||
"body": { |
||||
"query": { |
||||
"bool": { |
||||
"must": [{ |
||||
"match": { |
||||
"name.default": { |
||||
"query": "test", |
||||
"boost": 1, |
||||
"analyzer": "peliasQueryFullToken" |
||||
} |
||||
} |
||||
}], |
||||
"should": [{ |
||||
"match": { |
||||
"phrase.default": { |
||||
"query": "test", |
||||
"analyzer": "peliasPhrase", |
||||
"type": "phrase", |
||||
"boost": 1, |
||||
"slop": 2 |
||||
} |
||||
} |
||||
},{ |
||||
"function_score": { |
||||
"query": { |
||||
"match": { |
||||
"phrase.default": { |
||||
"query": "test", |
||||
"analyzer": "peliasPhrase", |
||||
"type": "phrase", |
||||
"slop": 2, |
||||
"boost": 1 |
||||
} |
||||
} |
||||
}, |
||||
"max_boost": 20, |
||||
"score_mode": "first", |
||||
"boost_mode": "replace", |
||||
"functions": [{ |
||||
"field_value_factor": { |
||||
"modifier": "log1p", |
||||
"field": "popularity", |
||||
"missing": 1 |
||||
}, |
||||
"weight": 1 |
||||
}] |
||||
} |
||||
},{ |
||||
"function_score": { |
||||
"query": { |
||||
"match": { |
||||
"phrase.default": { |
||||
"query": "test", |
||||
"analyzer": "peliasPhrase", |
||||
"type": "phrase", |
||||
"slop": 2, |
||||
"boost": 1 |
||||
} |
||||
} |
||||
}, |
||||
"max_boost": 20, |
||||
"score_mode": "first", |
||||
"boost_mode": "replace", |
||||
"functions": [{ |
||||
"field_value_factor": { |
||||
"modifier": "log1p", |
||||
"field": "population", |
||||
"missing": 1 |
||||
}, |
||||
"weight": 2 |
||||
}] |
||||
} |
||||
}, { |
||||
"bool": { |
||||
"should": [ |
||||
{ |
||||
"constant_score": { |
||||
"boost": 5, |
||||
"query": { |
||||
"term": { |
||||
"source": "openstreetmap" |
||||
} |
||||
} |
||||
} |
||||
}, |
||||
{ |
||||
"constant_score": { |
||||
"boost": 3, |
||||
"query": { |
||||
"term": { |
||||
"layer": "transit" |
||||
} |
||||
} |
||||
} |
||||
} |
||||
] |
||||
} |
||||
}] |
||||
} |
||||
}, |
||||
"sort": [ "_score" ], |
||||
"size": 10, |
||||
"track_scores": true |
||||
} |
||||
} |
@ -0,0 +1,55 @@
|
||||
const proxyquire = require('proxyquire'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.query = function(test, common) { |
||||
test('valid search with custom boosts', function(t) { |
||||
const clean = { |
||||
tokens: ['foo'], |
||||
tokens_complete: ['foo'], |
||||
tokens_incomplete: [], |
||||
text: 'test', |
||||
querySize: 10 |
||||
}; |
||||
|
||||
const config_with_boosts = { |
||||
generate: function() { |
||||
return { |
||||
api: { |
||||
customBoosts: { |
||||
source: { |
||||
openstreetmap: 5 |
||||
}, |
||||
layer: { |
||||
transit: 3 |
||||
} |
||||
} |
||||
} |
||||
}; |
||||
} |
||||
}; |
||||
|
||||
var expected_query = require('../fixture/search_with_custom_boosts.json'); |
||||
|
||||
const search_query_module = proxyquire('../../../query/search_original', { |
||||
'pelias-config': config_with_boosts |
||||
}); |
||||
|
||||
const actual_query = JSON.parse( JSON.stringify( search_query_module(clean) ) ); |
||||
console.log(JSON.stringify(actual_query.body.query.bool, null, 2)); |
||||
|
||||
t.deepEqual(actual_query, expected_query, 'query as expected'); |
||||
t.pass(); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
function test(name, testFunction) { |
||||
return tape('search with custom boosts query ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
Loading…
Reference in new issue