Merge pull request 'use PGroonga' (#3) from pgroonga into master

Reviewed-on: #3
This commit is contained in:
ゆめ 2024-11-06 18:31:38 -06:00
commit ad33c0efb5
9 changed files with 35 additions and 7 deletions

View file

@ -15,7 +15,7 @@ services:
db: db:
restart: always restart: always
image: postgres:15-alpine image: l1drm/postgres-pgroonga:alpine-15-znver4
ports: ports:
- "5432:5432" - "5432:5432"
env_file: env_file:

View file

@ -62,7 +62,7 @@ services:
db: db:
restart: always restart: always
image: postgres:15-alpine image: l1drm/postgres-pgroonga:alpine-15-znver4
user: "${MISSKEY_UID}:${MISSKEY_GID}" user: "${MISSKEY_UID}:${MISSKEY_GID}"
networks: networks:
- internal_network - internal_network

View file

@ -0,0 +1,18 @@
export class Pgroonga1730937958242 {
name = 'Pgroonga1730937958242'
async up(queryRunner) {
await queryRunner.query(`CREATE INDEX CONCURRENTLY "IDX_f27f5d88941e57442be75ba9c8" ON "note" USING "pgroonga" ("text")`);
await queryRunner.query(`CREATE INDEX CONCURRENTLY "IDX_7cc8d9b0ee7861b4e5dc86ad85" ON "note" USING "pgroonga" ("cw" pgroonga_varchar_full_text_search_ops_v2)`);
await queryRunner.query(`CREATE INDEX CONCURRENTLY "IDX_065d4d8f3b5adb4a08841eae3c" ON "user" USING "pgroonga" ("name" pgroonga_varchar_full_text_search_ops_v2)`);
await queryRunner.query(`CREATE INDEX CONCURRENTLY "IDX_fcb770976ff8240af5799e3ffc" ON "user_profile" USING "pgroonga" ("description" pgroonga_varchar_full_text_search_ops_v2) `);
}
async down(queryRunner) {
await queryRunner.query(`DROP INDEX "public"."IDX_f27f5d88941e57442be75ba9c8"`);
await queryRunner.query(`DROP INDEX "public"."IDX_7cc8d9b0ee7861b4e5dc86ad85"`);
await queryRunner.query(`DROP INDEX "public"."IDX_065d4d8f3b5adb4a08841eae3c"`);
await queryRunner.query(`DROP INDEX "public"."IDX_fcb770976ff8240af5799e3ffc"`);
}
}

View file

@ -215,7 +215,7 @@ export class SearchService {
} }
query query
.andWhere('note.text ILIKE :q', { q: `%${ sqlLikeEscape(q) }%` }) .andWhere('note.text &@~ :q', { q: sqlLikeEscape(q) })
.innerJoinAndSelect('note.user', 'user') .innerJoinAndSelect('note.user', 'user')
.leftJoinAndSelect('note.reply', 'reply') .leftJoinAndSelect('note.reply', 'reply')
.leftJoinAndSelect('note.renote', 'renote') .leftJoinAndSelect('note.renote', 'renote')

View file

@ -50,6 +50,7 @@ export class MiNote {
public threadId: string | null; public threadId: string | null;
// TODO: varcharにしたい // TODO: varcharにしたい
@Index() // USING pgroonga
@Column('text', { @Column('text', {
nullable: true, nullable: true,
}) })
@ -60,6 +61,7 @@ export class MiNote {
}) })
public name: string | null; public name: string | null;
@Index() // USING pgroonga pgroonga_varchar_full_text_search_ops_v2
@Column('varchar', { @Column('varchar', {
length: 512, nullable: true, length: 512, nullable: true,
}) })

View file

@ -49,6 +49,7 @@ export class MiUser {
}) })
public usernameLower: string; public usernameLower: string;
@Index() // USING pgroonga pgroonga_varchar_full_text_search_ops_v2
@Column('varchar', { @Column('varchar', {
length: 128, nullable: true, length: 128, nullable: true,
comment: 'The name of the User.', comment: 'The name of the User.',

View file

@ -36,6 +36,7 @@ export class MiUserProfile {
}) })
public birthday: string | null; public birthday: string | null;
@Index() // USING pgroonga pgroonga_varchar_full_text_search_ops_v2
@Column('varchar', { @Column('varchar', {
length: 2048, nullable: true, length: 2048, nullable: true,
comment: 'The description (bio) of the User.', comment: 'The description (bio) of the User.',

View file

@ -31,6 +31,11 @@ export const meta = {
code: 'UNAVAILABLE', code: 'UNAVAILABLE',
id: '0b44998d-77aa-4427-80d0-d2c9b8523011', id: '0b44998d-77aa-4427-80d0-d2c9b8523011',
}, },
noSuchNote: {
message: 'Query is empty.',
code: 'QUERY_IS_EMPTY',
id: 'd0410b51-f409-4667-8118-cfe999e453c3',
},
}, },
} as const; } as const;
@ -62,6 +67,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
private roleService: RoleService, private roleService: RoleService,
) { ) {
super(meta, paramDef, async (ps, me) => { super(meta, paramDef, async (ps, me) => {
if (ps.query.trim().length === 0) throw new ApiError(meta.errors.noSuchNote);
const policies = await this.roleService.getUserPolicies(me ? me.id : null); const policies = await this.roleService.getUserPolicies(me ? me.id : null);
if (!policies.canSearchNotes) { if (!policies.canSearchNotes) {
throw new ApiError(meta.errors.unavailable); throw new ApiError(meta.errors.unavailable);

View file

@ -63,12 +63,12 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
const nameQuery = this.usersRepository.createQueryBuilder('user') const nameQuery = this.usersRepository.createQueryBuilder('user')
.where(new Brackets(qb => { .where(new Brackets(qb => {
qb.where('user.name ILIKE :query', { query: '%' + sqlLikeEscape(ps.query) + '%' }); qb.where('user.name &@~ :query', { query: ps.query });
if (isUsername) { if (isUsername) {
qb.orWhere('user.usernameLower LIKE :username', { username: sqlLikeEscape(ps.query.replace('@', '').toLowerCase()) + '%' }); qb.orWhere('user.usernameLower LIKE :username', { username: sqlLikeEscape(ps.query.replace('@', '').toLowerCase()) + '%' });
} else if (this.userEntityService.validateLocalUsername(ps.query)) { // Also search username if it qualifies as username } else if (userEntityService.validateLocalUsername(ps.query)) { // Also search username if it qualifies as username
qb.orWhere('user.usernameLower LIKE :username', { username: '%' + sqlLikeEscape(ps.query.toLowerCase()) + '%' }); qb.orWhere('user.usernameLower LIKE :username', { username: ps.query.toLowerCase() + '%' });
} }
})) }))
.andWhere(new Brackets(qb => { .andWhere(new Brackets(qb => {
@ -93,7 +93,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
if (users.length < ps.limit) { if (users.length < ps.limit) {
const profQuery = this.userProfilesRepository.createQueryBuilder('prof') const profQuery = this.userProfilesRepository.createQueryBuilder('prof')
.select('prof.userId') .select('prof.userId')
.where('prof.description ILIKE :query', { query: '%' + sqlLikeEscape(ps.query) + '%' }); .where('prof.description &@~ :query', { query: ps.query });
if (ps.origin === 'local') { if (ps.origin === 'local') {
profQuery.andWhere('prof.userHost IS NULL'); profQuery.andWhere('prof.userHost IS NULL');