summaryrefslogtreecommitdiff
path: root/src/db/elasticsearch.ts
blob: 048e399bdfd57b7dbb2b6dda546e18db4af95cdf (plain)
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
import * as elasticsearch from '@elastic/elasticsearch';
import config from '../config';

const index = {
	settings: {
		analysis: {
			analyzer: {
				ngram: {
					tokenizer: 'ngram'
				}
			}
		}
	},
	mappings: {
		properties: {
			text: {
				type: 'text',
				index: true,
				analyzer: 'ngram',
			},
			userId: {
				type: 'keyword',
				index: true,
			},
			userHost: {
				type: 'keyword',
				index: true,
			}
		}
	}
};

// 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,
		password: config.elasticsearch.pass
	} : undefined,
	pingTimeout: 30000
}) : null;

if (client) {
	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
			});
		}
	});
}

export default client;