Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
No documents for aggregations count as false (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbbaughe authored May 7, 2019
1 parent 5d4fb11 commit 8c1cef0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,24 @@ export function formikToCondition(values, monitorUiMetadata = {}) {
const aggregationType = _.get(monitorUiMetadata, 'search.aggregationType', 'count');

if (searchType === SEARCH_TYPE.QUERY) return { script: values.script };

const resultsPath = getResultsPath(aggregationType);
const isCount = aggregationType === 'count';
const resultsPath = getResultsPath(isCount);
const operator = getOperator(thresholdEnum);
return getCondition(resultsPath, operator, thresholdValue);
return getCondition(resultsPath, operator, thresholdValue, isCount);
}

export function getCondition(resultsPath, operator, value) {
return { script: { lang: 'painless', source: `${resultsPath} ${operator} ${value}` } };
export function getCondition(resultsPath, operator, value, isCount) {
const baseSource = `${resultsPath} ${operator} ${value}`;
return {
script: {
lang: 'painless',
source: isCount ? baseSource : `return ${resultsPath} == null ? false : ${baseSource}`,
}
};
}

export function getResultsPath(aggregationType) {
if (aggregationType === 'count') return HITS_TOTAL_RESULTS_PATH;
return AGGREGATION_RESULTS_PATH;
export function getResultsPath(isCount) {
return isCount ? HITS_TOTAL_RESULTS_PATH : AGGREGATION_RESULTS_PATH;
}

export function getOperator(thresholdEnum) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('formikToCondition', () => {
expect(
formikToCondition(formikValues, { search: { searchType: 'graph', aggregationType: 'max' } })
).toEqual({
script: { lang: 'painless', source: `ctx.results[0].aggregations.when.value > 10000` },
script: { lang: 'painless', source: `return ctx.results[0].aggregations.when.value == null ? false : ctx.results[0].aggregations.when.value > 10000` },
});
});
});

0 comments on commit 8c1cef0

Please sign in to comment.