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

Fix/word_similarity - master #1545

Merged
merged 4 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
29 changes: 29 additions & 0 deletions migration/1715086559930-add_pg_trgm_indexes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddPgTrgmIndexes1715086559930 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
'CREATE INDEX if not exists trgm_idx_project_title ON project USING GIN (title gin_trgm_ops);',
);
await queryRunner.query(
'CREATE INDEX if not exists trgm_idx_project_description ON project USING GIN (description gin_trgm_ops);',
);
await queryRunner.query(
'CREATE INDEX if not exists trgm_idx_project_impact_location ON project USING GIN ("impactLocation" gin_trgm_ops);',
);
await queryRunner.query(
'CREATE INDEX if not exists trgm_idx_user_name ON public.user USING GIN ("name" gin_trgm_ops);',
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query('drop index if exists trgm_idx_project_title;');
await queryRunner.query(
'drop index if exists trgm_idx_project_description;',
);
await queryRunner.query(
'drop index if exists trgm_idx_project_impact_location;',
);
await queryRunner.query('drop index if exists trgm_idx_user_name;');
}
}
3 changes: 3 additions & 0 deletions src/entities/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ export class Project extends BaseEntity {
@Column({ nullable: true })
image?: string;

@Index('trgm_idx_project_impact_location', { synchronize: false })
@Field({ nullable: true })
@Column({ nullable: true })
impactLocation?: string;
Expand Down Expand Up @@ -606,6 +607,7 @@ export class ProjectUpdate extends BaseEntity {
@PrimaryGeneratedColumn()
readonly id: number;

@Index('trgm_idx_project_title', { synchronize: false })
@Field(_type => String)
@Column()
title: string;
Expand Down Expand Up @@ -667,6 +669,7 @@ export class ProjectUpdate extends BaseEntity {
@Column('text', { nullable: true })
organizationWebsite: string;

@Index('trgm_idx_project_description', { synchronize: false })
@Field({ nullable: true })
@Column('text', { nullable: true })
organizationDescription: string;
Expand Down
2 changes: 2 additions & 0 deletions src/entities/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Column,
CreateDateColumn,
Entity,
Index,
JoinTable,
ManyToMany,
OneToMany,
Expand Down Expand Up @@ -86,6 +87,7 @@ export class User extends BaseEntity {
@Column({ nullable: true })
lastName?: string;

@Index('trgm_idx_user_name', { synchronize: false })
@Field(_type => String, { nullable: true })
@Column({ nullable: true })
name?: string;
Expand Down
54 changes: 20 additions & 34 deletions src/resolvers/projectResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import { Donation } from '../entities/donation';
import { ProjectImage } from '../entities/projectImage';
import { ApolloContext } from '../types/ApolloContext';
import { publicSelectionFields, User } from '../entities/user';
import config from '../config';
import { Context } from '../context';
import SentryLogger from '../sentryLogger';
import {
Expand Down Expand Up @@ -319,40 +318,27 @@ export class ProjectResolver {
searchTerm?: string,
) {
if (!searchTerm) return query;
const similarityThreshold =
Number(config.get('PROJECT_SEARCH_SIMILARITY_THRESHOLD')) || 0.4;

return query.andWhere(
new Brackets(qb => {
qb.where(
'WORD_SIMILARITY(project.title, :searchTerm) > :similarityThreshold',
{
searchTerm: `%${searchTerm}%`,
similarityThreshold,
},

return (
query
// For future use! This is the way to use similarity in TypeORM
// .addSelect('similarity(project.title, :searchTerm)', 'title_slm')
// .addSelect('similarity(project.description, :searchTerm)', 'desc_slm')
// .addSelect('similarity(project.impactLocation, :searchTerm)', 'loc_slm')
// .setParameter('searchTerm', searchTerm)
.andWhere(
new Brackets(qb => {
qb.where('project.title % :searchTerm ', {
searchTerm,
})
.orWhere('project.description % :searchTerm ', {
searchTerm,
})
.orWhere('project.impactLocation % :searchTerm', {
searchTerm,
});
}),
)
.orWhere(
'WORD_SIMILARITY(project.description, :searchTerm) > :similarityThreshold',
{
searchTerm: `%${searchTerm}%`,
similarityThreshold,
},
)
.orWhere(
'WORD_SIMILARITY(project.impactLocation, :searchTerm) > :similarityThreshold',
{
searchTerm: `%${searchTerm}%`,
similarityThreshold,
},
)
.orWhere(
'WORD_SIMILARITY(user.name, :searchTerm) > :similarityThreshold',
{
searchTerm: `%${searchTerm}%`,
similarityThreshold,
},
);
}),
);
}

Expand Down
15 changes: 15 additions & 0 deletions src/server/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ export async function bootstrap() {

Container.set(DataSource, AppDataSource.getDataSource());

await setDatabaseParameters(AppDataSource.getDataSource());

const dropSchema = config.get('DROP_DATABASE') === 'true';
if (dropSchema) {
// eslint-disable-next-line no-console
Expand Down Expand Up @@ -449,3 +451,16 @@ export async function bootstrap() {
await initializeCronJobs();
}
}

async function setDatabaseParameters(ds: DataSource) {
await setPgTrgmParameters(ds);
}

async function setPgTrgmParameters(ds: DataSource) {
const similarityThreshold =
Number(config.get('PROJECT_SEARCH_SIMILARITY_THRESHOLD')) || 0.1;
await ds.query(`SET pg_trgm.similarity_threshold TO ${similarityThreshold};`);
await ds.query(
`SET pg_trgm.word_similarity_threshold TO ${similarityThreshold};`,
);
}
4 changes: 4 additions & 0 deletions test/pre-test-scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import { createDonationethUser1701756190381 } from '../migration/1701756190381-c
import { ChainType } from '../src/types/network';
import { COINGECKO_TOKEN_IDS } from '../src/adapters/price/CoingeckoPriceAdapter';
import { ProjectActualMatchinView151713700147145 } from '../migration/1713700147145-project_actual_matchin_view_15';
import { EnablePgTrgmExtension1713859866338 } from '../migration/1713859866338-enable_pg_trgm_extension';
import { AddPgTrgmIndexes1715086559930 } from '../migration/1715086559930-add_pg_trgm_indexes';

async function seedDb() {
await seedUsers();
Expand Down Expand Up @@ -479,6 +481,8 @@ async function runMigrations() {
);
await new createDonationethUser1701756190381().up(queryRunner);
await new ProjectActualMatchinView151713700147145().up(queryRunner);
await new EnablePgTrgmExtension1713859866338().up(queryRunner);
await new AddPgTrgmIndexes1715086559930().up(queryRunner);
} finally {
await queryRunner.release();
}
Expand Down
Loading