diff --git a/.config/example.yml b/.config/example.yml index cec6b6611..16349f8d0 100644 --- a/.config/example.yml +++ b/.config/example.yml @@ -68,17 +68,6 @@ redis: #prefix: example-prefix #db: 1 -# ┌─────────────────────────────┐ -#───┘ Elasticsearch configuration └───────────────────────────── - -# Elasticsearch is optional. -#elasticsearch: -# host: localhost -# port: 9200 -# ssl: false -# user: -# pass: - # ┌─────────────────────┐ #───┘ Other configuration └───────────────────────────────────── diff --git a/.gitignore b/.gitignore index ac01738f1..a8a9c09e2 100644 --- a/.gitignore +++ b/.gitignore @@ -29,7 +29,6 @@ built /data /.cache-loader /db -/elasticsearch npm-debug.log *.pem run.bat diff --git a/packages/backend/package.json b/packages/backend/package.json index 641f41a91..a8b9fda2c 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -15,7 +15,6 @@ "@bull-board/api": "^4.3.1", "@bull-board/koa": "^4.3.1", "@discordapp/twemoji": "14.0.2", - "@elastic/elasticsearch": "7.11.0", "@koa/cors": "3.1.0", "@koa/multer": "3.0.0", "@koa/router": "9.0.1", diff --git a/packages/backend/src/config/types.ts b/packages/backend/src/config/types.ts index 2d163f91b..2cc5db7bf 100644 --- a/packages/backend/src/config/types.ts +++ b/packages/backend/src/config/types.ts @@ -24,14 +24,6 @@ export type Source = { db?: number; prefix?: string; }; - elasticsearch?: { - host: string; - port: number; - ssl?: boolean; - user?: string; - pass?: string; - index?: string; - }; proxy?: string; proxySmtp?: string; diff --git a/packages/backend/src/db/elasticsearch.ts b/packages/backend/src/db/elasticsearch.ts deleted file mode 100644 index d98c5d180..000000000 --- a/packages/backend/src/db/elasticsearch.ts +++ /dev/null @@ -1,56 +0,0 @@ -import * as elasticsearch from '@elastic/elasticsearch'; -import config from '@/config/index.js'; - -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; diff --git a/packages/backend/src/server/api/endpoints/meta.ts b/packages/backend/src/server/api/endpoints/meta.ts index 35c8839b4..7687faec2 100644 --- a/packages/backend/src/server/api/endpoints/meta.ts +++ b/packages/backend/src/server/api/endpoints/meta.ts @@ -203,10 +203,6 @@ export const meta = { type: 'boolean', optional: false, nullable: false, }, - elasticsearch: { - type: 'boolean', - optional: false, nullable: false, - }, hcaptcha: { type: 'boolean', optional: false, nullable: false, @@ -319,7 +315,6 @@ export default define(meta, paramDef, async () => { localTimeLine: !instance.disableLocalTimeline, globalTimeLine: !instance.disableGlobalTimeline, emailRequiredForSignup: instance.emailRequiredForSignup, - elasticsearch: config.elasticsearch ? true : false, hcaptcha: instance.enableHcaptcha, recaptcha: instance.enableRecaptcha, objectStorage: instance.useObjectStorage, diff --git a/packages/backend/src/server/api/endpoints/notes/search.ts b/packages/backend/src/server/api/endpoints/notes/search.ts index 7958175c0..3d026fcf7 100644 --- a/packages/backend/src/server/api/endpoints/notes/search.ts +++ b/packages/backend/src/server/api/endpoints/notes/search.ts @@ -1,7 +1,4 @@ -import { In } from 'typeorm'; import { Notes } from '@/models/index.js'; -import config from '@/config/index.js'; -import es from '@/db/elasticsearch.js'; import define from '@/server/api/define.js'; import { makePaginationQuery } from '@/server/api/common/make-pagination-query.js'; import { visibilityQuery } from '@/server/api/common/generate-visibility-query.js'; @@ -45,96 +42,34 @@ export const paramDef = { // eslint-disable-next-line import/no-default-export export default define(meta, paramDef, async (ps, me) => { - if (es == null) { - const query = makePaginationQuery(Notes.createQueryBuilder('note'), ps.sinceId, ps.untilId); + const query = makePaginationQuery(Notes.createQueryBuilder('note'), ps.sinceId, ps.untilId); - if (ps.userId) { - query.andWhere('note.userId = :userId', { userId: ps.userId }); - } else if (ps.host) { - query.andWhere('note.userHost = :host', { host: ps.host }); - } else if (ps.channelId) { - query.andWhere('note.channelId = :channelId', { channelId: ps.channelId }); - } - - query - .andWhere('note.text ILIKE :q', { q: `%${ps.query}%` }) - .innerJoinAndSelect('note.user', 'user') - .leftJoinAndSelect('user.avatar', 'avatar') - .leftJoinAndSelect('user.banner', 'banner') - .leftJoinAndSelect('note.reply', 'reply') - .leftJoinAndSelect('note.renote', 'renote') - .leftJoinAndSelect('reply.user', 'replyUser') - .leftJoinAndSelect('replyUser.avatar', 'replyUserAvatar') - .leftJoinAndSelect('replyUser.banner', 'replyUserBanner') - .leftJoinAndSelect('renote.user', 'renoteUser') - .leftJoinAndSelect('renoteUser.avatar', 'renoteUserAvatar') - .leftJoinAndSelect('renoteUser.banner', 'renoteUserBanner'); - - if (me) generateMutedUserQuery(query, me); - if (me) generateBlockedUserQuery(query, me); - - const notes = await visibilityQuery(query, me).take(ps.limit).getMany(); - - return await Notes.packMany(notes, me); - } else { - const userQuery = ps.userId != null ? [{ - term: { - userId: ps.userId, - }, - }] : []; - - const hostQuery = ps.userId == null ? - ps.host === null ? [{ - bool: { - must_not: { - exists: { - field: 'userHost', - }, - }, - }, - }] : ps.host !== undefined ? [{ - term: { - userHost: ps.host, - }, - }] : [] - : []; - - const result = await es.search({ - index: config.elasticsearch.index || 'misskey_note', - body: { - size: ps.limit, - from: ps.offset, - query: { - bool: { - must: [{ - simple_query_string: { - fields: ['text'], - query: ps.query.toLowerCase(), - default_operator: 'and', - }, - }, ...hostQuery, ...userQuery], - }, - }, - sort: [{ - _doc: 'desc', - }], - }, - }); - - const hits = result.body.hits.hits.map((hit: any) => hit._id); - - if (hits.length === 0) return []; - - // Fetch found notes - const notes = await Notes.find({ - where: { - id: In(hits), - }, - order: { - id: -1, - }, - }); - - return await Notes.packMany(notes, me); + if (ps.userId) { + query.andWhere('note.userId = :userId', { userId: ps.userId }); + } else if (ps.host) { + query.andWhere('note.userHost = :host', { host: ps.host }); + } else if (ps.channelId) { + query.andWhere('note.channelId = :channelId', { channelId: ps.channelId }); } + + query + .andWhere('note.text ILIKE :q', { q: `%${ps.query}%` }) + .innerJoinAndSelect('note.user', 'user') + .leftJoinAndSelect('user.avatar', 'avatar') + .leftJoinAndSelect('user.banner', 'banner') + .leftJoinAndSelect('note.reply', 'reply') + .leftJoinAndSelect('note.renote', 'renote') + .leftJoinAndSelect('reply.user', 'replyUser') + .leftJoinAndSelect('replyUser.avatar', 'replyUserAvatar') + .leftJoinAndSelect('replyUser.banner', 'replyUserBanner') + .leftJoinAndSelect('renote.user', 'renoteUser') + .leftJoinAndSelect('renoteUser.avatar', 'renoteUserAvatar') + .leftJoinAndSelect('renoteUser.banner', 'renoteUserBanner'); + + if (me) generateMutedUserQuery(query, me); + if (me) generateBlockedUserQuery(query, me); + + const notes = await visibilityQuery(query, me).take(ps.limit).getMany(); + + return await Notes.packMany(notes, me); }); diff --git a/packages/backend/src/services/note/index.ts b/packages/backend/src/services/note/index.ts index f2bd21db1..d8cc7a99f 100644 --- a/packages/backend/src/services/note/index.ts +++ b/packages/backend/src/services/note/index.ts @@ -1,8 +1,5 @@ -import es from '@/db/elasticsearch.js'; -import config from '@/config/index.js'; import { Note } from '@/models/entities/note.js'; import { UserProfiles } from '@/models/index.js'; -import { normalizeForSearch } from '@/misc/normalize-for-search.js'; import { Cache } from '@/misc/cache.js'; import { UserProfile } from '@/models/entities/user-profile.js'; import { MINUTE } from '@/const.js'; @@ -16,17 +13,3 @@ export const mutedWordsCache = new Cache<{ userId: UserProfile['userId']; mutedW select: ['userId', 'mutedWords'], }), ); - -export function index(note: Note): void { - if (note.text == null || config.elasticsearch == null) return; - - es.index({ - index: config.elasticsearch.index || 'misskey_note', - id: note.id.toString(), - body: { - text: normalizeForSearch(note.text), - userId: note.userId, - userHost: note.userHost, - }, - }); -} diff --git a/packages/backend/src/services/note/side-effects.ts b/packages/backend/src/services/note/side-effects.ts index fd7c217c7..fe50ed388 100644 --- a/packages/backend/src/services/note/side-effects.ts +++ b/packages/backend/src/services/note/side-effects.ts @@ -26,7 +26,7 @@ import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc.js import { createNotification } from '../create-notification.js'; import { addNoteToAntenna } from '../add-note-to-antenna.js'; import { deliverToRelays } from '../relay.js'; -import { mutedWordsCache, index } from './index.js'; +import { mutedWordsCache } from './index.js'; import { Polls } from '@/models/index.js'; import { Poll } from '@/models/entities/poll.js'; @@ -399,9 +399,6 @@ export async function sideEffects(user: User, note: Note, silent = false, create // TODO AP deliver } - - // Register to search database - index(note); } async function notifyWatchers(noteId: Note['id'], user: { id: User['id']; }, nm: NotificationManager, type: NotificationType): Promise {