-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsearch.ts
52 lines (47 loc) · 1.24 KB
/
search.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import type { NextApiRequest, NextApiResponse } from "next";
import { client } from "@/config/elasticsearch";
import { buildQuery } from "@/utils/server/apiFunctions";
// import ElasticsearchAPIConnector from "@elastic/search-ui-elasticsearch-connector";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method !== "POST") {
return res.status(405).json({
error:
"Invalid request method. The endpoint only supports POST requests.",
});
}
let queryString = req.body.queryString as string;
let size = req.body.size;
let page = req.body.page;
let filterFields = req.body.filterFields;
let sortFields = req.body.sortFields;
const from = page * size;
let searchQuery = buildQuery({
queryString,
filterFields,
sortFields,
from,
size,
});
try {
// Call the search method
const result = await client.search({
index: process.env.INDEX,
...searchQuery,
});
return res.status(200).json({
success: true,
data: {
result,
},
});
} catch (error) {
console.error(error);
return res.status(400).json({
success: false,
message: error.errmsg || error.errors || error.message,
});
}
}