Browse Source

Merge pull request #67 from pelias/score-function

Score function
pull/72/head 1.1.4
Harish Krishna 10 years ago
parent
commit
218218c7b4
  1. 1
      controller/search.js
  2. 3
      package.json
  3. 8
      query/reverse.js
  4. 6
      query/search.js
  5. 22
      query/sort.js
  6. 4
      test/unit/controller/search.js
  7. 28
      test/unit/query/reverse.js
  8. 44
      test/unit/query/search.js
  9. 52
      test/unit/query/sort.js
  10. 1
      test/unit/run.js

1
controller/search.js

@ -13,6 +13,7 @@ function setup( backend, query ){
// backend command
var cmd = {
index: 'pelias',
searchType: 'dfs_query_then_fetch',
body: query( req.clean )
};

3
package.json

@ -36,7 +36,8 @@
"express": "^4.8.8",
"geojson": "^0.2.0",
"geojson-extent": "^0.3.1",
"geopipes-elasticsearch-backend": "git://github.com/geopipes/elasticsearch-backend#centroid-optional",
"geopipes-elasticsearch-backend": "0.0.11",
"pelias-suggester-pipeline": "2.0.2",
"is-object": "^1.0.1",
"pelias-esclient": "0.0.25",
"toobusy": "^0.2.4"

8
query/reverse.js

@ -1,6 +1,7 @@
var logger = require('../src/logger'),
queries = require('geopipes-elasticsearch-backend').queries;
queries = require('geopipes-elasticsearch-backend').queries,
sort = require('../query/sort');
function generate( params ){
@ -9,7 +10,10 @@ function generate( params ){
lon: params.lon
};
return queries.distance( centroid, { size: params.size || 1 } );
var query = queries.distance( centroid, { size: params.size || 1 } );
query.sort = query.sort.concat(sort);
return query;
}
module.exports = generate;

6
query/search.js

@ -1,6 +1,7 @@
var logger = require('../src/logger'),
queries = require('geopipes-elasticsearch-backend').queries;
queries = require('geopipes-elasticsearch-backend').queries,
sort = require('../query/sort');
function generate( params ){
@ -14,6 +15,7 @@ function generate( params ){
}
var query = queries.distance( centroid, { size: params.size } );
if (params.bbox) {
query = queries.bbox ( centroid, { size: params.size, bbox: params.bbox } );
}
@ -27,6 +29,8 @@ function generate( params ){
}
};
query.sort = query.sort.concat(sort);
return query;
}

22
query/sort.js

@ -0,0 +1,22 @@
var population = 'population';
var weights = require('pelias-suggester-pipeline').weights;
module.exports = [
{
'_script': {
'file': population,
'type': 'number',
'order': 'desc'
}
},
{
'_script': {
'params': {
'weights': weights
},
'file': 'weights',
'type': 'number',
'order': 'desc'
}
}
];

4
test/unit/controller/search.js

@ -53,7 +53,7 @@ module.exports.tests.functional_success = function(test, common) {
test('functional success', function(t) {
var backend = mockBackend( 'client/search/ok/1', function( cmd ){
t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias' }, 'correct backend command');
t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias', searchType: 'dfs_query_then_fetch' }, 'correct backend command');
});
var controller = setup( backend, mockQuery() );
var res = {
@ -78,7 +78,7 @@ module.exports.tests.functional_success = function(test, common) {
module.exports.tests.functional_failure = function(test, common) {
test('functional failure', function(t) {
var backend = mockBackend( 'client/search/fail/1', function( cmd ){
t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias' }, 'correct backend command');
t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias', searchType: 'dfs_query_then_fetch' }, 'correct backend command');
});
var controller = setup( backend, mockQuery() );
var next = function( message ){

28
test/unit/query/reverse.js

@ -1,5 +1,6 @@
var generate = require('../../../query/reverse');
var weights = require('pelias-suggester-pipeline').weights;
module.exports.tests = {};
@ -10,6 +11,27 @@ module.exports.tests.interface = function(test, common) {
});
};
var sort = [
'_score',
{
'_script': {
'file': 'population',
'type': 'number',
'order': 'desc'
}
},
{
'_script': {
'params': {
'weights': weights
},
'file': 'weights',
'type': 'number',
'order': 'desc'
}
}
];
module.exports.tests.query = function(test, common) {
test('valid query', function(t) {
var query = generate({
@ -43,6 +65,7 @@ module.exports.tests.query = function(test, common) {
}
},
'sort': [
'_score',
{
'_geo_distance': {
'center_point': {
@ -53,8 +76,9 @@ module.exports.tests.query = function(test, common) {
'unit': 'km'
}
}
],
'size': 1
].concat(sort.slice(1)),
'size': 1,
'track_scores': true
};
t.deepEqual(query, expected, 'valid reverse query');

44
test/unit/query/search.js

@ -1,5 +1,6 @@
var generate = require('../../../query/search');
var weights = require('pelias-suggester-pipeline').weights;
module.exports.tests = {};
@ -10,6 +11,27 @@ module.exports.tests.interface = function(test, common) {
});
};
var sort = [
'_score',
{
'_script': {
'file': 'population',
'type': 'number',
'order': 'desc'
}
},
{
'_script': {
'params': {
'weights': weights
},
'file': 'weights',
'type': 'number',
'order': 'desc'
}
}
];
module.exports.tests.query = function(test, common) {
test('valid query', function(t) {
var query = generate({
@ -55,10 +77,11 @@ module.exports.tests.query = function(test, common) {
}
}
},
'sort': [],
'size': 10
'sort': sort,
'size': 10,
'track_scores': true
};
t.deepEqual(query, expected, 'valid search query');
t.end();
});
@ -106,8 +129,9 @@ module.exports.tests.query = function(test, common) {
}
}
},
'sort': [],
'size': 10
'sort': sort,
'size': 10,
'track_scores': true
};
t.deepEqual(query, expected, 'valid search query');
@ -139,7 +163,9 @@ module.exports.tests.query = function(test, common) {
}
}
},
'size': 10
'size': 10,
'sort': sort,
'track_scores': true
};
t.deepEqual(query, expected, 'valid search query');
@ -186,6 +212,7 @@ module.exports.tests.query = function(test, common) {
}
},
'sort': [
'_score',
{
'_geo_distance': {
'center_point': {
@ -196,8 +223,9 @@ module.exports.tests.query = function(test, common) {
'unit': 'km'
}
}
],
'size': 10
].concat(sort.slice(1)),
'size': 10,
'track_scores': true
};
t.deepEqual(query, expected, 'valid search query');

52
test/unit/query/sort.js

@ -0,0 +1,52 @@
var generate = require('../../../query/sort');
var population = 'population';
var weights = require('pelias-suggester-pipeline').weights;
module.exports.tests = {};
module.exports.tests.interface = function(test, common) {
test('valid interface', function(t) {
t.equal(typeof generate, 'object', 'valid object');
t.end();
});
};
var expected = [
{
'_script': {
'file': population,
'type': 'number',
'order': 'desc'
}
},
{
'_script': {
'params': {
'weights': weights
},
'file': 'weights',
'type': 'number',
'order': 'desc'
}
}
];
module.exports.tests.query = function(test, common) {
test('valid part of query', function(t) {
var sort = generate;
t.deepEqual(sort, expected, 'valid sort part of the query');
t.end();
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('sort query ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

1
test/unit/run.js

@ -15,6 +15,7 @@ var tests = [
require('./sanitiser/coarse'),
require('./query/indeces'),
require('./query/suggest'),
require('./query/sort'),
require('./query/search'),
require('./query/reverse'),
require('./helper/geojsonify'),

Loading…
Cancel
Save