feat: search file
This commit is contained in:
parent
3247af3cab
commit
f5277a36f8
2 changed files with 45 additions and 0 deletions
35
Prototype/server/src/controllers/searchController.ts
Normal file
35
Prototype/server/src/controllers/searchController.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
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<EhrFile[]> {
|
||||
const result = await esClient.search<EhrFile & { attachment: Record<string, string> }>({
|
||||
index: "ehr-api-client",
|
||||
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] : []))
|
||||
: [];
|
||||
}
|
||||
}
|
||||
10
Prototype/server/src/interfaces/search.ts
Normal file
10
Prototype/server/src/interfaces/search.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
export interface Search {
|
||||
AND?: {
|
||||
field: string;
|
||||
value: string;
|
||||
}[];
|
||||
OR?: {
|
||||
field: string;
|
||||
value: string;
|
||||
}[];
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue