summaryrefslogtreecommitdiff
path: root/src/db/elasticsearch.ts
blob: ee5769d1d412328a28eb314f342ba90c43633860 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import * as elasticsearch from 'elasticsearch';
import config from '../config';

const index = {
	settings: {
		analysis: {
			normalizer: {
				lowercase_normalizer: {
					type: 'custom',
					filter: ['lowercase']
				}
			},
			analyzer: {
				bigram: {
					tokenizer: 'bigram_tokenizer'
				}
			},
			tokenizer: {
				bigram_tokenizer: {
					type: 'nGram',
					min_gram: 2,
					max_gram: 2
				}
			}
		}
	},
	mappings: {
		note: {
			properties: {
				text: {
					type: 'text',
					index: true,
					analyzer: 'bigram',
					normalizer: 'lowercase_normalizer'
				}
			}
		}
	}
};

// Init ElasticSearch connection
const client = config.elasticsearch ? new elasticsearch.Client({
	host: `${config.elasticsearch.host}:${config.elasticsearch.port}`
}) : null;

if (client) {
	// Send a HEAD request
	client.ping({
		// Ping usually has a 3000ms timeout
		requestTimeout: 30000
	}, error => {
		if (error) {
			console.error('elasticsearch is down!');
		} else {
			console.log('elasticsearch is available!');
		}
	});

	client.indices.exists({
		index: 'misskey'
	}).then(exist => {
		if (exist) return;

		client.indices.create({
			index: 'misskey',
			body: index
		});
	});
}

export default client;