-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathesSearcherFactory.js
487 lines (409 loc) · 15 KB
/
esSearcherFactory.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
'use strict';
/*jslint latedef:false*/
(function() {
angular.module('o19s.splainer-search')
.factory('EsSearcherFactory', [
'$http',
'$q',
'$log',
'EsDocFactory',
'activeQueries',
'esSearcherPreprocessorSvc',
'esUrlSvc',
'SearcherFactory',
'transportSvc',
EsSearcherFactory
]);
function EsSearcherFactory(
$http, $q, $log,
EsDocFactory,
activeQueries,
esSearcherPreprocessorSvc, esUrlSvc,
SearcherFactory,
transportSvc
) {
var Searcher = function(options) {
SearcherFactory.call(this, options, esSearcherPreprocessorSvc);
};
Searcher.prototype = Object.create(SearcherFactory.prototype);
Searcher.prototype.constructor = Searcher; // Reset the constructor
Searcher.prototype.addDocToGroup = addDocToGroup;
Searcher.prototype.pager = pager;
Searcher.prototype.search = search;
Searcher.prototype.explainOther = explainOther;
Searcher.prototype.explain = explain;
Searcher.prototype.majorVersion = majorVersion;
Searcher.prototype.isTemplateCall = isTemplateCall;
Searcher.prototype.renderTemplate = renderTemplate;
function addDocToGroup (groupedBy, group, solrDoc) {
/*jslint validthis:true*/
var self = this;
if (!self.grouped.hasOwnProperty(groupedBy)) {
self.grouped[groupedBy] = [];
}
var found = null;
angular.forEach(self.grouped[groupedBy], function(groupedDocs) {
if (groupedDocs.value === group && !found) {
found = groupedDocs;
}
});
if (!found) {
found = {docs:[], value:group};
self.grouped[groupedBy].push(found);
}
found.docs.push(solrDoc);
}
// return a new searcher that will give you
// the next page upon search(). To get the subsequent
// page, call pager on that searcher ad infinidum
function pager () {
/*jslint validthis:true*/
var self = this;
var pagerArgs = { from: 0, size: self.config.numberOfRows };
var nextArgs = angular.copy(self.args);
if (nextArgs.hasOwnProperty('pager') && nextArgs.pager !== undefined) {
pagerArgs = nextArgs.pager;
} else if (self.hasOwnProperty('pagerArgs') && self.pagerArgs !== undefined) {
pagerArgs = self.pagerArgs;
}
if (pagerArgs.hasOwnProperty('from')) {
pagerArgs.from = parseInt(pagerArgs.from) + pagerArgs.size;
if (pagerArgs.from >= self.numFound) {
return null; // no more results
}
} else {
pagerArgs.from = pagerArgs.size;
}
nextArgs.pager = pagerArgs;
var options = {
args: nextArgs,
config: self.config,
fieldList: self.fieldList,
queryText: self.queryText,
type: self.type,
url: self.url,
};
var nextSearcher = new Searcher(options);
return nextSearcher;
}
// search (execute the query) and produce results
// to the returned future
function search () {
/*jslint validthis:true*/
var self = this;
var uri = esUrlSvc.parseUrl(self.url);
var apiMethod = self.config.apiMethod;
var proxyUrl = self.config.proxyUrl;
if ( esUrlSvc.isBulkCall(uri) ) {
apiMethod = 'BULK';
}
var templateCall = isTemplateCall(self.args);
if (templateCall){
uri.pathname = uri.pathname + '/template';
}
// Using templates assumes that the _source field is defined
// in the template, not passed in
if (apiMethod === 'GET' && !templateCall) {
var fieldList = (self.fieldList === '*') ? '*' : self.fieldList.join(',');
esUrlSvc.setParams(uri, {
_source: fieldList,
});
}
var url = esUrlSvc.buildUrl(uri);
var transport = transportSvc.getTransport({apiMethod: apiMethod, proxyUrl: proxyUrl});
var queryDslWithPagerArgs = angular.copy(self.queryDsl);
if (self.pagerArgs) {
if (templateCall) {
queryDslWithPagerArgs.params.from = self.pagerArgs.from;
queryDslWithPagerArgs.params.size = self.pagerArgs.size;
}
else {
queryDslWithPagerArgs.from = self.pagerArgs.from;
queryDslWithPagerArgs.size = self.pagerArgs.size;
}
}
if (templateCall) {
delete queryDslWithPagerArgs._source;
delete queryDslWithPagerArgs.highlight;
} else if (self.config.highlight===false) {
delete queryDslWithPagerArgs.highlight;
}
self.inError = false;
var getExplData = function(doc) {
if (doc.hasOwnProperty('_explanation')) {
return doc._explanation;
}
else {
return null;
}
};
var getHlData = function(doc) {
if (doc.hasOwnProperty('highlight')) {
return doc.highlight;
} else {
return null;
}
};
var getQueryParsingData = function(data) {
if (data.hasOwnProperty('profile')) {
return data.profile;
}
else {
return {};
}
};
var formatError = function(msg) {
var errorMsg = '';
if (msg) {
if (msg.status >= 400) {
errorMsg = 'HTTP Error: ' + msg.status + ' ' + msg.statusText;
}
if (msg.status > 0) {
if (msg.hasOwnProperty('data') && msg.data) {
if (msg.data.hasOwnProperty('error')) {
errorMsg += '\n' + JSON.stringify(msg.data.error, null, 2);
}
if (msg.data.hasOwnProperty('_shards')) {
angular.forEach(msg.data._shards.failures, function(failure) {
errorMsg += '\n' + JSON.stringify(failure, null, 2);
});
}
}
}
else if (msg.status === -1 || msg.status === 0) {
errorMsg += 'Network Error! (host not found)\n';
errorMsg += '\n';
errorMsg += 'or CORS needs to be configured for your Elasticsearch\n';
errorMsg += '\n';
errorMsg += 'Enable CORS in elasticsearch.yml:\n';
errorMsg += '\n';
errorMsg += 'http.cors.allow-origin: "/https?:\\\\/\\\\/(.*?\\\\.)?(quepid\\\\.com|splainer\\\\.io)/"\n';
errorMsg += 'http.cors.enabled: true\n';
}
msg.searchError = errorMsg;
}
return msg;
};
// Build URL with params if any
// Eg. without params: /_search
// Eg. with params: /_search?size=5&from=5
//esUrlSvc.setParams(uri, self.pagerArgs);
var headers = esUrlSvc.getHeaders(uri, self.config.customHeaders);
activeQueries.count++;
return transport.query(url, queryDslWithPagerArgs, headers)
.then(function success(httpConfig) {
var data = httpConfig.data;
activeQueries.count--;
if (data.hits.hasOwnProperty('total') && data.hits.total.hasOwnProperty('value')) {
self.numFound = data.hits.total.value;
}
else {
self.numFound = data.hits.total;
}
self.parsedQueryDetails = getQueryParsingData(data);
var parseDoc = function(doc, groupedBy, group) {
var explDict = getExplData(doc);
var hlDict = getHlData(doc);
var options = {
groupedBy: groupedBy,
group: group,
fieldList: self.fieldList,
url: self.url,
explDict: explDict,
hlDict: hlDict,
version: self.majorVersion(),
};
return new EsDocFactory(doc, options);
};
angular.forEach(data.hits.hits, function(hit) {
var doc = parseDoc(hit);
self.docs.push(doc);
});
if ( angular.isDefined(data._shards) && data._shards.failed > 0 ) {
return $q.reject(formatError(httpConfig));
}
}, function error(msg) {
activeQueries.count--;
self.inError = true;
return $q.reject(formatError(msg));
})
.catch(function(response) {
$log.debug('Failed to execute search');
return $q.reject(response);
});
} // end of search()
function explainOther (otherQuery) {
/*jslint validthis:true*/
var self = this;
var otherSearcherOptions = {
fieldList: self.fieldList,
url: self.url,
args: self.args,
queryText: otherQuery,
config: {
apiMethod: 'POST',
customHeaders: self.config.customHeaders,
numberOfRows: self.config.numberOfRows,
version: self.config.version,
},
type: self.type,
};
if ( angular.isDefined(self.pagerArgs) && self.pagerArgs !== null ) {
otherSearcherOptions.args.pager = self.pagerArgs;
}
var otherSearcher = new Searcher(otherSearcherOptions);
return otherSearcher.search()
.then(function() {
self.numFound = otherSearcher.numFound;
var defer = $q.defer();
var promises = [];
var docs = [];
angular.forEach(otherSearcher.docs, function(doc) {
var promise = self.explain(doc)
.then(function(parsedDoc) {
docs.push(parsedDoc);
});
promises.push(promise);
});
$q.all(promises)
.then(function () {
self.docs = docs;
defer.resolve();
});
return defer.promise;
}).catch(function(response) {
$log.debug('Failed to run explainOther');
return response;
});
} // end of explainOther()
function explain(doc) {
/*jslint validthis:true*/
var self = this;
var uri = esUrlSvc.parseUrl(self.url);
var url = esUrlSvc.buildExplainUrl(uri, doc);
var headers = esUrlSvc.getHeaders(uri, self.config.customHeaders);
return $http.post(url, { query: self.queryDsl.query }, {headers: headers})
.then(function(response) {
var explDict = response.data.explanation || null;
var options = {
fieldList: self.fieldList,
url: self.url,
explDict: explDict,
};
return new EsDocFactory(doc, options);
}).catch(function(response) {
$log.debug('Failed to run explain');
return response;
});
} // end of explain()
// Let us track a version if need to make decisions if one version is different than another.
function majorVersion() {
var self = this;
if ( angular.isDefined(self.config) &&
angular.isDefined(self.config.version) &&
self.config.version !== null &&
self.config.version !== ''
) {
return parseInt(self.config.version.split('.')[0]);
} else {
return null;
}
} // end of majorVersion()
// Templatized queries require us to add a /template to the url.
// I feel like this args parameter should be replaced wiht self.args.
function isTemplateCall(args) {
return esUrlSvc.isTemplateCall(args);
}
// Templatized queries require us to add a /template to the url.
// Lots of refactoring between this method and the search() method!
function renderTemplate() {
/*jslint validthis:true*/
var self = this;
var uri = esUrlSvc.parseUrl(self.url);
var apiMethod = self.config.apiMethod;
var proxyUrl = self.config.proxyUrl;
var templateCall = isTemplateCall(self.args);
//if (templateCall){
// uri.pathname = uri.pathname + '/template';
//}
// Using templates assumes that the _source field is defined
// in the template, not passed in
//if (apiMethod === 'GET' && !templateCall) {
// var fieldList = (self.fieldList === '*') ? '*' : self.fieldList.join(',');
// esUrlSvc.setParams(uri, {
// _source: fieldList,
// });
//}
var url = esUrlSvc.buildRenderTemplateUrl(uri);
var transport = transportSvc.getTransport({apiMethod: apiMethod, proxyUrl: proxyUrl});
var queryDslWithPagerArgs = angular.copy(self.queryDsl);
if (self.pagerArgs) {
if (templateCall) {
queryDslWithPagerArgs.params.from = self.pagerArgs.from;
queryDslWithPagerArgs.params.size = self.pagerArgs.size;
}
else {
queryDslWithPagerArgs.from = self.pagerArgs.from;
queryDslWithPagerArgs.size = self.pagerArgs.size;
}
}
if (templateCall) {
delete queryDslWithPagerArgs._source;
delete queryDslWithPagerArgs.highlight;
} else if (self.config.highlight===false) {
delete queryDslWithPagerArgs.highlight;
}
self.inError = false;
var formatError = function(msg) {
var errorMsg = '';
if (msg) {
if (msg.status >= 400) {
errorMsg = 'HTTP Error: ' + msg.status + ' ' + msg.statusText;
}
if (msg.status > 0) {
if (msg.hasOwnProperty('data') && msg.data) {
if (msg.data.hasOwnProperty('error')) {
errorMsg += '\n' + JSON.stringify(msg.data.error, null, 2);
}
if (msg.data.hasOwnProperty('_shards')) {
angular.forEach(msg.data._shards.failures, function(failure) {
errorMsg += '\n' + JSON.stringify(failure, null, 2);
});
}
}
}
else if (msg.status === -1 || msg.status === 0) {
errorMsg += 'Network Error! (host not found)\n';
errorMsg += '\n';
errorMsg += 'or CORS needs to be configured for your Elasticsearch\n';
errorMsg += '\n';
errorMsg += 'Enable CORS in elasticsearch.yml:\n';
errorMsg += '\n';
errorMsg += 'http.cors.allow-origin: "/https?:\\\\/\\\\/(.*?\\\\.)?(quepid\\\\.com|splainer\\\\.io)/"\n';
errorMsg += 'http.cors.enabled: true\n';
}
msg.searchError = errorMsg;
}
return msg;
};
var headers = esUrlSvc.getHeaders(uri, self.config.customHeaders);
activeQueries.count++;
return transport.query(url, queryDslWithPagerArgs, headers)
.then(function success(httpConfig) {
var data = httpConfig.data;
activeQueries.count--;
self.renderedTemplateJson = data;
}, function error(msg) {
activeQueries.count--;
self.inError = true;
return $q.reject(formatError(msg));
})
.catch(function(response) {
$log.debug('Failed to render template');
return $q.reject(response);
});
}
// Return factory object
return Searcher;
}
})();