Skip to content
Closed
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
34 changes: 0 additions & 34 deletions package-lock.json

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

24 changes: 24 additions & 0 deletions src/migrations/1763000000000-AddCourseFtsSearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddCourseFtsSearch1763000000000 implements MigrationInterface {
name = 'AddCourseFtsSearch1763000000000';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE "course"
ADD COLUMN IF NOT EXISTS "search_vector" tsvector
GENERATED ALWAYS AS (
to_tsvector('english', coalesce(title, '') || ' ' || coalesce(description, ''))
) STORED
`);
await queryRunner.query(`
CREATE INDEX IF NOT EXISTS "IDX_course_search_vector"
ON "course" USING GIN ("search_vector")
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query('DROP INDEX IF EXISTS "IDX_course_search_vector"');
await queryRunner.query('ALTER TABLE "course" DROP COLUMN IF EXISTS "search_vector"');
}
}
1 change: 0 additions & 1 deletion src/search/search.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Test, TestingModule } from '@nestjs/testing';
import { ElasticsearchService as NestElasticsearchService } from '@nestjs/elasticsearch';
import { getRepositoryToken } from '@nestjs/typeorm';
import { SearchService } from './search.service';
import { SEARCH_CONSTANTS } from './search.constants';
import { Course } from '../courses/entities/course.entity';

const mockQueryBuilder = {
Expand Down
8 changes: 6 additions & 2 deletions src/search/search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ export class SearchService {
try {
const qb = this.courseRepository.createQueryBuilder('course');
if (safeQuery) {
qb.where('course.title ILIKE :query OR course.description ILIKE :query', {
query: `%${safeQuery}%`,
qb.where("course.search_vector @@ plainto_tsquery('english', :query)", {
query: safeQuery,
});
}

Expand All @@ -83,6 +83,10 @@ export class SearchService {
if (sort === 'price_asc') qb.orderBy('course.price', 'ASC');
else if (sort === 'price_desc') qb.orderBy('course.price', 'DESC');
else if (sort === 'newest') qb.orderBy('course.createdAt', 'DESC');
else if (sort === 'relevance')
qb.orderBy("ts_rank(course.search_vector, plainto_tsquery('english', :query))", 'DESC');
else if (safeQuery)
qb.orderBy("ts_rank(course.search_vector, plainto_tsquery('english', :query))", 'DESC');
else qb.orderBy('course.createdAt', 'DESC');

const skip = (page - 1) * limit;
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.build.tsbuildinfo

Large diffs are not rendered by default.