Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Service & Service Object for Search #945

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 64 additions & 33 deletions lib/search/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@
'use strict';

var is = require('is');
var nodeutil = require('util');

/**
* @type {module:search/field}
* @private
*/
var Field = require('./field.js');

/**
* @type {module:common/serviceObject}
* @private
*/
var ServiceObject = require('../common/service-object.js');

/**
* @type {module:common/util}
* @private
Expand All @@ -52,13 +59,59 @@ var util = require('../common/util.js');
* var document = search.index('records').document('stephen');
*/
function Document(index, id) {
this.search_ = index.search_;
this.index_ = index;
var methods = {
/**
* Delete this document.
*
* @resource [Documents: delete API Documentation]{@link https://cloud.google.com/search/reference/rest/v1/projects/indexes/documents/delete}
*
* @param {function=} callback - The callback function.
* @param {?error} callback.err - An error returned while making this
* request.
* @param {object} callback.apiResponse - The full API response.
*
* @example
* document.delete(function(err, apiResponse) {});
*/
delete: true,

/**
* Check if the document exists.
*
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this
* request.
* @param {boolean} callback.exists - Whether the zone exists or not.
*
* @example
* document.exists(function(err, exists) {});
*/
exists: true,

/**
* Get a document if it exists.
*
* @example
* document.get(function(err, document, apiResponse) {
* // `document.fields` has been populated.
* });
*/
get: true
};

ServiceObject.call(this, {
parent: index,
baseUrl: '/documents',
id: id,
methods: methods
});

this.id = id;
this.fields = {};
}

nodeutil.inherits(Document, ServiceObject);

/**
* Add a field to this document.
*
Expand All @@ -82,21 +135,17 @@ Document.prototype.addField = function(name) {
};

/**
* Delete this document.
*
* @resource [Documents: delete API Documentation]{@link https://cloud.google.com/search/reference/rest/v1/projects/indexes/documents/delete}
*
* @param {function=} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request
* @param {object} callback.apiResponse - The full API response.
* Create a document.
*
* @example
* document.delete(function(err, apiResponse) {});
* document.create(function(err, document, apiResponse) {
* if (!err) {
* // The document was created successfully.
* }
* });
*/
Document.prototype.delete = function(callback) {
this.makeReq_('DELETE', '', null, null, function(err, resp) {
(callback || util.noop)(err, resp);
});
Document.prototype.create = function(callback) {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

this.parent.createDocument(this, callback);
};

/**
Expand Down Expand Up @@ -129,7 +178,7 @@ Document.prototype.getMetadata = function(callback) {

callback = callback || util.noop;

this.makeReq_('GET', '/', null, null, function(err, resp) {
ServiceObject.prototype.getMetadata.call(this, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
Expand Down Expand Up @@ -203,22 +252,4 @@ Document.prototype.toJSON = function() {
return documentObject;
};

/**
* Make a new request object from the provided arguments and wrap the callback
* to intercept non-successful responses.
*
* @private
*
* @param {string} method - Action.
* @param {string} path - Request path.
* @param {*} query - Request query object.
* @param {*} body - Request body contents.
* @param {function} callback - The callback function.
*/
Document.prototype.makeReq_ = function(method, path, query, body, callback) {
path = '/documents/' + this.id + path;

this.index_.makeReq_(method, path, query, body, callback);
};

module.exports = Document;
83 changes: 55 additions & 28 deletions lib/search/index-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,23 @@

'use strict';

var arrify = require('arrify');
var extend = require('extend');
var is = require('is');
var nodeutil = require('util');

/**
* @type {module:search/document}
* @private
*/
var Document = require('./document.js');

/**
* @type {module:common/serviceObject}
* @private
*/
var ServiceObject = require('../common/service-object.js');

/**
* @type {module:common/streamrouter}
* @private
Expand All @@ -53,14 +61,20 @@ var streamRouter = require('../common/stream-router.js');
* var index = search.index('records');
*/
function Index(search, id) {
this.search_ = search;
this.id = id;
ServiceObject.call(this, {
parent: search,
baseUrl: '/indexes',
id: id,
methods: {
// Nothing needed other than the `request` method.
}
});

if (!this.id) {
throw new Error('An ID is needed to access a Google Cloud Search index.');
}
this.id = id;
}

nodeutil.inherits(Index, ServiceObject);

/**
* Create a document in the index.
*
Expand Down Expand Up @@ -121,7 +135,11 @@ Index.prototype.createDocument = function(documentObj, callback) {
document = this.documentFromObject_(documentObj);
}

this.makeReq_('POST', '/documents', null, documentObj, function(err, resp) {
this.request({
method: 'POST',
uri: '/documents',
json: documentObj
}, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
Expand All @@ -139,8 +157,29 @@ Index.prototype.createDocument = function(documentObj, callback) {
*
* @example
* var myDocument = index.document('my-document');
*
* //-
* // Documents can also be created from objects.
* //-
* var myDocument = index.document({
* docId: 'my-document',
* fields: {
* person: {
* values: [
* {
* stringFormat: 'TEXT',
* stringValue: 'Stephen'
* }
* ]
* }
* }
* });
*/
Index.prototype.document = function(id) {
if (is.object(id)) {
return this.documentFromObject_(id);
}

return new Document(this, id);
};

Expand Down Expand Up @@ -222,7 +261,10 @@ Index.prototype.getDocuments = function(query, callback) {
query = {};
}

this.makeReq_('GET', '/documents', query, null, function(err, resp) {
this.request({
uri: '/documents',
qs: query
}, function(err, resp) {
if (err) {
callback(err, null, null, resp);
return;
Expand All @@ -236,7 +278,7 @@ Index.prototype.getDocuments = function(query, callback) {
});
}

var documents = (resp.documents || [])
var documents = arrify(resp.documents)
.map(self.documentFromObject_.bind(self));

callback(null, documents, nextQuery, resp);
Expand Down Expand Up @@ -322,7 +364,10 @@ Index.prototype.search = function(query, callback) {
throw new Error('A query must be either a string or object.');
}

this.makeReq_('GET', '/search', query, null, function(err, resp) {
this.request({
uri: '/search',
qs: query
}, function(err, resp) {
if (err) {
callback(err, null, null, resp);
return;
Expand All @@ -336,7 +381,7 @@ Index.prototype.search = function(query, callback) {
});
}

var documents = (resp.results || [])
var documents = arrify(resp.results)
.map(self.documentFromObject_.bind(self));

callback(null, documents, nextQuery, resp);
Expand Down Expand Up @@ -385,24 +430,6 @@ Index.prototype.documentFromObject_ = function(documentObj) {
return document;
};

/**
* Make a new request object from the provided arguments and wrap the callback
* to intercept non-successful responses.
*
* @private
*
* @param {string} method - Action.
* @param {string} path - Request path.
* @param {*} query - Request query object.
* @param {*} body - Request body contents.
* @param {function} callback - The callback function.
*/
Index.prototype.makeReq_ = function(method, path, query, body, callback) {
path = '/indexes/' + this.id + path;

this.search_.makeReq_(method, path, query, body, callback);
};

/*! Developer Documentation
*
* {module:search/index#getDocuments} and {module:search/index#search} can be
Expand Down
Loading