Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

feat: accept Elasticsearch client as optional constructor argument #73

Merged
merged 2 commits into from
May 20, 2021
Merged
Changes from all 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
10 changes: 8 additions & 2 deletions src/elasticSearchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
SearchFilter,
FhirVersion,
} from 'fhir-works-on-aws-interface';
import { Client } from '@elastic/elasticsearch';
import { ElasticSearch } from './elasticSearch';
import {
DEFAULT_SEARCH_RESULTS_PER_PAGE,
Expand All @@ -36,6 +37,8 @@ const MAX_INCLUDE_ITERATIVE_DEPTH = 5;

// eslint-disable-next-line import/prefer-default-export
export class ElasticSearchService implements Search {
private readonly esClient: Client;

private readonly searchFiltersForAllQueries: SearchFilter[];

private readonly cleanUpFunction: (resource: any) => any;
Expand All @@ -53,6 +56,7 @@ export class ElasticSearchService implements Search {
* @param fhirVersion
* @param compiledImplementationGuides - The output of ImplementationGuides.compile.
* This parameter enables support for search parameters defined in Implementation Guides.
* @param esClient
*/
constructor(
searchFiltersForAllQueries: SearchFilter[] = [],
Expand All @@ -61,11 +65,13 @@ export class ElasticSearchService implements Search {
},
fhirVersion: FhirVersion = '4.0.1',
compiledImplementationGuides?: any,
esClient: Client = ElasticSearch,
) {
this.searchFiltersForAllQueries = searchFiltersForAllQueries;
this.cleanUpFunction = cleanUpFunction;
this.fhirVersion = fhirVersion;
this.fhirSearchParametersRegistry = new FHIRSearchParametersRegistry(fhirVersion, compiledImplementationGuides);
this.esClient = esClient;
}

async getCapabilities() {
Expand Down Expand Up @@ -155,7 +161,7 @@ export class ElasticSearchService implements Search {
// eslint-disable-next-line class-methods-use-this
private async executeQuery(searchQuery: any): Promise<{ hits: any[]; total: number }> {
try {
const apiResponse = await ElasticSearch.search(searchQuery);
const apiResponse = await this.esClient.search(searchQuery);
return {
total: apiResponse.body.hits.total.value,
hits: apiResponse.body.hits.hits,
Expand All @@ -180,7 +186,7 @@ export class ElasticSearchService implements Search {
hits: [],
};
}
const apiResponse = await ElasticSearch.msearch({
const apiResponse = await this.esClient.msearch({
body: searchQueries.flatMap(query => [{ index: query.index }, { query: query.body.query }]),
});

Expand Down