Skip to content

Commit

Permalink
search: introduce api
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Jun 8, 2015
1 parent 01ec51f commit 0c6feb4
Show file tree
Hide file tree
Showing 8 changed files with 567 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ var Datastore = require('./datastore');
*/
var PubSub = require('./pubsub');

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

/**
* @type {module:storage}
* @private
Expand Down Expand Up @@ -120,6 +126,10 @@ function gcloud(config) {
options = options || {};
return new PubSub(util.extendGlobalConfig(config, options));
},
search: function(options) {
options = options || {};
return new Search(util.extendGlobalConfig(config, options));
},
storage: function(options) {
options = options || {};
return new Storage(util.extendGlobalConfig(config, options));
Expand Down Expand Up @@ -194,6 +204,10 @@ gcloud.pubsub = function(config) {
return new PubSub(config);
};

gcloud.search = function (config) {
return new Search(config);
};

/**
* Google Cloud Storage allows you to store data on Google infrastructure.
* Read [Google Cloud Storage API docs](https://developers.google.com/storage/)
Expand Down
69 changes: 69 additions & 0 deletions lib/search/document.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*!
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*!
* @module search/document
*/

'use strict';

/**
* @type {module:common/util}
* @private
*/
var util = require('../common/util.js');

function Document(index, id) {
this.search_ = index.search_;
this.index_ = index;
this.id = id;
}

Document.prototype.delete = function(callback) {
callback = callback || util.noop;
this.makeReq_('DELETE', '', null, null, callback);
};

Document.prototype.getMetadata = function(callback) {
var self = this;

callback = callback || util.noop;

this.makeReq_('GET', '/', null, null, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
}

if (util.is(resp.fields, 'object')) {
self.fields = resp.fields;
}

if (util.is(resp.rank, 'number')) {
self.rank = resp.rank;
}

callback(null, self, resp);
});
};

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;
140 changes: 140 additions & 0 deletions lib/search/index-class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*!
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*!
* @module search/index
*/

'use strict';

var extend = require('extend');

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

/**
* @type {module:common/util}
* @private
*/
var util = require('../common/util.js');

function Index(search, id) {
this.search_ = search;
this.id = id;
}

Index.prototype.createDocument = function(document, callback) {
var self = this;

this.makeReq_('POST', '/documents', null, document, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
}

var document = self.documentFromApiResp_(resp);

callback(null, document, resp);
});
};

Index.prototype.document = function(id) {
return new Document(this, id);
};

Index.prototype.getDocuments = function(query, callback) {
var self = this;

if (util.is(query, 'function')) {
callback = query;
query = {};
}

this.makeReq_('GET', '/documents', query, null, function(err, resp) {
if (err) {
callback(err, null, null, resp);
return;
}

var nextQuery = null;

if (resp.nextPageToken) {
nextQuery = extend({}, query, {
pageToken: resp.nextPageToken
});
}

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

callback(null, documents, nextQuery, resp);
});
};

Index.prototype.search = function(query, callback) {
var self = this;

if (util.is(query, 'string')) {
query = {
query: query
};
}

this.makeReq_('GET', '/search', query, null, function(err, resp) {
if (err) {
callback(err, null, null, resp);
return;
}

var nextQuery = null;

if (resp.nextPageToken) {
nextQuery = extend({}, query, {
pageToken: resp.nextPageToken
});
}

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

callback(null, documents, nextQuery, resp);
});
};

Index.prototype.documentFromApiResp_ = function(apiResp) {
var document = this.document(apiResp.docId);

if (util.is(apiResp.fields, 'object')) {
document.fields = apiResp.fields;
}

if (util.is(apiResp.rank, 'number')) {
document.rank = apiResp.rank;
}

return document;
};

Index.prototype.makeReq_ = function(method, path, query, body, callback) {
path = '/indexes/' + this.id + path;

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

module.exports = Index;
129 changes: 129 additions & 0 deletions lib/search/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*!
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*!
* @module search
*/

'use strict';

var extend = require('extend');

/**
* @type {module:search/index}
* @private
*/
var Index = require('./index-class.js');

/**
* @type {module:common/util}
* @private
*/
var util = require('../common/util.js');

/**
* @const {string} Base URL for the Search API.
* @private
*/
var SEARCH_BASE_URL = 'https://cloudsearch.googleapis.com/v1/';

/**
* @const {array} Required scopes for the Search API.
* @private
*/
var SCOPES = [
'https://www.googleapis.com/auth/cloud-platform',
'https://www.googleapis.com/auth/cloudsearch',
'https://www.googleapis.com/auth/userinfo.email'
];

function Search(options) {
options = options || {};

if (!options.projectId) {
throw util.missingProjectIdError;
}

this.makeAuthorizedRequest_ = util.makeAuthorizedRequestFactory({
credentials: options.credentials,
keyFile: options.keyFilename,
scopes: SCOPES,
email: options.email
});

this.projectId = options.projectId;
this.projectName = 'projects/' + this.projectId;
}

Search.prototype.getIndexes = function(query, callback) {
var self = this;

if (util.is(query, 'function')) {
callback = query;
query = {};
}

this.makeReq_('GET', '/indexes', query, null, function(err, resp) {
if (err) {
callback(err, null, null, resp);
return;
}

var nextQuery = null;

if (resp.nextPageToken) {
nextQuery = extend({}, query, {
pageToken: resp.nextPageToken
});
}

var indexes = (resp.indexes || []).map(function(indexObject) {
var index = self.index(indexObject.indexId);

if (util.is(resp.indexedField, 'object')) {
index.fields = resp.indexedField;
}

return index;
});

callback(null, indexes, nextQuery, resp);
});
};

Search.prototype.index = function(id) {
return new Index(this, id);
};

Search.prototype.makeReq_ = function(method, path, query, body, callback) {
var reqOpts = {
method: method,
qs: query,
uri: util.format('{base}projects/{projectId}{path}', {
base: SEARCH_BASE_URL,
projectId: this.projectId,
path: path
})
};

if (body) {
reqOpts.json = body;
}

this.makeAuthorizedRequest_(reqOpts, callback);
};

module.exports = Search;
3 changes: 3 additions & 0 deletions scripts/docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
./node_modules/.bin/dox < lib/pubsub/subscription.js > docs/json/master/pubsub/subscription.json &
./node_modules/.bin/dox < lib/pubsub/topic.js > docs/json/master/pubsub/topic.json &

./node_modules/.bin/dox < lib/search/index.js > docs/json/master/search/index.json &


./node_modules/.bin/dox < lib/storage/acl.js > docs/json/master/storage/acl.json &
./node_modules/.bin/dox < lib/storage/bucket.js > docs/json/master/storage/bucket.json &
./node_modules/.bin/dox < lib/storage/file.js > docs/json/master/storage/file.json &
Expand Down
Loading

0 comments on commit 0c6feb4

Please sign in to comment.