Skip to content

Commit

Permalink
Merge pull request #116 from aditya-kanekar/elasticcloud-support
Browse files Browse the repository at this point in the history
Added support for API key auth, fixes issues with explain missing on newer ES versions.
  • Loading branch information
worleydl authored Oct 13, 2022
2 parents 94b2fe1 + e827721 commit 2865fbe
Showing 5 changed files with 30 additions and 18 deletions.
5 changes: 3 additions & 2 deletions factories/esSearcherFactory.js
Original file line number Diff line number Diff line change
@@ -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) {
1 change: 1 addition & 0 deletions factories/searcherFactory.js
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@
self.queryText = options.queryText;
self.config = options.config;
self.type = options.type;
self.customHeaders = options.customHeaders;

self.docs = [];
self.grouped = {};
12 changes: 7 additions & 5 deletions factories/settingsValidatorFactory.js
Original file line number Diff line number Diff line change
@@ -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
);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "splainer-search",
"version": "2.19.0",
"version": "2.20.0",
"main": "splainer-search.js",
"authors": [
"Doug Turnbull <[email protected]>",
28 changes: 18 additions & 10 deletions services/esUrlSvc.js
Original file line number Diff line number Diff line change
@@ -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');
}

0 comments on commit 2865fbe

Please sign in to comment.