Skip to content

Commit 0c6feb4

Browse files
search: introduce api
1 parent 01ec51f commit 0c6feb4

File tree

8 files changed

+567
-0
lines changed

8 files changed

+567
-0
lines changed

lib/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ var Datastore = require('./datastore');
3838
*/
3939
var PubSub = require('./pubsub');
4040

41+
/**
42+
* @type {module:search}
43+
* @private
44+
*/
45+
var Search = require('./search');
46+
4147
/**
4248
* @type {module:storage}
4349
* @private
@@ -120,6 +126,10 @@ function gcloud(config) {
120126
options = options || {};
121127
return new PubSub(util.extendGlobalConfig(config, options));
122128
},
129+
search: function(options) {
130+
options = options || {};
131+
return new Search(util.extendGlobalConfig(config, options));
132+
},
123133
storage: function(options) {
124134
options = options || {};
125135
return new Storage(util.extendGlobalConfig(config, options));
@@ -194,6 +204,10 @@ gcloud.pubsub = function(config) {
194204
return new PubSub(config);
195205
};
196206

207+
gcloud.search = function (config) {
208+
return new Search(config);
209+
};
210+
197211
/**
198212
* Google Cloud Storage allows you to store data on Google infrastructure.
199213
* Read [Google Cloud Storage API docs](https://developers.google.com/storage/)

lib/search/document.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*!
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*!
18+
* @module search/document
19+
*/
20+
21+
'use strict';
22+
23+
/**
24+
* @type {module:common/util}
25+
* @private
26+
*/
27+
var util = require('../common/util.js');
28+
29+
function Document(index, id) {
30+
this.search_ = index.search_;
31+
this.index_ = index;
32+
this.id = id;
33+
}
34+
35+
Document.prototype.delete = function(callback) {
36+
callback = callback || util.noop;
37+
this.makeReq_('DELETE', '', null, null, callback);
38+
};
39+
40+
Document.prototype.getMetadata = function(callback) {
41+
var self = this;
42+
43+
callback = callback || util.noop;
44+
45+
this.makeReq_('GET', '/', null, null, function(err, resp) {
46+
if (err) {
47+
callback(err, null, resp);
48+
return;
49+
}
50+
51+
if (util.is(resp.fields, 'object')) {
52+
self.fields = resp.fields;
53+
}
54+
55+
if (util.is(resp.rank, 'number')) {
56+
self.rank = resp.rank;
57+
}
58+
59+
callback(null, self, resp);
60+
});
61+
};
62+
63+
Document.prototype.makeReq_ = function(method, path, query, body, callback) {
64+
path = '/documents/' + this.id + path;
65+
66+
this.index_.makeReq_(method, path, query, body, callback);
67+
};
68+
69+
module.exports = Document;

lib/search/index-class.js

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*!
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*!
18+
* @module search/index
19+
*/
20+
21+
'use strict';
22+
23+
var extend = require('extend');
24+
25+
/**
26+
* @type {module:search/document}
27+
* @private
28+
*/
29+
var Document = require('./document.js');
30+
31+
/**
32+
* @type {module:common/util}
33+
* @private
34+
*/
35+
var util = require('../common/util.js');
36+
37+
function Index(search, id) {
38+
this.search_ = search;
39+
this.id = id;
40+
}
41+
42+
Index.prototype.createDocument = function(document, callback) {
43+
var self = this;
44+
45+
this.makeReq_('POST', '/documents', null, document, function(err, resp) {
46+
if (err) {
47+
callback(err, null, resp);
48+
return;
49+
}
50+
51+
var document = self.documentFromApiResp_(resp);
52+
53+
callback(null, document, resp);
54+
});
55+
};
56+
57+
Index.prototype.document = function(id) {
58+
return new Document(this, id);
59+
};
60+
61+
Index.prototype.getDocuments = function(query, callback) {
62+
var self = this;
63+
64+
if (util.is(query, 'function')) {
65+
callback = query;
66+
query = {};
67+
}
68+
69+
this.makeReq_('GET', '/documents', query, null, function(err, resp) {
70+
if (err) {
71+
callback(err, null, null, resp);
72+
return;
73+
}
74+
75+
var nextQuery = null;
76+
77+
if (resp.nextPageToken) {
78+
nextQuery = extend({}, query, {
79+
pageToken: resp.nextPageToken
80+
});
81+
}
82+
83+
var documents = (resp.documents || [])
84+
.map(self.documentFromApiResp_.bind(self));
85+
86+
callback(null, documents, nextQuery, resp);
87+
});
88+
};
89+
90+
Index.prototype.search = function(query, callback) {
91+
var self = this;
92+
93+
if (util.is(query, 'string')) {
94+
query = {
95+
query: query
96+
};
97+
}
98+
99+
this.makeReq_('GET', '/search', query, null, function(err, resp) {
100+
if (err) {
101+
callback(err, null, null, resp);
102+
return;
103+
}
104+
105+
var nextQuery = null;
106+
107+
if (resp.nextPageToken) {
108+
nextQuery = extend({}, query, {
109+
pageToken: resp.nextPageToken
110+
});
111+
}
112+
113+
var documents = (resp.results || [])
114+
.map(self.documentFromApiResp_.bind(self));
115+
116+
callback(null, documents, nextQuery, resp);
117+
});
118+
};
119+
120+
Index.prototype.documentFromApiResp_ = function(apiResp) {
121+
var document = this.document(apiResp.docId);
122+
123+
if (util.is(apiResp.fields, 'object')) {
124+
document.fields = apiResp.fields;
125+
}
126+
127+
if (util.is(apiResp.rank, 'number')) {
128+
document.rank = apiResp.rank;
129+
}
130+
131+
return document;
132+
};
133+
134+
Index.prototype.makeReq_ = function(method, path, query, body, callback) {
135+
path = '/indexes/' + this.id + path;
136+
137+
this.search_.makeReq_(method, path, query, body, callback);
138+
};
139+
140+
module.exports = Index;

lib/search/index.js

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*!
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*!
18+
* @module search
19+
*/
20+
21+
'use strict';
22+
23+
var extend = require('extend');
24+
25+
/**
26+
* @type {module:search/index}
27+
* @private
28+
*/
29+
var Index = require('./index-class.js');
30+
31+
/**
32+
* @type {module:common/util}
33+
* @private
34+
*/
35+
var util = require('../common/util.js');
36+
37+
/**
38+
* @const {string} Base URL for the Search API.
39+
* @private
40+
*/
41+
var SEARCH_BASE_URL = 'https://cloudsearch.googleapis.com/v1/';
42+
43+
/**
44+
* @const {array} Required scopes for the Search API.
45+
* @private
46+
*/
47+
var SCOPES = [
48+
'https://www.googleapis.com/auth/cloud-platform',
49+
'https://www.googleapis.com/auth/cloudsearch',
50+
'https://www.googleapis.com/auth/userinfo.email'
51+
];
52+
53+
function Search(options) {
54+
options = options || {};
55+
56+
if (!options.projectId) {
57+
throw util.missingProjectIdError;
58+
}
59+
60+
this.makeAuthorizedRequest_ = util.makeAuthorizedRequestFactory({
61+
credentials: options.credentials,
62+
keyFile: options.keyFilename,
63+
scopes: SCOPES,
64+
email: options.email
65+
});
66+
67+
this.projectId = options.projectId;
68+
this.projectName = 'projects/' + this.projectId;
69+
}
70+
71+
Search.prototype.getIndexes = function(query, callback) {
72+
var self = this;
73+
74+
if (util.is(query, 'function')) {
75+
callback = query;
76+
query = {};
77+
}
78+
79+
this.makeReq_('GET', '/indexes', query, null, function(err, resp) {
80+
if (err) {
81+
callback(err, null, null, resp);
82+
return;
83+
}
84+
85+
var nextQuery = null;
86+
87+
if (resp.nextPageToken) {
88+
nextQuery = extend({}, query, {
89+
pageToken: resp.nextPageToken
90+
});
91+
}
92+
93+
var indexes = (resp.indexes || []).map(function(indexObject) {
94+
var index = self.index(indexObject.indexId);
95+
96+
if (util.is(resp.indexedField, 'object')) {
97+
index.fields = resp.indexedField;
98+
}
99+
100+
return index;
101+
});
102+
103+
callback(null, indexes, nextQuery, resp);
104+
});
105+
};
106+
107+
Search.prototype.index = function(id) {
108+
return new Index(this, id);
109+
};
110+
111+
Search.prototype.makeReq_ = function(method, path, query, body, callback) {
112+
var reqOpts = {
113+
method: method,
114+
qs: query,
115+
uri: util.format('{base}projects/{projectId}{path}', {
116+
base: SEARCH_BASE_URL,
117+
projectId: this.projectId,
118+
path: path
119+
})
120+
};
121+
122+
if (body) {
123+
reqOpts.json = body;
124+
}
125+
126+
this.makeAuthorizedRequest_(reqOpts, callback);
127+
};
128+
129+
module.exports = Search;

scripts/docs.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
./node_modules/.bin/dox < lib/pubsub/subscription.js > docs/json/master/pubsub/subscription.json &
3232
./node_modules/.bin/dox < lib/pubsub/topic.js > docs/json/master/pubsub/topic.json &
3333

34+
./node_modules/.bin/dox < lib/search/index.js > docs/json/master/search/index.json &
35+
36+
3437
./node_modules/.bin/dox < lib/storage/acl.js > docs/json/master/storage/acl.json &
3538
./node_modules/.bin/dox < lib/storage/bucket.js > docs/json/master/storage/bucket.json &
3639
./node_modules/.bin/dox < lib/storage/file.js > docs/json/master/storage/file.json &

0 commit comments

Comments
 (0)