Browse Source

Merge pull request #113 from pelias/boost-exact-matches#5

Boost exact matches#5
pull/131/head
Severyn Kozak 10 years ago
parent
commit
57d133b813
  1. 4
      query/reverse.js
  2. 4
      query/search.js
  3. 100
      query/sort.js
  4. 12
      test/unit/query/search.js
  5. 27
      test/unit/query/sort.js

4
query/reverse.js

@ -10,7 +10,7 @@ function generate( params ){
}; };
var query = queries.distance( centroid, { size: params.size || 1 } ); var query = queries.distance( centroid, { size: params.size || 1 } );
query.sort = query.sort.concat(sort); query.sort = query.sort.concat( sort( params ) );
if ( params.categories && params.categories.length > 0 ) { if ( params.categories && params.categories.length > 0 ) {
addCategoriesFilter( query, params.categories ); addCategoriesFilter( query, params.categories );
@ -25,4 +25,4 @@ function addCategoriesFilter( query, categories ) {
}); });
} }
module.exports = generate; module.exports = generate;

4
query/search.js

@ -45,9 +45,9 @@ function generate( params ){
}); });
} }
query.sort = query.sort.concat(sort); query.sort = query.sort.concat( sort( params ) );
return query; return query;
} }
module.exports = generate; module.exports = generate;

100
query/sort.js

@ -4,47 +4,65 @@ var popularity = 'popularity';
var category = 'category'; var category = 'category';
var category_weights = require('../helper/category_weights'); var category_weights = require('../helper/category_weights');
var weights = require('pelias-suggester-pipeline').weights; var weights = require('pelias-suggester-pipeline').weights;
var isObject = require( 'is-object' );
module.exports = [ module.exports = function( params ){
{ var scriptsConfig = [
'_script': { {
'file': admin_boost, '_script': {
'type': 'number', 'file': admin_boost,
'order': 'desc' 'type': 'number',
} 'order': 'desc'
}, }
{ },
'_script': { {
'file': population, '_script': {
'type': 'number', 'file': population,
'order': 'desc' 'type': 'number',
} 'order': 'desc'
}, }
{ },
'_script': { {
'file': popularity, '_script': {
'type': 'number', 'file': popularity,
'order': 'desc' 'type': 'number',
} 'order': 'desc'
}, }
{ },
'_script': { {
'params': { '_script': {
'category_weights': category_weights 'params': {
}, 'category_weights': category_weights
'file': category, },
'type': 'number', 'file': category,
'order': 'desc' 'type': 'number',
} 'order': 'desc'
}, }
{ },
'_script': { {
'params': { '_script': {
'weights': weights 'params': {
}, 'weights': weights
'file': 'weights', },
'type': 'number', 'file': 'weights',
'order': 'desc' 'type': 'number',
'order': 'desc'
}
} }
];
if( isObject( params ) && params.hasOwnProperty( 'input' ) ){
scriptsConfig.push({
'_script': {
'params': {
'input': params.input
},
'file': 'exact_match',
'type': 'number',
'order': 'desc'
}
});
} }
];
return scriptsConfig;
};

12
test/unit/query/search.js

@ -58,6 +58,16 @@ var sort = [
'type': 'number', 'type': 'number',
'order': 'desc' 'order': 'desc'
} }
},
{
'_script': {
'params': {
'input': 'test'
},
'file': 'exact_match',
'type': 'number',
'order': 'desc'
}
} }
]; ];
@ -239,4 +249,4 @@ module.exports.all = function (tape, common) {
for( var testCase in module.exports.tests ){ for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common); module.exports.tests[testCase](test, common);
} }
}; };

27
test/unit/query/sort.js

@ -11,7 +11,8 @@ module.exports.tests = {};
module.exports.tests.interface = function(test, common) { module.exports.tests.interface = function(test, common) {
test('valid interface', function(t) { test('valid interface', function(t) {
t.equal(typeof generate, 'object', 'valid object'); t.equal(typeof generate(), 'object', 'valid object');
t.equal(typeof generate({input: 'foobar'}), 'object', 'valid object');
t.end(); t.end();
}); });
}; };
@ -62,8 +63,26 @@ var expected = [
module.exports.tests.query = function(test, common) { module.exports.tests.query = function(test, common) {
test('valid part of query', function(t) { test('valid part of query', function(t) {
var sort = generate; t.deepEqual(generate(), expected, 'valid sort part of the query');
t.deepEqual(sort, expected, 'valid sort part of the query'); t.deepEqual(generate( {} ), expected, 'valid sort part of the query');
t.end();
});
};
module.exports.tests.queryWithInput = function ( test, common ){
test( 'Valid sort query component when input is present.', function ( t ){
var expectedWithInput = expected.slice();
expectedWithInput.push({
_script: {
params: {
input: 'foobar'
},
file: 'exact_match',
type: 'number',
order: 'desc'
}
});
t.deepEqual(generate({ input: 'foobar' }), expectedWithInput, 'valid sort part of the query');
t.end(); t.end();
}); });
}; };
@ -77,4 +96,4 @@ module.exports.all = function (tape, common) {
for( var testCase in module.exports.tests ){ for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common); module.exports.tests[testCase](test, common);
} }
}; };

Loading…
Cancel
Save