Sharkey/src/db/elasticsearch.ts

53 lines
916 B
TypeScript
Raw Normal View History

2019-04-25 01:46:39 +03:00
import * as elasticsearch from '@elastic/elasticsearch';
2018-04-02 07:15:53 +03:00
import config from '../config';
2016-12-29 00:49:51 +02:00
2018-07-04 14:36:06 +03:00
const index = {
settings: {
analysis: {
analyzer: {
2019-04-25 01:46:39 +03:00
ngram: {
tokenizer: 'ngram'
2018-07-04 14:36:06 +03:00
}
}
}
},
mappings: {
2019-04-25 01:46:39 +03:00
properties: {
text: {
type: 'text',
index: true,
analyzer: 'ngram',
},
userId: {
type: 'keyword',
index: true,
},
userHost: {
type: 'keyword',
index: true,
2018-07-04 14:36:06 +03:00
}
}
}
};
2016-12-29 00:49:51 +02:00
// Init ElasticSearch connection
const client = config.elasticsearch ? new elasticsearch.Client({
2019-04-25 01:46:39 +03:00
node: `http://${config.elasticsearch.host}:${config.elasticsearch.port}`,
pingTimeout: 30000
}) : null;
2016-12-29 00:49:51 +02:00
2018-07-04 14:13:05 +03:00
if (client) {
2018-07-04 14:36:06 +03:00
client.indices.exists({
index: config.elasticsearch.index || 'misskey_note',
2018-07-04 14:36:06 +03:00
}).then(exist => {
2019-04-25 01:46:39 +03:00
if (!exist.body) {
client.indices.create({
index: config.elasticsearch.index || 'misskey_note',
2019-04-25 01:46:39 +03:00
body: index
});
}
2018-07-04 14:13:05 +03:00
});
}
2016-12-29 00:49:51 +02:00
export default client;