iceshrimp-161sh/packages/backend/src/db/elasticsearch.ts

66 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-01-13 05:40:33 +01:00
import * as elasticsearch from "@elastic/elasticsearch";
import config from "@/config/index.js";
2016-12-28 23:49:51 +01:00
2018-07-04 13:36:06 +02:00
const index = {
settings: {
analysis: {
analyzer: {
2019-04-25 00:46:39 +02:00
ngram: {
2023-01-13 05:40:33 +01:00
tokenizer: "ngram",
2021-12-09 15:58:30 +01:00
},
},
},
2018-07-04 13:36:06 +02:00
},
mappings: {
2019-04-25 00:46:39 +02:00
properties: {
text: {
2023-01-13 05:40:33 +01:00
type: "text",
2019-04-25 00:46:39 +02:00
index: true,
2023-01-13 05:40:33 +01:00
analyzer: "ngram",
2019-04-25 00:46:39 +02:00
},
userId: {
2023-01-13 05:40:33 +01:00
type: "keyword",
2019-04-25 00:46:39 +02:00
index: true,
},
userHost: {
2023-01-13 05:40:33 +01:00
type: "keyword",
2019-04-25 00:46:39 +02:00
index: true,
2021-12-09 15:58:30 +01:00
},
},
},
2018-07-04 13:36:06 +02:00
};
2016-12-28 23:49:51 +01:00
// Init ElasticSearch connection
2023-01-13 05:40:33 +01:00
const client = config.elasticsearch
? new elasticsearch.Client({
node: `${config.elasticsearch.ssl ? "https://" : "http://"}${
config.elasticsearch.host
}:${config.elasticsearch.port}`,
auth:
config.elasticsearch.user && config.elasticsearch.pass
? {
username: config.elasticsearch.user,
password: config.elasticsearch.pass,
}
: undefined,
pingTimeout: 30000,
})
: null;
2016-12-28 23:49:51 +01:00
2018-07-04 13:13:05 +02:00
if (client) {
2023-01-13 05:40:33 +01:00
client.indices
.exists({
index: config.elasticsearch.index || "misskey_note",
})
.then((exist) => {
if (!exist.body) {
client.indices.create({
index: config.elasticsearch.index || "misskey_note",
body: index,
});
}
});
2018-07-04 13:13:05 +02:00
}
2016-12-28 23:49:51 +01:00
export default client;