import { Body, Controller, Post, Route, SuccessResponse, Tags } from "tsoa"; import HttpStatusCode from "../interfaces/http-status"; import esClient from "../elasticsearch"; import { Search } from "../interfaces/search"; import { EhrFile } from "../interfaces/ehr-fs"; @Route("/search") export class SearchController extends Controller { @Post("/") @Tags("Search") @SuccessResponse(HttpStatusCode.OK) public async searchFile(@Body() search: Search): Promise { const result = await esClient.search }>({ index: process.env.ELASTICSEARCH_INDEX ?? 'ehr-index', query: { bool: { must: search.AND?.map((v) => ({ match: { [v.field]: v.value } })), should: search.OR?.map((v) => ({ match: { [v.field]: v.value } })), }, }, }); return result.hits.hits.length > 0 ? result.hits.hits .map((v) => { if (!v._source) return; const { attachment, ...rest } = v._source; return rest; }) .flatMap((v) => (!!v ? [v] : [])) : []; } }