Skip to content

Commit

Permalink
refactor(exposeInternalDebugTools): simplify code & cover additional …
Browse files Browse the repository at this point in the history
…hosts formats (#1476)
  • Loading branch information
missinglink authored Jul 22, 2020
1 parent 4e92f1e commit 7453a5d
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions controller/search.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const _ = require('lodash');
const querystring = require('querystring');

const searchService = require('../service/search');
const logger = require('pelias-logger').get('api');
const logging = require( '../helper/logging' );
Expand All @@ -12,7 +10,7 @@ function isRequestTimeout(err) {
}

function setup( peliasConfig, esclient, query, should_execute ){
const apiConfig = (peliasConfig || {}).api || {};
const apiConfig = _.get(peliasConfig, 'api', {});

function controller( req, res, next ){
if (!should_execute(req, res)) {
Expand Down Expand Up @@ -56,25 +54,26 @@ function setup( peliasConfig, esclient, query, should_execute ){
body: renderedQuery.body
};

if (req.clean.enableElasticExplain) {
// support for the 'clean.enableElasticExplain' config flag
if (_.get(req, 'clean.enableElasticExplain') === true) {
cmd.explain = true;
}

// support for the 'clean.exposeInternalDebugTools' config flag
let debugUrl;
if (req.clean.exposeInternalDebugTools &&
peliasConfig.esclient &&
Array.isArray(peliasConfig.esclient.hosts) &&
peliasConfig.esclient.hosts.length > 0) {
const firstEsHostEntry = peliasConfig.esclient.hosts[0];
const esHost = firstEsHostEntry.host ?
`${firstEsHostEntry.protocol}://${firstEsHostEntry.host}:${firstEsHostEntry.port}` : firstEsHostEntry;

debugUrl =
`${esHost}/${apiConfig.indexName}/_search?` +
querystring.stringify({
source_content_type: 'application/json',
source: JSON.stringify(cmd.body)
});
if (_.get(req, 'clean.exposeInternalDebugTools') === true) {

// select a random elasticsearch host to use for 'exposeInternalDebugTools' actions
const host = _.first(esclient.transport.connectionPool.getConnections(null, 1)).host;

// generate a URL which opens this query directly in elasticsearch
debugUrl = host.makeUrl({
path: `${apiConfig.indexName}/_search`,
query: {
source_content_type: 'application/json',
source: JSON.stringify(cmd.body)
}
});
}

debugLog.push(req, {
Expand Down

0 comments on commit 7453a5d

Please sign in to comment.