-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
62 lines (55 loc) · 1.6 KB
/
server.js
File metadata and controls
62 lines (55 loc) · 1.6 KB
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
53
54
55
56
57
58
59
60
61
62
// server.js
const express = require('express');
const { Client } = require('@opensearch-project/opensearch');
const path = require('path');
require('dotenv').config();
const app = express();
app.use(express.json());
app.use(express.static('public'));
const client = new Client({
node: 'https://localhost:9200',
auth: {
username: process.env.OPENSEARCH_USERNAME,
password: process.env.OPENSEARCH_PASSWORD
},
ssl: {
verify_certs: false,
rejectUnauthorized: false
}
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.post('/api/search', async (req, res) => {
try {
const { searchText } = req.body;
const response = await client.search({
index: 'dadjokes',
body: {
size: 100,
query: {
neural: {
"joke-embedding": {
model_id: process.env.MODEL_ID,
query_text: searchText,
k: 100
}
}
}
}
});
const hits = response.body.hits.hits.map(hit => ({
joke: hit._source.joke,
score: hit._score,
jid: hit._source.jid
}));
res.json(hits);
} catch (error) {
console.error('Search error:', error);
res.status(500).json({ error: 'Search failed' });
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});