-
Notifications
You must be signed in to change notification settings - Fork 668
Expand file tree
/
Copy pathindex.js
More file actions
253 lines (225 loc) · 6.52 KB
/
index.js
File metadata and controls
253 lines (225 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*!
* 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/streamrouter}
* @private
*/
var streamRouter = require('../common/stream-router.js');
/**
* @type {module:common/util}
* @private
*/
var util = require('../common/util.js');
/**
* @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'
];
/**
* @const {string} Base URL for the Search API.
* @private
*/
var SEARCH_BASE_URL = 'https://cloudsearch.googleapis.com/v1/';
/**
* Create a Search object to Interact with the Cloud Search API. Using this
* object, you can access your indexes with {module:search/index} and documents
* with {module:search/document}.
*
* Follow along with the examples to see how to do everything from creating
* documents to searching indexes.
*
* @alias module:search
* @constructor
*
* @param {object} options - [Configuration object](#/docs/?method=gcloud).
*
* @example
* var gcloud = require('gcloud')({
* keyFilename: '/path/to/keyfile.json',
* projectId: 'grape-spaceship-123'
* });
*
* var search = gcloud.search();
*/
function Search(options) {
if (!options || !options.projectId) {
throw util.missingProjectIdError;
}
this.makeAuthorizedRequest_ = util.makeAuthorizedRequestFactory({
credentials: options.credentials,
email: options.email,
keyFile: options.keyFilename,
scopes: SCOPES
});
this.projectId_ = options.projectId;
}
/**
* Get {module:search/index} objects for all of the indexes in your project.
*
* @param {object=} query - Query object.
* @param {boolean} options.autoPaginate - Have pagination handled
* automatically. Default: true.
* @param {string} query.pageSize - The maximum number of indexes to return per
* page. If not specified, 100 indexes are returned per page.
* @param {string} query.pageToken - A previously-returned page token
* representing part of the larger set of results to view.
* @param {string} query.prefix - The prefix of the index name. It is used to
* list all indexes with names that have this prefix.
* @param {string} query.view - See [this table](https://goo.gl/sY6Lpt) for a
* list of accepted values and what each will do.
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request
* @param {module:search/index[]} callback.indexes - A list of Indexes.
* @param {?object} callback.nextQuery - If present, query with this object to
* check for more results.
* @param {object} callback.apiResponse - The full API response.
*
* @example
* search.getIndexes(function(err, indexes) {
* // indexes is an array of Index objects.
* });
*
* //-
* // To control how many API requests are made and page through the results
* // manually, set `autoPaginate` to `false`.
* //-
* function onApiResponse(err, indexes, nextQuery, apiResponse) {
* if (err) {
* console.error(err);
* return;
* }
*
* // indexes is an array of Index objects.
*
* if (nextQuery) {
* search.getIndexes(nextQuery, onApiResponse);
* }
* }
*
* search.getIndexes({
* autoPaginate: false
* }, onApiResponse);
*
* //-
* // Get the indexes as a readable object stream.
* //-
* search.getIndexes()
* .on('error', console.error)
* .on('data', function(index) {
* // index is an Index object.
* })
* .on('end', function() {
* // All indexes retrieved.
* });
*
* //-
* // If you anticipate many results, you can end a stream early to prevent
* // unnecessary processing and API requests.
* //-
* search.getIndexes()
* .on('data', function(index) {
* this.end();
* });
*/
Search.prototype.getIndexes = function(query, callback) {
var self = this;
if (util.is(query, 'function')) {
callback = query;
query = {};
}
query = extend({}, query);
if (query.prefix) {
query.indexNamePrefix = query.prefix;
delete query.prefix;
}
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);
});
};
/**
* Get a reference to a Google Cloud Search index.
*
* @param {string} id - Name of the index.
* @return {module:search/index}
*/
Search.prototype.index = function(id) {
return new Index(this, id);
};
/**
* 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.
*/
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);
};
/*! Developer Documentation
*
* {module:search#getIndexes} can be used with either a callback or as a
* readable object stream. `streamRouter` is used to add this dual behavior.
*/
streamRouter.extend(Search, 'getIndexes');
module.exports = Search;