Skip to content

Commit

Permalink
feat: Introduce Meilisearch (#10755)
Browse files Browse the repository at this point in the history
* wip

* wip

* Update SearchService.ts

* Update SearchService.ts

* wip

* wip

* Update SearchService.ts

* Update CHANGELOG.md

* wip

* Update SearchService.ts

* Update docker-compose.yml.example
  • Loading branch information
syuilo committed May 4, 2023
1 parent 5f62cef commit 5c08f2b
Show file tree
Hide file tree
Showing 18 changed files with 259 additions and 93 deletions.
16 changes: 7 additions & 9 deletions .config/docker_example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,13 @@ redis:
# #prefix: example-prefix
# #db: 1

# ┌─────────────────────────────┐
#───┘ Elasticsearch configuration └─────────────────────────────

#elasticsearch:
# host: localhost
# port: 9200
# ssl: false
# user:
# pass:
# ┌───────────────────────────┐
#───┘ MeiliSearch configuration └─────────────────────────────

#meilisearch:
# host: meilisearch
# port: 7700
# apiKey: ''

# ┌───────────────┐
#───┘ ID generation └───────────────────────────────────────────
Expand Down
12 changes: 5 additions & 7 deletions .config/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,13 @@ redis:
# #prefix: example-prefix
# #db: 1

# ┌─────────────────────────────
#───┘ Elasticsearch configuration └─────────────────────────────
# ┌───────────────────────────┐
#───┘ MeiliSearch configuration └─────────────────────────────

#elasticsearch:
#meilisearch:
# host: localhost
# port: 9200
# ssl: false
# user:
# pass:
# port: 7700
# apiKey: ''

# ┌───────────────┐
#───┘ ID generation └───────────────────────────────────────────
Expand Down
16 changes: 7 additions & 9 deletions .devcontainer/devcontainer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,13 @@ redis:
# #prefix: example-prefix
# #db: 1

# ┌─────────────────────────────┐
#───┘ Elasticsearch configuration └─────────────────────────────

#elasticsearch:
# host: localhost
# port: 9200
# ssl: false
# user:
# pass:
# ┌───────────────────────────┐
#───┘ MeiliSearch configuration └─────────────────────────────

#meilisearch:
# host: meilisearch
# port: 7700
# apiKey: ''

# ┌───────────────┐
#───┘ ID generation └───────────────────────────────────────────
Expand Down
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ build/
built/
db/
docker-compose.yml
elasticsearch/
node_modules/
packages/*/node_modules
redis/
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ built
/data
/.cache-loader
/db
/elasticsearch
/meili_data
npm-debug.log
*.pem
run.bat
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- Node.js 18.6.0以上が必要になりました

### General
- Meilisearchを全文検索に使用できるようになりました
- 新規登録前に簡潔なルールをユーザーに表示できる、サーバールール機能を追加
- ユーザーへの自分用メモ機能
* ユーザーに対して、自分だけが見られるメモを追加できるようになりました。
Expand Down
12 changes: 5 additions & 7 deletions chart/files/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,13 @@ redis:
# #prefix: example-prefix
# #db: 1

# ┌─────────────────────────────
#───┘ Elasticsearch configuration └─────────────────────────────
# ┌───────────────────────────┐
#───┘ MeiliSearch configuration └─────────────────────────────

#elasticsearch:
#meilisearch:
# host: localhost
# port: 9200
# ssl: false
# user:
# pass:
# port: 7700
# apiKey: ''

# ┌───────────────┐
#───┘ ID generation └───────────────────────────────────────────
Expand Down
14 changes: 8 additions & 6 deletions docker-compose.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
links:
- db
- redis
# - es
# - meilisearch
depends_on:
db:
condition: service_healthy
Expand Down Expand Up @@ -48,16 +48,18 @@ services:
interval: 5s
retries: 20

# es:
# meilisearch:
# restart: always
# image: docker.elastic.co/elasticsearch/elasticsearch-oss:6.4.2
# image: getmeili/meilisearch:v1.1.1
# environment:
# - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
# - "TAKE_FILE_OWNERSHIP=111"
# - MEILI_NO_ANALYTICS=true
# - MEILI_ENV=production
# env_file:
# - .config/meilisearch.env
# networks:
# - internal_network
# volumes:
# - ./elasticsearch:/usr/share/elasticsearch/data
# - ./meili_data:/meili_data

networks:
internal_network:
Expand Down
1 change: 1 addition & 0 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"jsdom": "21.1.1",
"json5": "2.2.3",
"jsonld": "8.1.1",
"meilisearch": "0.32.3",
"jsrsasign": "10.8.6",
"mfm-js": "0.23.3",
"mime-types": "2.1.35",
Expand Down
20 changes: 18 additions & 2 deletions packages/backend/src/GlobalModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { setTimeout } from 'node:timers/promises';
import { Global, Inject, Module } from '@nestjs/common';
import * as Redis from 'ioredis';
import { DataSource } from 'typeorm';
import { MeiliSearch } from 'meilisearch';
import { DI } from './di-symbols.js';
import { loadConfig } from './config.js';
import { createPostgresDataSource } from './postgres.js';
Expand All @@ -22,6 +23,21 @@ const $db: Provider = {
inject: [DI.config],
};

const $meilisearch: Provider = {
provide: DI.meilisearch,
useFactory: (config) => {
if (config.meilisearch) {
return new MeiliSearch({
host: `http://${config.meilisearch.host}:${config.meilisearch.port}`,
apiKey: config.meilisearch.apiKey,
});
} else {
return null;
}
},
inject: [DI.config],
};

const $redis: Provider = {
provide: DI.redis,
useFactory: (config) => {
Expand Down Expand Up @@ -73,8 +89,8 @@ const $redisForSub: Provider = {
@Global()
@Module({
imports: [RepositoryModule],
providers: [$config, $db, $redis, $redisForPub, $redisForSub],
exports: [$config, $db, $redis, $redisForPub, $redisForSub, RepositoryModule],
providers: [$config, $db, $meilisearch, $redis, $redisForPub, $redisForSub],
exports: [$config, $db, $meilisearch, $redis, $redisForPub, $redisForSub, RepositoryModule],
})
export class GlobalModule implements OnApplicationShutdown {
constructor(
Expand Down
10 changes: 4 additions & 6 deletions packages/backend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,10 @@ export type Source = {
db?: number;
prefix?: string;
};
elasticsearch: {
meilisearch?: {
host: string;
port: number;
ssl?: boolean;
user?: string;
pass?: string;
index?: string;
port: string;
apiKey: string;
};

proxy?: string;
Expand Down Expand Up @@ -139,6 +136,7 @@ const path = process.env.MISSKEY_CONFIG_YML
: process.env.NODE_ENV === 'test'
? resolve(dir, 'test.yml')
: resolve(dir, 'default.yml');

export function loadConfig() {
const meta = JSON.parse(fs.readFileSync(`${_dirname}/../../../built/meta.json`, 'utf-8'));
const clientManifestExists = fs.existsSync(_dirname + '/../../../built/_vite_/manifest.json');
Expand Down
7 changes: 7 additions & 0 deletions packages/backend/src/core/CoreModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import { WebhookService } from './WebhookService.js';
import { ProxyAccountService } from './ProxyAccountService.js';
import { UtilityService } from './UtilityService.js';
import { FileInfoService } from './FileInfoService.js';
import { SearchService } from './SearchService.js';
import { ChartLoggerService } from './chart/ChartLoggerService.js';
import FederationChart from './chart/charts/federation.js';
import NotesChart from './chart/charts/notes.js';
Expand Down Expand Up @@ -171,6 +172,8 @@ const $VideoProcessingService: Provider = { provide: 'VideoProcessingService', u
const $WebhookService: Provider = { provide: 'WebhookService', useExisting: WebhookService };
const $UtilityService: Provider = { provide: 'UtilityService', useExisting: UtilityService };
const $FileInfoService: Provider = { provide: 'FileInfoService', useExisting: FileInfoService };
const $SearchService: Provider = { provide: 'SearchService', useExisting: SearchService };

const $ChartLoggerService: Provider = { provide: 'ChartLoggerService', useExisting: ChartLoggerService };
const $FederationChart: Provider = { provide: 'FederationChart', useExisting: FederationChart };
const $NotesChart: Provider = { provide: 'NotesChart', useExisting: NotesChart };
Expand Down Expand Up @@ -295,6 +298,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
WebhookService,
UtilityService,
FileInfoService,
SearchService,
ChartLoggerService,
FederationChart,
NotesChart,
Expand Down Expand Up @@ -413,6 +417,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
$WebhookService,
$UtilityService,
$FileInfoService,
$SearchService,
$ChartLoggerService,
$FederationChart,
$NotesChart,
Expand Down Expand Up @@ -532,6 +537,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
WebhookService,
UtilityService,
FileInfoService,
SearchService,
FederationChart,
NotesChart,
UsersChart,
Expand Down Expand Up @@ -649,6 +655,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
$WebhookService,
$UtilityService,
$FileInfoService,
$SearchService,
$FederationChart,
$NotesChart,
$UsersChart,
Expand Down
16 changes: 5 additions & 11 deletions packages/backend/src/core/NoteCreateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { bindThis } from '@/decorators.js';
import { DB_MAX_NOTE_TEXT_LENGTH } from '@/const.js';
import { RoleService } from '@/core/RoleService.js';
import { MetaService } from '@/core/MetaService.js';
import { SearchService } from '@/core/SearchService.js';

const mutedWordsCache = new MemorySingleCache<{ userId: UserProfile['userId']; mutedWords: UserProfile['mutedWords']; }[]>(1000 * 60 * 5);

Expand Down Expand Up @@ -198,6 +199,7 @@ export class NoteCreateService implements OnApplicationShutdown {
private apRendererService: ApRendererService,
private roleService: RoleService,
private metaService: MetaService,
private searchService: SearchService,
private notesChart: NotesChart,
private perUserNotesChart: PerUserNotesChart,
private activeUsersChart: ActiveUsersChart,
Expand Down Expand Up @@ -728,17 +730,9 @@ export class NoteCreateService implements OnApplicationShutdown {

@bindThis
private index(note: Note) {
if (note.text == null || this.config.elasticsearch == null) return;
/*
es!.index({
index: this.config.elasticsearch.index ?? 'misskey_note',
id: note.id.toString(),
body: {
text: normalizeForSearch(note.text),
userId: note.userId,
userHost: note.userHost,
},
});*/
if (note.text == null && note.cw == null) return;

this.searchService.indexNote(note);
}

@bindThis
Expand Down

0 comments on commit 5c08f2b

Please sign in to comment.