fix search limit and performance (#2529)

This commit is contained in:
Philip Okugbe
2026-09-29 20:27:25 +01:00
committed by GitHub
parent 0e839c8cd6
commit 870b71d2aa
3 changed files with 28 additions and 7 deletions
@@ -6,11 +6,15 @@ import {
IsOptional,
IsString,
IsUUID,
MaxLength,
} from 'class-validator';
export const SEARCH_QUERY_MAX_LENGTH = 200;
export class SearchDTO {
@IsOptional()
@IsString()
@MaxLength(SEARCH_QUERY_MAX_LENGTH)
query?: string;
@IsOptional()
@@ -61,6 +65,7 @@ export class SearchPublicSpaceDTO extends SearchDTO {
export class SearchSuggestionDTO {
@IsString()
@MaxLength(SEARCH_QUERY_MAX_LENGTH)
query: string;
@IsOptional()
+22 -6
View File
@@ -55,11 +55,6 @@ export class SearchService {
: sql<number>`ts_rank(tsv, to_tsquery('english', f_unaccent(${searchQuery})))`.as(
'rank',
);
const highlightColumn = browseByFilters || titleOnly
? sql<string>`''`.as('highlight')
: sql<string>`ts_headline('english', text_content, to_tsquery('english', f_unaccent(${searchQuery})),'MinWords=9, MaxWords=10, MaxFragments=3')`.as(
'highlight',
);
let queryResults = this.db
.selectFrom('pages')
@@ -73,7 +68,6 @@ export class SearchService {
'createdAt',
'updatedAt',
rankColumn,
highlightColumn,
])
.$if(!browseByFilters && !titleOnly, (qb) =>
qb.where(
@@ -189,10 +183,32 @@ export class SearchService {
results = results.filter((r: any) => accessibleSet.has(r.id));
}
if (!browseByFilters && !titleOnly && results.length > 0) {
const highlights = await this.db
.selectFrom('pages')
.select([
'id',
sql<string>`ts_headline('english', substring(text_content, 1, 100000), to_tsquery('english', f_unaccent(${searchQuery})),'MinWords=9, MaxWords=10, MaxFragments=3')`.as(
'highlight',
),
])
.where(
'id',
'in',
results.map((r: any) => r.id),
)
.execute();
const highlightById = new Map(highlights.map((h) => [h.id, h.highlight]));
for (const result of results) {
result.highlight = highlightById.get(result.id) ?? '';
}
}
//@ts-ignore
const searchResults = results.map((result: SearchResponseDto) => {
result.wholeWord = true
if (!result.highlight) {
result.highlight = '';
result.matchedText = [];
return result;
}