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: Re-support for Elasticsearch #13166

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
34380dc
feat: support elasticsearch
MomentQYC Feb 4, 2024
1b92ec0
feat: Supports both tokenization by Elasticsearch analyzers and handl…
MomentQYC Feb 5, 2024
194cf4d
Merge branch 'misskey-dev:develop' into develop
MomentQYC Feb 5, 2024
4511ee0
style
MomentQYC Feb 5, 2024
c18f235
Merge branch 'develop' of github.com:MomentQYC/misskey into develop
MomentQYC Feb 5, 2024
ddcb68c
Merge branch 'misskey-dev:develop' into develop
MomentQYC Feb 5, 2024
eb31645
Update CHANGELOG.md
MomentQYC Feb 5, 2024
48824f2
fix: regenerate misskey-js type definitions
MomentQYC Feb 5, 2024
c2d03fd
feat: Support for multiple indexes
MomentQYC Feb 5, 2024
9fb8421
style
MomentQYC Feb 5, 2024
0368c49
fix: regenerate misskey-js type definitions
MomentQYC Feb 5, 2024
646e993
Merge branch 'develop' into develop
MomentQYC Feb 5, 2024
177871c
fix: regenerate misskey-js type definitions
MomentQYC Feb 5, 2024
32b2455
fix: regenerate misskey-js type definitions
MomentQYC Feb 5, 2024
b31838d
Merge branch 'misskey-dev:develop' into develop
MomentQYC Feb 6, 2024
61f65f5
fix: regenerate misskey-js type definitions
MomentQYC Feb 6, 2024
29768e6
Merge branch 'develop' into develop
MomentQYC Feb 6, 2024
48d0e61
Merge branch 'misskey-dev:develop' into develop
MomentQYC Feb 15, 2024
a509dd1
Merge branch 'develop' into develop
MomentQYC Feb 17, 2024
9617d79
Merge branch 'misskey-dev:develop' into develop
MomentQYC Feb 27, 2024
a5ece37
Merge branch 'misskey-dev:develop' into develop
MomentQYC Mar 3, 2024
6d226e3
Merge branch 'develop' into develop
MomentQYC Apr 2, 2024
d45f35f
Merge branch 'develop' into develop
MomentQYC May 31, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .config/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,20 @@ redis:
# index: ''
# scope: local

# ┌───────────────────────────┐
#───┘ ElasticSearch configuration └─────────────────────────────

# If MeiliSearch is also enabled, the MeiliSearch feature is used
# and ElasticSearch is disabled.

#elasticsearch:
# host: localhost
# port: 9200
# user: ''
# pass: ''
# ssl: true
# index: ''

# ┌───────────────┐
#───┘ ID generation └───────────────────────────────────────────

Expand Down
1 change: 1 addition & 0 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"@bull-board/fastify": "5.17.0",
"@bull-board/ui": "5.17.0",
"@discordapp/twemoji": "15.0.3",
"@elastic/elasticsearch": "8.13.1",
"@fastify/accepts": "4.3.0",
"@fastify/cookie": "9.3.1",
"@fastify/cors": "9.0.1",
Expand Down
24 changes: 22 additions & 2 deletions packages/backend/src/GlobalModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { Global, Inject, Module } from '@nestjs/common';
import * as Redis from 'ioredis';
import { DataSource } from 'typeorm';
import { Client as ElasticSearch } from '@elastic/elasticsearch';
import { MeiliSearch } from 'meilisearch';
import { DI } from './di-symbols.js';
import { Config, loadConfig } from './config.js';
Expand Down Expand Up @@ -43,6 +44,25 @@ const $meilisearch: Provider = {
inject: [DI.config],
};

const $elasticsearch: Provider = {
provide: DI.elasticsearch,
useFactory: (config: Config) => {
if (config.elasticsearch) {
return new ElasticSearch({
nodes: `${config.elasticsearch.ssl ? 'https' : 'http'}://${config.elasticsearch.host}:${config.elasticsearch.port}`,
auth: {
username: config.elasticsearch.user,
password: config.elasticsearch.pass,
},
//headers: {'Content-Type': 'application/json'},
});
} else {
return null;
}
},
inject: [DI.config],
};

const $redis: Provider = {
provide: DI.redis,
useFactory: (config: Config) => {
Expand Down Expand Up @@ -81,8 +101,8 @@ const $redisForTimelines: Provider = {
@Global()
@Module({
imports: [RepositoryModule],
providers: [$config, $db, $meilisearch, $redis, $redisForPub, $redisForSub, $redisForTimelines],
exports: [$config, $db, $meilisearch, $redis, $redisForPub, $redisForSub, $redisForTimelines, RepositoryModule],
providers: [$config, $db, $meilisearch, $elasticsearch, $redis, $redisForPub, $redisForSub, $redisForTimelines],
exports: [$config, $db, $meilisearch, $elasticsearch, $redis, $redisForPub, $redisForSub, $redisForTimelines, RepositoryModule],
})
export class GlobalModule implements OnApplicationShutdown {
constructor(
Expand Down
17 changes: 17 additions & 0 deletions packages/backend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ type Source = {
index: string;
scope?: 'local' | 'global' | string[];
};
elasticsearch?: {
host: string;
port: string;
user: string;
pass: string;
ssl?: boolean;
index: string;
};
sentryForBackend?: { options: Partial<Sentry.NodeOptions>; enableNodeProfiling: boolean; };
sentryForFrontend?: { options: Partial<Sentry.NodeOptions> };

Expand Down Expand Up @@ -129,6 +137,14 @@ export type Config = {
index: string;
scope?: 'local' | 'global' | string[];
} | undefined;
elasticsearch: {
host: string;
port: string;
user: string;
pass: string;
ssl?: boolean;
index: string;
} | undefined;
proxy: string | undefined;
proxySmtp: string | undefined;
proxyBypassHosts: string[] | undefined;
Expand Down Expand Up @@ -235,6 +251,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
127 changes: 127 additions & 0 deletions packages/backend/src/core/SearchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { isUserRelated } from '@/misc/is-user-related.js';
import { CacheService } from '@/core/CacheService.js';
import { QueryService } from '@/core/QueryService.js';
import { IdService } from '@/core/IdService.js';
import { Client as ElasticSearch } from '@elastic/elasticsearch';
import type { Index, MeiliSearch } from 'meilisearch';

type K = string;
Expand Down Expand Up @@ -65,6 +66,7 @@ function compileQuery(q: Q): string {
export class SearchService {
private readonly meilisearchIndexScope: 'local' | 'global' | string[] = 'local';
private meilisearchNoteIndex: Index | null = null;
private elasticsearchNoteIndex: string | null = null;

constructor(
@Inject(DI.config)
Expand All @@ -73,6 +75,9 @@ export class SearchService {
@Inject(DI.meilisearch)
private meilisearch: MeiliSearch | null,

@Inject(DI.elasticsearch)
private elasticsearch: ElasticSearch | null,

@Inject(DI.notesRepository)
private notesRepository: NotesRepository,

Expand Down Expand Up @@ -106,6 +111,44 @@ export class SearchService {
});
}

if (!meilisearch && this.elasticsearch) {
const indexName = `${config.elasticsearch!.index}---notes`;
this.elasticsearchNoteIndex = indexName;

this.elasticsearch.indices.exists({
index: indexName,
}).then((indexExists) => {
if (!indexExists) {
this.elasticsearch?.indices.create({
index: indexName + `-${new Date().toISOString().slice(0, 7).replace(/-/g, '')}`,
body: {
mappings: {
properties: {
text: { type: 'text' },
cw: { type: 'text' },
createdAt: { type: 'long' },
userId: { type: 'keyword' },
userHost: { type: 'keyword' },
channelId: { type: 'keyword' },
tags: { type: 'keyword' },
},
},
settings: {
//TODO: Make settings for optimization.
},
},
}).catch((error) => {
console.error(error);
});
}
}).catch((error) => {
console.error(error);
});
} else {
console.error('Elasticsearch is not available');
this.elasticsearchNoteIndex = null;
}

if (config.meilisearch?.scope) {
this.meilisearchIndexScope = config.meilisearch.scope;
}
Expand Down Expand Up @@ -145,6 +188,24 @@ export class SearchService {
primaryKey: 'id',
});
}

if (!this.meilisearch && this.elasticsearch) {
const body = {
createdAt: this.idService.parse(note.id).date.getTime(),
userId: note.userId,
userHost: note.userHost,
channelId: note.channelId,
cw: note.cw,
text: note.text,
tags: note.tags,
};

await this.elasticsearch.index({
index: this.elasticsearchNoteIndex + `-${new Date().toISOString().slice(0, 7).replace(/-/g, '')}` as string,
id: note.id,
body: body,
});
}
}

@bindThis
Expand All @@ -154,6 +215,13 @@ export class SearchService {
if (this.meilisearch) {
this.meilisearchNoteIndex!.deleteDocument(note.id);
}

if (!this.meilisearch && this.elasticsearch) {
(this.elasticsearch.delete)({
index: this.elasticsearchNoteIndex + `-${new Date().toISOString().slice(0, 7).replace(/-/g, '')}` as string,
id: note.id,
});
}
}

@bindThis
Expand Down Expand Up @@ -205,6 +273,65 @@ export class SearchService {
return true;
});
return notes.sort((a, b) => a.id > b.id ? -1 : 1);
} else if (this.elasticsearch) {
const esFilter: any = {
bool: {
must: [],
},
};
if (pagination.untilId) esFilter.bool.must.push({ range: { createdAt: { lt: this.idService.parse(pagination.untilId).date.getTime() } } });
if (pagination.sinceId) esFilter.bool.must.push({ range: { createdAt: { gt: this.idService.parse(pagination.sinceId).date.getTime() } } });
if (opts.userId) esFilter.bool.must.push({ term: { userId: opts.userId } });
if (opts.channelId) esFilter.bool.must.push({ term: { channelId: opts.channelId } });
if (opts.host) {
if (opts.host === '.') {
esFilter.bool.must.push({ bool: { must_not: [{ exists: { field: 'userHost' } }] } });
} else {
esFilter.bool.must.push({ term: { userHost: opts.host } });
}
}
const res = await (this.elasticsearch.search)({
index: this.elasticsearchNoteIndex + `*` as string,
body: {
query: {
bool: {
must: [
{
bool: {
should: [
{ wildcard: { "text": { value: `*${q}*` }, } },
{ simple_query_string: { fields: ["text"], "query": q, default_operator: 'and', } },
],
minimum_should_match: 1,
},
},
esFilter,
]
},
},
},
sort: [{ createdAt: { order: "desc" } }],
_source: ['id', 'createdAt'],
size: pagination.limit,

});
const noteIds = res.hits.hits.map((hit: any) => hit._id);
if (noteIds.length === 0) return [];
const [
userIdsWhoMeMuting,
userIdsWhoBlockingMe,
] = me ? await Promise.all([
this.cacheService.userMutingsCache.fetch(me.id),
this.cacheService.userBlockedCache.fetch(me.id),
]) : [new Set<string>(), new Set<string>()];
const notes = (await this.notesRepository.findBy({
id: In(noteIds),
})).filter(note => {
if (me && isUserRelated(note, userIdsWhoBlockingMe)) return false;
if (me && isUserRelated(note, userIdsWhoMeMuting)) return false;
return true;
});
return notes.sort((a, b) => a.id > b.id ? -1 : 1);
} else {
const query = this.queryService.makePaginationQuery(this.notesRepository.createQueryBuilder('note'), pagination.sinceId, pagination.untilId);

Expand Down
1 change: 1 addition & 0 deletions packages/backend/src/di-symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const DI = {
config: Symbol('config'),
db: Symbol('db'),
meilisearch: Symbol('meilisearch'),
elasticsearch: Symbol('elasticsearch'),
redis: Symbol('redis'),
redisForPub: Symbol('redisForPub'),
redisForSub: Symbol('redisForSub'),
Expand Down
27 changes: 26 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading