Skip to content

Commit

Permalink
Better support sorting for csv report based on saved search (opensear…
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongnansu authored Jun 16, 2021
1 parent 9c5d22c commit b6e8d74
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
24 changes: 10 additions & 14 deletions server/routes/utils/dataReportHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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('@')
) {
Expand Down
34 changes: 21 additions & 13 deletions server/routes/utils/savedSearchReportHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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({
Expand All @@ -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,
};
}
Expand Down

0 comments on commit b6e8d74

Please sign in to comment.