Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Elasticsearchのサポート / ElasticsearchとMeilisearchを共存可能に #104

Draft
wants to merge 9 commits into
base: hanami
Choose a base branch
from
3 changes: 3 additions & 0 deletions .devcontainer/Dockerfile.elastic
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM docker.elastic.co/elasticsearch/elasticsearch:8.15.2

RUN bin/elasticsearch-plugin install analysis-kuromoji
33 changes: 32 additions & 1 deletion .devcontainer/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ services:

db:
restart: unless-stopped
image: postgres:15-alpine
image: postgres:17-alpine
networks:
- internal_network
environment:
Expand All @@ -45,6 +45,37 @@ services:
interval: 5s
retries: 20

elasticsearch:
restart: unless-stopped
build:
context: .
dockerfile: Dockerfile.elastic
networks:
- internal_network
environment:
discovery.type: single-node
xpack.security.enabled: false
ulimits:
memlock:
soft: -1
hard: -1
healthcheck:
test: "curl -s http://localhost:9200/_cluster/health | grep -q '\"status\":\"green\"'"
interval: 30s
retries: 20

kibana:
restart: unless-stopped
image: docker.elastic.co/kibana/kibana:8.15.2
networks:
- internal_network
environment:
ELASTICSEARCH_HOSTS: http://elasticsearch:9200
healthcheck:
test: "curl -s http://localhost:5601/api/status | grep -q '\"status\":\"green\"'"
interval: 30s
retries: 20

volumes:
postgres-data:
redis-data:
Expand Down
5 changes: 5 additions & 0 deletions .devcontainer/devcontainer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ url: http://127.0.0.1:3000/
# ONCE YOU HAVE STARTED THE INSTANCE, DO NOT CHANGE THE
# URL SETTINGS AFTER THAT!

elasticsearch:
host: elasticsearch
port: 9200
rejectUnauthorized: false

# ┌───────────────────────┐
#───┘ Port and TLS settings └───────────────────────────────────

Expand Down
3 changes: 2 additions & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"@bull-board/fastify": "6.0.0",
"@bull-board/ui": "6.0.0",
"@discordapp/twemoji": "15.1.0",
"@elastic/elasticsearch": "^8.15.0",
"@fastify/accepts": "5.0.1",
"@fastify/cookie": "10.0.1",
"@fastify/cors": "10.0.1",
Expand Down Expand Up @@ -132,8 +133,8 @@
"json5": "2.2.3",
"jsonld": "8.3.2",
"jsrsasign": "11.1.0",
"meilisearch": "0.42.0",
"juice": "11.0.0",
"meilisearch": "0.42.0",
"mfm-js": "0.24.0",
"microformats-parser": "2.0.2",
"mime-types": "2.1.35",
Expand Down
29 changes: 27 additions & 2 deletions packages/backend/src/GlobalModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Global, Inject, Module } from '@nestjs/common';
import * as Redis from 'ioredis';
import { DataSource } from 'typeorm';
import { MeiliSearch } from 'meilisearch';
import { Client as ElasticSearch } from '@elastic/elasticsearch';
import { DI } from './di-symbols.js';
import { Config, loadConfig } from './config.js';
import { createPostgresDataSource } from './postgres.js';
Expand Down Expand Up @@ -45,6 +46,30 @@ const $meilisearch: Provider = {
inject: [DI.config],
};

const $elasticsearch: Provider = {
provide: DI.elasticsearch,
useFactory: (config: Config) => {
if (config.elasticsearch) {
return new ElasticSearch({
nodes: {
url: new URL(`${config.elasticsearch.ssl ? 'https' : 'http'}://${config.elasticsearch.host}:${config.elasticsearch.port}`),
ssl: {
rejectUnauthorized: config.elasticsearch.rejectUnauthorized,
},
},
auth: (config.elasticsearch.user && config.elasticsearch.pass) ? {
username: config.elasticsearch.user,
password: config.elasticsearch.pass,
} : undefined,
pingTimeout: 30000,
});
} else {
return null;
}
},
inject: [DI.config],
};

const $redis: Provider = {
provide: DI.redis,
useFactory: (config: Config) => {
Expand Down Expand Up @@ -148,8 +173,8 @@ const $meta: Provider = {
@Global()
@Module({
imports: [RepositoryModule],
providers: [$config, $db, $meta, $meilisearch, $redis, $redisForPub, $redisForSub, $redisForTimelines, $redisForReactions],
exports: [$config, $db, $meta, $meilisearch, $redis, $redisForPub, $redisForSub, $redisForTimelines, $redisForReactions, RepositoryModule],
providers: [$config, $db, $meta, $meilisearch, $elasticsearch, $redis, $redisForPub, $redisForSub, $redisForTimelines, $redisForReactions],
exports: [$config, $db, $meta, $meilisearch, $elasticsearch, $redis, $redisForPub, $redisForSub, $redisForTimelines, $redisForReactions, RepositoryModule],
})
export class GlobalModule implements OnApplicationShutdown {
constructor(
Expand Down
20 changes: 20 additions & 0 deletions packages/backend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ type Source = {

setupPassword?: string;

elasticsearch?: {
host: string;
port: string;
user: string;
pass: string;
ssl?: boolean;
rejectUnauthorized?: boolean;
index: string;
};

proxy?: string;
proxySmtp?: string;
proxyBypassHosts?: string[];
Expand Down Expand Up @@ -141,6 +151,15 @@ export type Config = {
index: string;
scope?: 'local' | 'global' | string[];
} | undefined;
elasticsearch: {
host: string;
port: string;
user: string;
pass: string;
ssl?: boolean;
rejectUnauthorized?: boolean;
index: string;
} | undefined;
proxy: string | undefined;
proxySmtp: string | undefined;
proxyBypassHosts: string[] | undefined;
Expand Down Expand Up @@ -271,6 +290,7 @@ export function loadConfig(): Config {
dbReplications: config.dbReplications,
dbSlaves: config.dbSlaves,
meilisearch: config.meilisearch,
elasticsearch: config.elasticsearch,
redis,
redisForPubsub: config.redisForPubsub ? convertRedisOptions(config.redisForPubsub, host) : redis,
redisForJobQueue: config.redisForJobQueue ? convertRedisOptions(config.redisForJobQueue, host) : redis,
Expand Down
Loading
Loading