Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions Controllers/dataset.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ const cache = require('../Components/cache');
const config = require('../Config');
const path = require('path');
const { Parser } = require('json2csv');
const { datasetFields } = require('../Utils/datasetFields');
const { DATASET_DEFAULT_SORT_FIELD, datasetFields } = require('../Utils/datasetFields');
const datasetService = require('../Services/dataset.service');

const search = async (req, res) => {
const body = req.body;
const data = {};
const filters = body.filters ?? {};
const filters = body.filters && typeof body.filters === 'object' && !Array.isArray(body.filters) ? body.filters : {};
const options = {};
const pageInfo = body.pageInfo ?? {page: 1, pageSize: 10};
const searchText = body.search_text?.trim() ?? '';
const sort = body.sort ?? {k: 'dbGaP_phs', v: 'asc'};
const searchText = body.search_text && typeof body.search_text === 'string' ? body.search_text.trim() : '';
const sort = body.sort ?? {k: DATASET_DEFAULT_SORT_FIELD, v: 'asc'};

if (pageInfo.page !== parseInt(pageInfo.page, 10) || pageInfo.page <= 0) {
pageInfo.page = 1;
Expand All @@ -37,7 +37,7 @@ const search = async (req, res) => {
// sort.name = "Resource";
// sort.k = "data_resource_id";
// }
if (!(sort.v && ['asc', 'desc'].includes(sort.v))) {
if (sort?.v && !['asc', 'desc'].includes(sort?.v)) {
sort.v = 'asc';
}

Expand All @@ -47,6 +47,15 @@ const search = async (req, res) => {
data.pageInfo = options.pageInfo;

const searchResult = await datasetService.search(searchText, filters, options);
if (searchResult.error) {
res.json({
status:"error",
aggs: 'all',
data: {},
error: searchResult.error,
});
return;
}

if (searchResult.total !== 0 && (options.pageInfo.page - 1) * options.pageInfo.pageSize >= searchResult.total) {
let lastPage = Math.ceil(searchResult.total / options.pageInfo.pageSize);
Expand Down
12 changes: 11 additions & 1 deletion Services/dataset.service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const config = require('../Config');
const elasticsearch = require('../Components/elasticsearch');
const cache = require('../Components/cache');
const logger = require('../Components/logger');
const mysql = require('../Components/mysql');
const queryGenerator = require('./queryGenerator');
const cacheKeyGenerator = require('./cacheKeyGenerator');
Expand All @@ -16,6 +17,7 @@ const search = async (searchText, filters, options) => {
let query = null;
let result = {};
let searchableText = null;
let searchResults;

// Check searchText type
if (searchText && typeof searchText !== 'string') {
Expand Down Expand Up @@ -60,7 +62,15 @@ const search = async (searchText, filters, options) => {
result.aggs = 'all';
}

let searchResults = await elasticsearch.searchWithAggregations(config.indexDS, query);
try {
searchResults = await elasticsearch.searchWithAggregations(config.indexDS, query);
} catch (error) {
logger.error(`Error searching datasets: ${error}`);
return {
error: error?.body?.error?.root_cause ? JSON.stringify(error.body.error.root_cause).replace(/\\n/g, '') : error.message,
};
}

let datasets = searchResults.hits.hits.map((ds) => {
if (ds.inner_hits) {
const terms = Object.keys(ds.inner_hits);
Expand Down
32 changes: 32 additions & 0 deletions Services/dataset.service.test.fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,35 @@ export const normalOpensearchResults = {
},
aggs: undefined,
};

// Example of Opensearch response with error
export const errorOpensearchResults = {
"error": {
"root_cause": [
{
"type": "query_shard_exception",
"reason": "No mapping found for [dbGaP_phs] in order to sort on",
"index": "datasets",
"index_uuid": "JHoru6szQ8G_-NtFJd--Bg"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "datasets",
"node": "YzqehJMeSham0G7I2LQdmw",
"reason": {
"type": "query_shard_exception",
"reason": "No mapping found for [dbGaP_phs] in order to sort on",
"index": "datasets",
"index_uuid": "JHoru6szQ8G_-NtFJd--Bg"
}
}
]
},
"status": 400
};
11 changes: 11 additions & 0 deletions Services/dataset.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
normalOptions,
normalSearchText,
normalOpensearchResults,
errorOpensearchResults,
} from './dataset.service.test.fixtures.js';

beforeEach(() => {
Expand Down Expand Up @@ -83,4 +84,14 @@ describe('search', () => {
const resultEmpty = await datasetService.search(normalSearchText, normalFilters, {});
expect(resultNull).toEqual(resultEmpty);
});

it('should handle Opensearch error response', async () => {
const error = new Error("Test Opensearch failure");
let result;
error.body = errorOpensearchResults;
vi.spyOn(elasticsearch, "searchWithAggregations").mockRejectedValue(error);
result = await datasetService.search();
expect(result).toHaveProperty('error');
expect(result.error).toBeDefined();
});
});
2 changes: 2 additions & 0 deletions Utils/datasetFields.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const DATASET_DEFAULT_SORT_FIELD = 'dataset_title_sort';
// Maps Dataset natural field names to property names
const DATASET_SEARCH_FIELDS = [
// 'dataset_uuid',
Expand Down Expand Up @@ -93,6 +94,7 @@ const datasetFields = {
};

module.exports = {
DATASET_DEFAULT_SORT_FIELD: 'dataset_title_sort',
DATASET_SEARCH_FIELDS,
DATASET_HIGHLIGHT_FIELDS,
DATASET_RETURN_FIELDS,
Expand Down