diff --git a/server/routes/utils/dataReportHelpers.ts b/server/routes/utils/dataReportHelpers.ts index 3840121b..6c6fc1c0 100644 --- a/server/routes/utils/dataReportHelpers.ts +++ b/server/routes/utils/dataReportHelpers.ts @@ -26,7 +26,7 @@ import { DATA_REPORT_CONFIG } from './constants'; -import esb from 'elastic-builder'; +import esb, { Sort } from 'elastic-builder'; import moment from 'moment'; import converter from 'json-2-csv'; import _ from 'lodash'; @@ -61,7 +61,7 @@ export const getSelectedFields = async (columns) => { metaData.selectedFields = selectedFields; }; -//Build the OpenSearch query from the meta data +// Build the OpenSearch query from the meta data // is_count is set to 1 if we building the count query but 0 if we building the fetch data query export const buildQuery = (report, is_count) => { let requestBody = esb.boolQuery(); @@ -149,14 +149,10 @@ export const buildQuery = (report, is_count) => { let reqBody = esb.requestBodySearch().query(requestBody).version(true); if (report._source.sorting.length > 0) { - if (report._source.sorting.length === 1) - reqBody.sort( - esb.sort(report._source.sorting[0][0], report._source.sorting[0][1]) - ); - else - reqBody.sort( - esb.sort(report._source.sorting[0], report._source.sorting[1]) - ); + const sortings: Sort[] = report._source.sorting.map((element: string[]) => { + return esb.sort(element[0], element[1]); + }); + reqBody.sorts(sortings); } //get the selected fields only @@ -172,7 +168,7 @@ export const getOpenSearchData = (arrayHits, report, params) => { for (let valueRes of arrayHits) { for (let data of valueRes.hits) { const fields = data.fields; - //get all the fields of type date and fromat them to excel format + // get all the fields of type date and format them to excel format for (let dateType of report._source.dateFields) { if (data._source[dateType]) { data._source[dateType] = moment(fields[dateType][0]).format( @@ -249,11 +245,11 @@ function traverse(data, keys, result = {}) { */ function sanitize(doc: any) { for (const field in doc) { - if (doc[field] == null) - continue + if (doc[field] == null) continue; if ( doc[field].toString().startsWith('+') || - (doc[field].toString().startsWith('-') && typeof doc[field] !== "number") || + (doc[field].toString().startsWith('-') && + typeof doc[field] !== 'number') || doc[field].toString().startsWith('=') || doc[field].toString().startsWith('@') ) { diff --git a/server/routes/utils/savedSearchReportHelper.ts b/server/routes/utils/savedSearchReportHelper.ts index 140f38ac..ac2802c0 100644 --- a/server/routes/utils/savedSearchReportHelper.ts +++ b/server/routes/utils/savedSearchReportHelper.ts @@ -37,6 +37,8 @@ import { } from '../../../../../src/core/server'; import { getFileName, callCluster } from './helpers'; import { CreateReportResultType } from './types'; +import { RequestParams } from '@elastic/elasticsearch'; +import esb from 'elastic-builder'; /** * Specify how long scroll context should be maintained for scrolled search @@ -200,16 +202,17 @@ async function generateReportData( } async function getOpenSearchDataByScroll() { + const searchParams: RequestParams.Search = { + index: report._source.paternName, + scroll: scrollTimeout, + body: reqBody, + size: maxResultSize, + }; // Open scroll context by fetching first batch opensearchData = await callCluster( client, 'search', - { - index: report._source.paternName, - scroll: scrollTimeout, - body: reqBody, - size: maxResultSize, - }, + searchParams, isScheduledTask ); arrayHits.push(opensearchData.hits); @@ -243,20 +246,23 @@ async function generateReportData( } async function getOpenSearchDataBySearch() { + const searchParams: RequestParams.Search = { + index: report._source.paternName, + body: reqBody, + size: total, + }; + opensearchData = await callCluster( client, 'search', - { - index: report._source.paternName, - body: reqBody, - size: total, - }, + searchParams, isScheduledTask ); + arrayHits.push(opensearchData.hits); } - function buildRequestBody(query: any) { + function buildRequestBody(query: esb.RequestBodySearch) { const docvalues = []; for (const dateType of report._source.dateFields) { docvalues.push({ @@ -265,8 +271,10 @@ async function generateReportData( }); } + // elastic-builder doesn't provide function to build docvalue_fields with format, + // this is a workaround which appends docvalues field to the request body. return { - query: query.toJSON().query, + ...query.toJSON(), docvalue_fields: docvalues, }; }