forked from azure-contrib/node-azure-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
292 lines (259 loc) · 8.04 KB
/
index.js
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
var http = require('https');
var url = require('url');
module.exports = function(options){
if (!options) throw new Error("please supply the options object");
if (!options.url) throw new Error("please supply the url of the search service");
if (!options.key) throw new Error("please supply the key of the search service");
if (!options.version) options.version = "2015-02-28";
var get = function(path, overrides, callback){
execute(path, "GET", null, overrides, callback);
}
var post = function(path, body, callback){
execute(path, "POST", body, null, callback);
}
var put = function(path, body, callback){
execute(path, "PUT", body, null, callback);
}
var del = function(path, callback){
execute(path, "DELETE", null, null, callback);
}
var execute = function(path, method, body, overrides, cb){
path.push({"api-version":options.version});
var payload = "";
if (body) {
payload = JSON.stringify(body);
}
var headers = {
"Content-Type": "application/json; charset=UTF-8",
"api-key": options.key,
"Accept": "application/json",
"Accept-Charset": "UTF-8",
"Content-Length": Buffer.byteLength(payload) // fix
};
if (overrides){
for (var x in overrides){
headers[x] = overrides[x];
}
}
var req = http.request({
host: url.parse(options.url).hostname,
path: "/" + arrayToPath(path),
port: 443,
method: method,
withCredentials: false,
headers: headers,
}, function(res) {
var result = ""
res.setEncoding('utf8');
res.on('data', function(chunk) {
result += chunk;
});
res.on('end', function() {
if (!cb) return;
// Bail out if no result
if(!result){
if(res.statusCode < 200 || res.statusCode > 206){
return cb({ code: res.statusCode}, null, res); // Fail
} else {
return cb(null, null, res); // Success with no body
}
}
// Parse result
try {
if (overrides && overrides.Accept == "text/plain"){
// do nothing
} else {
result = JSON.parse(result);
}
} catch (err) {
return cb("failed to parse JSON:\n" + err + "\n " + result, null, res);
}
// Check for Azure error
var error = result.error;
if (error){
error.code = error.code || res.statusCode; // This is currently not populated by azure
cb(error, null, res);
}
else {
cb(null, result, res);
}
return;
});
res.on('error', function(err){
if (cb){
cb(err, null, res);
cb = undefined;
return;
}
});
});
try {
if (payload) {
req.write(payload);
}
req.end();
req.on('error', function(err){
if (cb){
cb(err, null, req);
cb = undefined;
return;
}
});
} catch (err){
if (cb){
cb(err);
cb = undefined;
return;
}
}
}
return {
listIndexes: function(cb){
get(['indexes'], null, function(err, data){
if (err) return cb(err);
if (data && data.error) return cb(data.error);
cb(null, data.value);
});
},
createIndex : function(schema, cb){
if (!schema) throw new Error("schema is not defined");
post(['indexes'], schema, function(err, data){
if (err) return cb(err);
if (data && data.error) return cb(data.error);
cb(null, data);
});
},
getIndex : function(indexName, cb){
if (!indexName) throw new Error("indexName is not defined");
get(['indexes', indexName], null, function(err, data){
if (err) return cb(err);
if (data && data.error) return cb(data.error);
cb(null, data);
});
},
getIndexStats : function(indexName, cb){
if (!indexName) throw new Error("indexName is not defined");
get(['indexes', indexName, 'stats'], null, function(err, data){
if (err) return cb(err);
if (data && data.error) return cb(data.error);
cb(null, data);
});
},
deleteIndex : function(indexName, cb){
if (!indexName) throw new Error("indexName is not defined");
del(['indexes', indexName],function(err, data){
if (err) return cb(err);
if (data && data.error) return cb(data.error);
cb(null, data);
});
},
addDocuments : function(indexName, documents, cb){
if (!indexName) throw new Error("indexName is not defined");
if (!documents) throw new Error("documents is not defined");
post(['indexes', indexName, "docs", "index"], {value:documents}, function(err, data){
if (err) return cb(err);
if (data && data.error) return cb(data.error);
cb(null, data.value);
});
},
updateDocuments : function(indexName,documents,cb){
if (!indexName) throw new Error("indexName is not defined");
if (!documents) throw new Error("documents is not defined");
for(var i=0; i<documents.length;i++){
documents[i]["@search.action"] = "merge";
}
post(['indexes', indexName, "docs", "index"], {value:documents}, function(err, data){
if (err) return cb(err);
if (data && data.error) return cb(data.error);
cb(null, data.value);
});
},
uploadDocuments : function(indexName,documents,cb){
if (!indexName) throw new Error("indexName is not defined");
if (!documents) throw new Error("documents is not defined");
for(var i=0; i<documents.length;i++){
documents[i]["@search.action"] = "upload";
}
post(['indexes', indexName, "docs", "index"], {value:documents}, function(err, data){
if (err) return cb(err);
if (data && data.error) return cb(data.error);
cb(null, data.value);
});
},
deleteDocuments : function(indexName,keys,cb){
if (!indexName) throw new Error("indexName is not defined");
if (!keys) throw new Error("keys is not defined");
for(var i=0; i<keys.length;i++){
keys[i]["@search.action"] = "delete";
}
post(['indexes', indexName, "docs", "index"], {value:keys}, function(err, data){
if (err) return cb(err);
if (data && data.error) return cb(data.error);
cb(null, data.value);
});
},
search : function(indexName, query, cb){
if (!indexName) throw new Error("indexName is not defined");
if (!query) throw new Error("query is not defined");
get(['indexes', indexName, 'docs', query], null, function(err, results){
if (err) return cb(err);
if (results && results.error) return cb(results.error);
if (query.fullResults) {
cb(null, results);
} else {
cb(null, results.value);
}
});
},
lookup : function (indexName, key, cb){
if (!indexName) throw new Error("indexName is not defined");
if (!key) throw new Error("key is not defined");
get(['indexes', indexName, "docs('" + key + "')"], null, function(err, data){
if (err) return cb(err);
if (data && data.error) return cb(data.error);
cb(null, data);
});
},
count : function (indexName, cb){
if (!indexName) throw new Error("indexName is not defined");
get(['indexes', indexName, 'docs', '$count'], {"Accept" : "text/plain"}, function(err, data){
if (err) return cb(err);
cb(null, parseInt(data.trim()));
});
},
suggest : function(indexName, query, cb){
if (!indexName) throw new Error("indexName is not defined");
if (!query) throw new Error("query is not defined");
get(['indexes', indexName, 'docs', 'suggest', query], null, function(err, results){
if (err) return cb(err);
if (results && results.error) return cb(results.error);
cb(null, results.value);
});
},
updateIndex: function(indexName, schema, cb){
if (!indexName) throw new Error("indexName is not defined");
if (!schema) throw new Error("schema is not defined");
put(['indexes', indexName], schema, function(err, data){
if (err) return cb(err);
if (data && data.error) return cb(data.error);
cb();
});
}
};
}
// converts this ["hello","world", {format:"json"}] into this "hello/world?format=json"
function arrayToPath(array){
var path = array.filter(function(x){
return typeof x !== "object";
});
var filter = array.filter(function(x){
return typeof x === "object"
});
var qs = [];
filter.forEach(function(x){
for (var key in x){
qs.push(key + "=" + encodeURIComponent(x[key]));
}
});
return path.join("/") + "?" + qs.join("&");
}