diff --git a/query/search.js b/query/search.js index 009cdb2f..35f0acef 100644 --- a/query/search.js +++ b/query/search.js @@ -33,23 +33,34 @@ function generate( params ){ } }; + /** + * If the query contained an administrative region name, boost results that + * contain a matching admin value, assigning higher values to higher admin + * levels (admin0 carries more weight than locality, for instance). + */ if (params.input_admin) { - var admin_fields = get_layers(['admin','admin1_abbr','alpha3']); - query.query.filtered.query.bool.should = []; - - admin_fields.forEach(function(admin_field) { - var match = {}; - match[admin_field] = params.input_admin; - query.query.filtered.query.bool.should.push({ - 'match': match - }); + var adminFieldWeights = { + admin0: 20, + alpha3: 20, + admin1: 10, + admin1_abbr: 10, + admin2: 5, + locality: 1, + local_admin: 1 + }; + query.query.filtered.query.bool.should = Object.keys( adminFieldWeights ).map( function ( field ){ + var boostFactor = adminFieldWeights[ field ]; + var shouldClause = { match: { } }; + shouldClause.match[ field ] = { + query: params.input_admin, + boost: boostFactor + }; + return shouldClause; }); } query.sort = query.sort.concat(sort); - // console.log( JSON.stringify( query, null, 2 ) ); - return query; } -module.exports = generate; \ No newline at end of file +module.exports = generate; diff --git a/test/unit/query/search.js b/test/unit/query/search.js index c7d3428a..1732f2bb 100644 --- a/test/unit/query/search.js +++ b/test/unit/query/search.js @@ -238,42 +238,58 @@ module.exports.tests.query = function(test, common) { 'should': [ { 'match': { - 'admin1_abbr': 'usa' - } - }, - { - 'match': { - 'alpha3': 'usa' + 'admin0': { + 'query': 'usa', + 'boost': 20 + } } }, { 'match': { - 'admin0': 'usa' + 'alpha3': { + 'query': 'usa', + 'boost': 20 + } } }, { 'match': { - 'admin1': 'usa' + 'admin1': { + 'query': 'usa', + 'boost': 10 + } } }, { 'match': { - 'admin2': 'usa' + 'admin1_abbr': { + 'query': 'usa', + 'boost': 10 + } } }, { 'match': { - 'neighborhood': 'usa' + 'admin2': { + 'query': 'usa', + 'boost': 5 + } } }, { 'match': { - 'locality': 'usa' + 'locality': { + 'query': 'usa', + 'boost': 1 + } } }, { 'match': { - 'local_admin': 'usa' + 'local_admin': { + 'query': 'usa', + 'boost': 1 + } } } ] @@ -330,4 +346,4 @@ module.exports.all = function (tape, common) { for( var testCase in module.exports.tests ){ module.exports.tests[testCase](test, common); } -}; \ No newline at end of file +};