diff --git a/factories/esSearcherFactory.js b/factories/esSearcherFactory.js index fa32d34..ea9ad31 100644 --- a/factories/esSearcherFactory.js +++ b/factories/esSearcherFactory.js @@ -225,7 +225,7 @@ // Eg. with params: /_search?size=5&from=5 //esUrlSvc.setParams(uri, self.pagerArgs); - var headers = esUrlSvc.getHeaders(uri); + var headers = esUrlSvc.getHeaders(uri, self.config.customHeaders); activeQueries.count++; return transport.query(url, queryDslWithPagerArgs, headers) @@ -287,6 +287,7 @@ queryText: otherQuery, config: { apiMethod: 'POST', + customHeaders: self.config.customHeaders, numberOfRows: self.config.numberOfRows, version: self.config.version, }, @@ -334,7 +335,7 @@ var self = this; var uri = esUrlSvc.parseUrl(self.url); var url = esUrlSvc.buildExplainUrl(uri, doc); - var headers = esUrlSvc.getHeaders(uri); + var headers = esUrlSvc.getHeaders(uri, self.config.customHeaders); return $http.post(url, { query: self.queryDsl.query }, {headers: headers}) .then(function(response) { diff --git a/factories/searcherFactory.js b/factories/searcherFactory.js index 39b3d10..722e823 100644 --- a/factories/searcherFactory.js +++ b/factories/searcherFactory.js @@ -18,6 +18,7 @@ self.queryText = options.queryText; self.config = options.config; self.type = options.type; + self.customHeaders = options.customHeaders; self.docs = []; self.grouped = {}; diff --git a/factories/settingsValidatorFactory.js b/factories/settingsValidatorFactory.js index 336590b..9279c43 100644 --- a/factories/settingsValidatorFactory.js +++ b/factories/settingsValidatorFactory.js @@ -14,10 +14,11 @@ var Validator = function(settings) { var self = this; - self.searchUrl = settings.searchUrl; - self.searchEngine = settings.searchEngine; - self.apiMethod = settings.apiMethod; - self.version = settings.version; + self.searchUrl = settings.searchUrl; + self.searchEngine = settings.searchEngine; + self.apiMethod = settings.apiMethod; + self.version = settings.version; + self.customHeaders = settings.customHeaders; self.searcher = null; self.fields = []; @@ -45,7 +46,8 @@ '', { version: self.version, - apiMethod: self.apiMethod + apiMethod: self.apiMethod, + customHeaders: self.customHeaders }, self.searchEngine ); diff --git a/package.json b/package.json index d549930..d7ee61a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "splainer-search", - "version": "2.19.0", + "version": "2.20.0", "main": "splainer-search.js", "authors": [ "Doug Turnbull ", diff --git a/services/esUrlSvc.js b/services/esUrlSvc.js index 163c8ba..2067155 100644 --- a/services/esUrlSvc.js +++ b/services/esUrlSvc.js @@ -63,13 +63,18 @@ angular.module('o19s.splainer-search') * for an ES document. * */ - function buildDocUrl (uri, doc) { + function buildDocUrl (uri, doc, addExplain) { var index = doc._index; var type = doc._type; var id = doc._id; var url = self.buildBaseUrl(uri); - url = url + '/' + index + '/' + type + '/' + id; + + if (type) { + url = url + '/' + index + '/' + type + '/' + id + (addExplain ? '/_explain' : ''); + } else { + url = url + '/' + index + '/' + (addExplain ? '_explain/' : '') + id; + } return url; } @@ -80,13 +85,12 @@ angular.module('o19s.splainer-search') * Builds ES URL of the form [protocol]://[host][:port]/[index]/[type]/[id]/_explain * for an ES document. * + * For newer versions of ES the format has changed as doc types are deprecated: + * [protocol]://[host][:port]/[index]/_explain/[id] + * */ function buildExplainUrl (uri, doc) { - var docUrl = self.buildDocUrl(uri, doc); - - var url = docUrl + '/_explain'; - - return url; + return buildDocUrl(uri, doc, true); } /** @@ -142,10 +146,14 @@ angular.module('o19s.splainer-search') uri.params = params; } - function getHeaders (uri) { + function getHeaders (uri, customHeaders) { var headers = {}; + customHeaders = customHeaders || ''; - if ( angular.isDefined(uri.username) && uri.username !== '' && + if (customHeaders.length > 0) { + // TODO: Validate before saving? Or throw exception when this is called + headers = JSON.parse(customHeaders); + } else if ( angular.isDefined(uri.username) && uri.username !== '' && angular.isDefined(uri.password) && uri.password !== '') { var authorization = 'Basic ' + btoa(uri.username + ':' + uri.password); headers = { 'Authorization': authorization }; @@ -157,7 +165,7 @@ angular.module('o19s.splainer-search') function isBulkCall (uri) { return uri.pathname.endsWith('_msearch'); } - + function isTemplateCall (uri) { return uri.pathname.endsWith('_search/template'); }