Browse Source

Merge pull request #595 from pelias/334-configurable-index-name

Allow Elasticsearch index name to be set from pelias-config
pull/446/merge
Julian Simioni 8 years ago committed by GitHub
parent
commit
ae26dba6da
  1. 4
      controller/place.js
  2. 4
      controller/search.js
  3. 6
      routes/v1.js
  4. 5
      test/ciao_test_data.js
  5. 39
      test/unit/controller/place.js
  6. 39
      test/unit/controller/search.js

4
controller/place.js

@ -1,7 +1,7 @@
var service = { mget: require('../service/mget') };
var logger = require('pelias-logger').get('api:controller:place');
function setup( backend ){
function setup( config, backend ){
// allow overriding of dependencies
backend = backend || require('../src/backend');
@ -16,7 +16,7 @@ function setup( backend ){
var query = req.clean.ids.map( function(id) {
return {
_index: 'pelias',
_index: config.indexName,
_type: id.layers,
_id: id.id
};

4
controller/search.js

@ -4,7 +4,7 @@ var service = { search: require('../service/search') };
var logger = require('pelias-logger').get('api:controller:search');
var logging = require( '../helper/logging' );
function setup( backend, query ){
function setup( config, backend, query ){
// allow overriding of dependencies
backend = backend || require('../src/backend');
@ -26,7 +26,7 @@ function setup( backend, query ){
// backend command
var cmd = {
index: 'pelias',
index: config.indexName,
searchType: 'dfs_query_then_fetch',
body: query( req.clean )
};

6
routes/v1.js

@ -61,7 +61,7 @@ function addRoutes(app, peliasConfig) {
search: createRouter([
sanitisers.search.middleware,
middleware.calcSize(),
controllers.search(),
controllers.search(peliasConfig),
postProc.distances('focus.point.'),
postProc.confidenceScores(peliasConfig),
postProc.dedupe(),
@ -74,7 +74,7 @@ function addRoutes(app, peliasConfig) {
]),
autocomplete: createRouter([
sanitisers.autocomplete.middleware,
controllers.search(null, require('../query/autocomplete')),
controllers.search(peliasConfig, null, require('../query/autocomplete')),
postProc.distances('focus.point.'),
postProc.confidenceScores(peliasConfig),
postProc.dedupe(),
@ -88,7 +88,7 @@ function addRoutes(app, peliasConfig) {
reverse: createRouter([
sanitisers.reverse.middleware,
middleware.calcSize(),
controllers.search(undefined, reverseQuery),
controllers.search(peliasConfig, undefined, reverseQuery),
postProc.distances('point.'),
// reverse confidence scoring depends on distance from origin
// so it must be calculated first

5
test/ciao_test_data.js

@ -15,6 +15,7 @@
var client = require('elasticsearch').Client(),
async = require('async'),
actions = [];
var config = require('pelias-config').generate().api;
// add one record per 'type' in order to cause the _default_ mapping
// to be copied when the new type is created.
@ -28,7 +29,7 @@ types.forEach( function( type, i1 ){
layers.forEach( function( layer, i3 ){
actions.push( function( done ){
client.index({
index: 'pelias',
index: config.indexName,
type: type,
id: [i1,i2,i3].join(':'),
body: {
@ -49,7 +50,7 @@ types.forEach( function( type, i1 ){
// call refresh so the index merges the changes
actions.push( function( done ){
client.indices.refresh( { index: 'pelias' }, done);
client.indices.refresh( { index: config.indexName }, done);
});
// perform all actions in series

39
test/unit/controller/place.js

@ -11,6 +11,11 @@ module.exports.tests.interface = function(test, common) {
});
};
// reminder: this is only the api subsection of the full config
var fakeDefaultConfig = {
indexName: 'pelias'
};
// functionally test controller (backend success)
module.exports.tests.functional_success = function(test, common) {
@ -43,7 +48,37 @@ module.exports.tests.functional_success = function(test, common) {
var backend = mockBackend( 'client/mget/ok/1', function( cmd ){
t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: [ 'a' ] } ] } }, 'correct backend command');
});
var controller = setup( backend );
var controller = setup( fakeDefaultConfig, backend );
var res = {
status: function( code ){
t.equal(code, 200, 'status set');
return res;
},
json: function( json ){
t.equal(typeof json, 'object', 'returns json');
t.equal(typeof json.date, 'number', 'date set');
t.equal(json.type, 'FeatureCollection', 'valid geojson');
t.true(Array.isArray(json.features), 'features is array');
t.deepEqual(json.features, expected, 'values correctly mapped');
}
};
var req = { clean: { ids: [ {'id' : 123, layers: [ 'a' ] } ] }, errors: [], warnings: [] };
var next = function next() {
t.equal(req.errors.length, 0, 'next was called without error');
t.end();
};
controller(req, res, next );
});
test('functional success with custom index name', function(t) {
var fakeCustomizedConfig = {
indexName: 'alternateindexname'
};
var backend = mockBackend( 'client/mget/ok/1', function( cmd ){
t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'alternateindexname', _type: [ 'a' ] } ] } }, 'correct backend command');
});
var controller = setup( fakeCustomizedConfig, backend );
var res = {
status: function( code ){
t.equal(code, 200, 'status set');
@ -72,7 +107,7 @@ module.exports.tests.functional_failure = function(test, common) {
var backend = mockBackend( 'client/mget/fail/1', function( cmd ){
t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: ['b'] } ] } }, 'correct backend command');
});
var controller = setup( backend );
var controller = setup( fakeDefaultConfig, backend );
var req = { clean: { ids: [ {'id' : 123, layers: [ 'b' ] } ] }, errors: [], warnings: [] };
var next = function( message ){
t.equal(req.errors[0],'a backend error occurred','error passed to errorHandler');

39
test/unit/controller/search.js

@ -1,4 +1,3 @@
var setup = require('../../../controller/search'),
mockBackend = require('../mock/backend'),
mockQuery = require('../mock/query');
@ -13,6 +12,11 @@ module.exports.tests.interface = function(test, common) {
});
};
// reminder: this is only the api subsection of the full config
var fakeDefaultConfig = {
indexName: 'pelias'
};
// functionally test controller (backend success)
module.exports.tests.functional_success = function(test, common) {
@ -82,7 +86,7 @@ module.exports.tests.functional_success = function(test, common) {
searchType: 'dfs_query_then_fetch'
}, 'correct backend command');
});
var controller = setup(backend, mockQuery());
var controller = setup(fakeDefaultConfig, backend, mockQuery());
var res = {
status: function (code) {
t.equal(code, 200, 'status set');
@ -105,6 +109,33 @@ module.exports.tests.functional_success = function(test, common) {
};
controller(req, res, next);
});
test('functional success with alternate index name', function(t) {
var fakeCustomizedConfig = {
indexName: 'alternateindexname'
};
var backend = mockBackend('client/search/ok/1', function (cmd) {
t.deepEqual(cmd, {
body: {a: 'b'},
index: 'alternateindexname',
searchType: 'dfs_query_then_fetch'
}, 'correct backend command');
});
var controller = setup(fakeCustomizedConfig, backend, mockQuery());
var res = {
status: function (code) {
t.equal(code, 200, 'status set');
return res;
}
};
var req = { clean: { a: 'b' }, errors: [], warnings: [] };
var next = function next() {
t.equal(req.errors.length, 0, 'next was called without error');
t.end();
};
controller(req, res, next);
});
};
// functionally test controller (backend failure)
@ -113,7 +144,7 @@ module.exports.tests.functional_failure = function(test, common) {
var backend = mockBackend( 'client/search/fail/1', function( cmd ){
t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias', searchType: 'dfs_query_then_fetch' }, 'correct backend command');
});
var controller = setup( backend, mockQuery() );
var controller = setup( fakeDefaultConfig, backend, mockQuery() );
var req = { clean: { a: 'b' }, errors: [], warnings: [] };
var next = function(){
t.equal(req.errors[0],'a backend error occurred');
@ -128,7 +159,7 @@ module.exports.tests.timeout = function(test, common) {
var backend = mockBackend( 'client/search/timeout/1', function( cmd ){
t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias', searchType: 'dfs_query_then_fetch' }, 'correct backend command');
});
var controller = setup( backend, mockQuery() );
var controller = setup( fakeDefaultConfig, backend, mockQuery() );
var req = { clean: { a: 'b' }, errors: [], warnings: [] };
var next = function(){
t.equal(req.errors[0],'Request Timeout after 5000ms');

Loading…
Cancel
Save