calckey/packages/backend/src/db/elasticsearch.ts

57 lines
1.1 KiB
TypeScript
Raw Normal View History

2019-04-24 22:46:39 +00:00
import * as elasticsearch from '@elastic/elasticsearch';
import config from '@/config/index.js';
2016-12-28 22:49:51 +00:00
2018-07-04 11:36:06 +00:00
const index = {
settings: {
analysis: {
analyzer: {
2019-04-24 22:46:39 +00:00
ngram: {
2021-12-09 14:58:30 +00:00
tokenizer: 'ngram',
},
},
},
2018-07-04 11:36:06 +00:00
},
mappings: {
2019-04-24 22:46:39 +00:00
properties: {
text: {
type: 'text',
index: true,
analyzer: 'ngram',
},
userId: {
type: 'keyword',
index: true,
},
userHost: {
type: 'keyword',
index: true,
2021-12-09 14:58:30 +00:00
},
},
},
2018-07-04 11:36:06 +00:00
};
2016-12-28 22:49:51 +00:00
// Init ElasticSearch connection
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,
2021-12-09 14:58:30 +00:00
password: config.elasticsearch.pass,
} : undefined,
2021-12-09 14:58:30 +00:00
pingTimeout: 30000,
}) : null;
2016-12-28 22:49:51 +00:00
2018-07-04 11:13:05 +00:00
if (client) {
2018-07-04 11:36:06 +00:00
client.indices.exists({
index: config.elasticsearch.index || 'misskey_note',
2018-07-04 11:36:06 +00:00
}).then(exist => {
2019-04-24 22:46:39 +00:00
if (!exist.body) {
client.indices.create({
index: config.elasticsearch.index || 'misskey_note',
2021-12-09 14:58:30 +00:00
body: index,
2019-04-24 22:46:39 +00:00
});
}
2018-07-04 11:13:05 +00:00
});
}
2016-12-28 22:49:51 +00:00
export default client;