[#128,backend/elasticsearch][s]: standardize on url field on dataset to hold url for ES backend.

* also ensure reasonable docs for all methods
This commit is contained in:
Rufus Pollock
2012-05-26 22:55:01 +01:00
parent 13d1a9e0bd
commit 92543a76ee

View File

@@ -10,9 +10,11 @@ this.recline.Backend.ElasticSearch = this.recline.Backend.ElasticSearch || {};
// on localhost:9200 with index // twitter and type tweet it would be: // on localhost:9200 with index // twitter and type tweet it would be:
// //
// <pre>http://localhost:9200/twitter/tweet</pre> // <pre>http://localhost:9200/twitter/tweet</pre>
//
// @param {Object} options: set of options such as: // @param {Object} options: set of options such as:
// //
// * headers - {dict of headers to add to each request} // * headers - {dict of headers to add to each request}
// * dataType: dataType for AJAx requests e.g. set to jsonp to make jsonp requests (default is json requests)
my.Wrapper = function(endpoint, options) { my.Wrapper = function(endpoint, options) {
var self = this; var self = this;
this.endpoint = endpoint; this.endpoint = endpoint;
@@ -21,6 +23,11 @@ this.recline.Backend.ElasticSearch = this.recline.Backend.ElasticSearch || {};
}, },
options); options);
// ### mapping
//
// Get ES mapping for this type/table
//
// @return promise compatible deferred object.
this.mapping = function() { this.mapping = function() {
var schemaUrl = self.endpoint + '/_mapping'; var schemaUrl = self.endpoint + '/_mapping';
var jqxhr = recline.Backend.makeRequest({ var jqxhr = recline.Backend.makeRequest({
@@ -30,24 +37,26 @@ this.recline.Backend.ElasticSearch = this.recline.Backend.ElasticSearch || {};
return jqxhr; return jqxhr;
}; };
this.get = function(id, error, success) { // ### get
//
// Get document corresponding to specified id
//
// @return promise compatible deferred object.
this.get = function(id) {
var base = this.endpoint + '/' + id; var base = this.endpoint + '/' + id;
return recline.Backend.makeRequest({ return recline.Backend.makeRequest({
url: base, url: base,
dataType: 'json', dataType: 'json'
error: error,
success: success
}); });
} };
// ### upsert // ### upsert
// //
// create / update a document to ElasticSearch backend // create / update a document to ElasticSearch backend
// //
// @param {Object} doc an object to insert to the index. // @param {Object} doc an object to insert to the index.
// @param {string} url (optional) url for ElasticSearch endpoint (if not // @return deferred supporting promise API
// defined called this._getESUrl() this.upsert = function(doc) {
this.upsert = function(doc, error, success) {
var data = JSON.stringify(doc); var data = JSON.stringify(doc);
url = this.endpoint; url = this.endpoint;
if (doc.id) { if (doc.id) {
@@ -57,9 +66,7 @@ this.recline.Backend.ElasticSearch = this.recline.Backend.ElasticSearch || {};
url: url, url: url,
type: 'POST', type: 'POST',
data: data, data: data,
dataType: 'json', dataType: 'json'
error: error,
success: success
}); });
}; };
@@ -68,9 +75,8 @@ this.recline.Backend.ElasticSearch = this.recline.Backend.ElasticSearch || {};
// Delete a document from the ElasticSearch backend. // Delete a document from the ElasticSearch backend.
// //
// @param {Object} id id of object to delete // @param {Object} id id of object to delete
// @param {string} url (optional) url for ElasticSearch endpoint (if not // @return deferred supporting promise API
// provided called this._getESUrl() this.delete = function(id) {
this.delete = function(id, error, success) {
url = this.endpoint; url = this.endpoint;
url += '/' + id; url += '/' + id;
return recline.Backend.makeRequest({ return recline.Backend.makeRequest({
@@ -113,6 +119,9 @@ this.recline.Backend.ElasticSearch = this.recline.Backend.ElasticSearch || {};
return out; return out;
}; };
// ### query
//
// @return deferred supporting promise API
this.query = function(queryObj) { this.query = function(queryObj) {
var queryNormalized = this._normalizeQuery(queryObj); var queryNormalized = this._normalizeQuery(queryObj);
var data = {source: JSON.stringify(queryNormalized)}; var data = {source: JSON.stringify(queryNormalized)};
@@ -144,21 +153,14 @@ this.recline.Backend.ElasticSearch = this.recline.Backend.ElasticSearch || {};
// //
// Backbone sync implementation for this backend. // Backbone sync implementation for this backend.
// //
// URL of ElasticSearch endpoint to use must be specified on the // URL of ElasticSearch endpoint to use must be specified on the dataset
// dataset (and on a Document by the document having a dataset // (and on a Document via its dataset attribute) by the dataset having a
// attribute) by the dataset having one of the following data // url attribute.
// attributes (see also `_getESUrl` function):
//
// <pre>
// elasticsearch_url
// webstore_url
// url
// </pre>
this.sync = function(method, model, options) { this.sync = function(method, model, options) {
if (model.__type__ == 'Dataset') { if (model.__type__ == 'Dataset') {
var endpoint = self._getESUrl(model); var endpoint = model.get('url');
} else { } else {
var endpoint = self._getESUrl(model.dataset); var endpoint = model.dataset.get('url');
} }
var es = new my.Wrapper(endpoint, esOptions); var es = new my.Wrapper(endpoint, esOptions);
if (method === "read") { if (method === "read") {
@@ -192,24 +194,12 @@ this.recline.Backend.ElasticSearch = this.recline.Backend.ElasticSearch || {};
} }
}; };
// ### _getESUrl // ### query
// //
// get url to ElasticSearch endpoint (see above) // query the ES backend
this._getESUrl = function(dataset) {
if (dataset) {
var out = dataset.get('elasticsearch_url');
if (out) return out;
out = dataset.get('webstore_url');
if (out) return out;
out = dataset.get('url');
return out;
}
return this.get('url');
};
this.query = function(model, queryObj) { this.query = function(model, queryObj) {
var dfd = $.Deferred(); var dfd = $.Deferred();
var url = this._getESUrl(model); var url = model.get('url');
var es = new my.Wrapper(url, esOptions); var es = new my.Wrapper(url, esOptions);
var jqxhr = es.query(queryObj); var jqxhr = es.query(queryObj);
// TODO: fail case // TODO: fail case