Skip to content
This repository has been archived by the owner on Mar 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #12 from lemonde/amador-replica-commands
Browse files Browse the repository at this point in the history
feat(replication) Permet de repliquer des indexes
  • Loading branch information
larube authored Feb 2, 2017
2 parents b33dbb7 + c08efeb commit 5b21831
Show file tree
Hide file tree
Showing 24 changed files with 411 additions and 300 deletions.
53 changes: 53 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"bitwise": true,
"camelcase": true,
"curly": false,
"eqeqeq": true,
"es3": false,
"forin": true,
"immed": true,
"indent": 2,
"latedef": "nofunc",
"newcap": true,
"noarg": true,
"noempty": true,
"nonew": false,
"plusplus": false,
"quotmark": true,
"regexp": false,
"undef": true,
"unused": true,
"strict": false,
"trailing": true,
"maxparams": 4,
"maxdepth": 2,
"maxstatements": 30,
"maxcomplexity": 10,
"maxlen": 110,

"asi": false,
"boss": false,
"debug": false,
"eqnull": true,
"esnext": true,
"evil": false,
"expr": false,
"funcscope": false,
"globalstrict": false,
"iterator": false,
"lastsemic": false,
"laxbreak": false,
"laxcomma": false,
"loopfunc": false,
"moz": false,
"multistr": true,
"proto": false,
"scripturl": false,
"smarttabs": false,
"shadow": false,
"sub": false,
"supernew": false,
"validthis": true,

"node": true
}
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
language: node_js
node_js:
- "0.10"
- 6.9
25 changes: 13 additions & 12 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var request = require('request'),
_ = require('lodash'),
_s = require('underscore.string'),
urlParser = require('url');
const request = require('request');
const _ = require('lodash');
const _s = require('underscore.string');
const urlParser = require('url');

// Avalable APIS
var apis = ['search', 'moreLikeThis', 'indexes', 'fields', 'documents', 'templates'];
const apis = ['search', 'moreLikeThis', 'indexes', 'fields', 'documents', 'templates', 'replication'];

// Build API
function buildApi(name) {
Expand All @@ -30,21 +30,23 @@ function Client(options) {
// Make an http request to defined endpoint
Client.prototype.request = function (options, callback) {

var url = urlParser.format(_(this.options)
const url = urlParser.format(_(this.options)
.pick(['hostname', 'port', 'protocol', 'query'])
.defaults({
protocol: 'http',
pathname: options.pathname
pathname: options.pathname,
search: options.search,
body: options.body
}).value());

request(_(options)
.defaults({
url: url,
url,
json: true
})
.value(), function (err, res, body) {
.value(), (err, res, body) => {
if (err) {
var message = err.message;
let message = err.message;

if (err.code === 'ECONNREFUSED') {
message = 'Cannot connect to OpenSearchServer at ' + url;
Expand All @@ -62,5 +64,4 @@ Client.prototype.request = function (options, callback) {
});
};


module.exports = Client;
module.exports = Client;
6 changes: 3 additions & 3 deletions lib/documents.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var _ = require('lodash');
const _ = require('lodash');

var documents = module.exports = {
const documents = module.exports = {
documents: {}
};

Expand Down Expand Up @@ -31,4 +31,4 @@ var documents = module.exports = {
}, callback);
};

}).call(documents.documents);
}).call(documents.documents);
18 changes: 8 additions & 10 deletions lib/fields.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
var util = require('./util');
const util = require('./util');

var fields = module.exports = {
fields: {}
};
const fields = module.exports = { fields: {} };

(function () {

// Create or update a new field
this.createOrUpdate = function (index, options, callback) {
options = options || {};

['indexed', 'stored', 'termVector'].forEach(function (param) {
['indexed', 'stored', 'termVector'].forEach((param) => {
if (typeof options[param] !== 'undefined')
options[param] = util.convertParam(options[param]);
});

this.request({
pathname: '/services/rest/index/' + index + '/field/' + options.name,
pathname: `/services/rest/index/${index}/field/${options.name}`,
method: 'PUT',
json: options
}, callback);
Expand All @@ -28,15 +26,15 @@ var fields = module.exports = {
// Remove an existing field
this.destroy = function (index, field, callback) {
this.request({
pathname: '/services/rest/index/' + index + '/field/' + field,
pathname: `/services/rest/index/${index}/field/${field}`,
method: 'DELETE'
}, callback);
};

// Set the unique and the default fields
this.setUniqueDefault = function (index, options, callback) {
this.request({
pathname: '/services/rest/index/' + index + '/field',
pathname: `/services/rest/index/${index}/field`,
method: 'POST',
qs: options
}, callback);
Expand All @@ -45,9 +43,9 @@ var fields = module.exports = {
// list fields
this.list = function (index, callback) {
this.request({
pathname: '/services/rest/index/' + index + '/field/',
pathname: `/services/rest/index/${index}/field/`,
method: 'GET'
}, callback);
};

}).call(fields.fields);
}).call(fields.fields);
17 changes: 9 additions & 8 deletions lib/indexes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
var _ = require('lodash');
const _ = require('lodash');

var indexes = module.exports = {
indexes: {}
};
const indexes = module.exports = { indexes: {} };

(function () {

Expand All @@ -17,26 +15,29 @@ var indexes = module.exports = {
template: ''
});

const { template } = options;
const templateQueryString = template ? '/template/' + template : '';

this.request({
pathname: '/services/rest/index/' + index + (options.template ? '/template/' + options.template : ''),
pathname: `/services/rest/index/${index}${templateQueryString}`,
method: 'POST'
}, callback);
};

// Test if index exists
this.exists = function (index, callback) {
this.request({
pathname: '/services/rest/index/' + index,
pathname: `/services/rest/index/${index}`,
method: 'GET'
}, callback);
};

// Remove an existing index
this.destroy = function (index, callback) {
this.request({
pathname: '/services/rest/index/' + index,
pathname: `/services/rest/index/${index}`,
method: 'DELETE'
}, callback);
};

}).call(indexes.indexes);
}).call(indexes.indexes);
8 changes: 4 additions & 4 deletions lib/more-like-this.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
var _ = require('lodash');
const _ = require('lodash');

var moreLikeThis = module.exports = {};
const moreLikeThis = module.exports = {};

(function () {

// More like this
this.moreLikeThis = function (index, options, callback) {
this.request({
pathname: '/services/rest/index/' + index + '/morelikethis',
pathname: `/services/rest/index/${index}/morelikethis`,
method: 'POST',
json: _.defaults({}, options || {}, {
analyzerName: 'StandardAnalyzer',
Expand All @@ -25,4 +25,4 @@ var moreLikeThis = module.exports = {};
}, callback);
};

}).call(moreLikeThis);
}).call(moreLikeThis);
6 changes: 3 additions & 3 deletions lib/oss.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var Client = require('./client');
var util = require('./util');
const Client = require('./client');
const util = require('./util');

// Create a new client
function createClient(options) {
Expand All @@ -8,4 +8,4 @@ function createClient(options) {

exports.Client = Client;
exports.createClient = createClient;
exports.util = util;
exports.util = util;
33 changes: 33 additions & 0 deletions lib/replication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const _ = require('lodash');

const replication = module.exports = {};

(function () {

this.createReplicationIndex = function(index, searcher, callback) {
this.request({
pathname: `/services/rest/index/${index}/replication`,
method: 'PUT',
body: {
replicationType: 'MAIN_INDEX',
remoteUrl: buildUrl(searcher),
remoteIndexName: index,
secTimeOut: 120
}
}, callback);
};

this.replicate = function(index, searcher, callback) {
const searcherUrl = buildUrl(searcher);
this.request({
pathname: `/services/rest/index/${index}/replication/run`,
method: 'PUT',
search: `name=${searcherUrl}/${index}`
}, callback);
};

function buildUrl({ protocol = 'http', hostname, port }) {
return `${protocol}://${hostname}:${port}`;
}

}).call(replication);
8 changes: 4 additions & 4 deletions lib/search.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var _ = require('lodash');
const _ = require('lodash');

var search = module.exports = {};
const search = module.exports = {};

(function () {

Expand All @@ -11,10 +11,10 @@ var search = module.exports = {};
});

this.request({
pathname: '/services/rest/index/' + index + '/search/' + options.type,
pathname: `/services/rest/index/${index}/search/${options.type}`,
method: 'POST',
json: _.omit(options, 'type')
}, callback);
};

}).call(search);
}).call(search);
16 changes: 7 additions & 9 deletions lib/templates.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
var _ = require('lodash');
const _ = require('lodash');

var templates = module.exports = {
templates: {}
};
const templates = module.exports = { templates: {} };

(function () {

// Get a search template.
this.get = function (index, name, callback) {
this.request({
pathname: '/services/rest/index/' + index + '/search/template/' + name,
pathname: `/services/rest/index/${index}/search/template/${name}`,
method: 'GET'
}, callback);
};

// List all search templates.
this.list = function (index, callback) {
this.request({
pathname: '/services/rest/index/' + index + '/search/template/',
pathname: `/services/rest/index/${index}/search/template/`,
method: 'GET'
}, callback);
};
Expand All @@ -29,7 +27,7 @@ var templates = module.exports = {
});

this.request({
pathname: '/services/rest/index/' + index + '/search/' + options.type + '/' + name,
pathname: `/services/rest/index/${index}/search/${options.type}/${name}`,
method: 'PUT',
json: _.omit(options, 'type')
}, callback);
Expand All @@ -41,9 +39,9 @@ var templates = module.exports = {
// Remove an existing template.
this.destroy = function (index, name, callback) {
this.request({
pathname: '/services/rest/index/' + index + '/search/template/' + name,
pathname: `/services/rest/index/${index}/search/template/${name}`,
method: 'DELETE'
}, callback);
};

}).call(templates.templates);
}).call(templates.templates);
4 changes: 2 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _ = require('lodash');
const _ = require('lodash');

function convertParam(val) {
return val === true ? 'YES' :
Expand All @@ -7,4 +7,4 @@ function convertParam(val) {
val;
}

exports.convertParam = convertParam;
exports.convertParam = convertParam;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-oss-client",
"version": "1.0.0",
"version": "2.0.0",
"description": "Node.js client for Open Search Server.",
"main": "index.js",
"scripts": {
Expand Down
Loading

0 comments on commit 5b21831

Please sign in to comment.