mirror of
https://github.com/docmost/docmost.git
synced 2026-09-12 16:31:17 +08:00
feat: enhanced search filters (#2398)
* advanced search filters * improve labelId filtering in query * index label ids on typesense * index label ids on typesense * filters on AI search * refactor: detect typesense schema drift from the declared collection schema * feat: show secondary search filters only when applied Created by and Labels now stay hidden until they hold a value; a Filter button at the right end of the bar lists the hidden ones and opens the picked filter's dropdown immediately. * fix: keep applied search filters in the order they were added * feat: add title-only search mode Adds a titleOnly flag to page search: the Postgres driver matches titles via trigram-indexed unaccented LIKE ranked by word_similarity, and a Match filter in the search spotlight switches between everything and title-only matching. * refactor: make title-only search a toggle chip * fix: drop stale results when the search content type changes * feat: add trigram index on attachment file names * feat: extend title-only search to attachments The titleOnly flag now matches attachment file names via the separator-normalized trigram expression, and the toggle chip stays available in attachment mode as File name only. * refactor: keep the title-only label uniform across search modes * feat: index txt attachments for content search * feat: pin the current user at the top of the creator filter * refactor: dim the you suffix in the creator filter * feat: browse label-filtered pages without a query Selecting labels now lists their pages newest-first before any text is typed, on both the Postgres and Typesense drivers. * fix: match any selected label instead of requiring all * fix: use a gray spotlight selection instead of primary blue * feat: include search results in the spotlight tab order * feat: show last updated time on search results * fix: stop empty-state flicker while the search query debounces * feat: browse creator-filtered results without a query Selecting a creator with an empty query now lists their pages (or attachments in attachment mode) newest-first, with the same space and page permission filtering as typed search. * fix: show Untitled for pages without a title in search results * quick page sharing fix * minor search filter fixes --------- Co-authored-by: Philipinho <16838612+Philipinho@users.noreply.github.com>
This commit is contained in:
@@ -29,12 +29,36 @@ export class SearchService {
|
||||
workspaceId: string;
|
||||
},
|
||||
): Promise<{ items: SearchResponseDto[] }> {
|
||||
const { query } = searchParams;
|
||||
const query = searchParams.query?.trim() ?? '';
|
||||
const labelIds = [...new Set(searchParams.labelIds ?? [])];
|
||||
// selected filters (labels, creator) are browsable without a query
|
||||
const browseByFilters =
|
||||
query.length < 1 &&
|
||||
(labelIds.length > 0 || Boolean(searchParams.creatorId));
|
||||
|
||||
if (query.length < 1) {
|
||||
if (query.length < 1 && !browseByFilters) {
|
||||
return { items: [] };
|
||||
}
|
||||
const searchQuery = tsquery(query.trim() + '*');
|
||||
const searchQuery = tsquery(query + '*');
|
||||
const titleOnly = searchParams.titleOnly === true;
|
||||
const titleQuery = query;
|
||||
// escape LIKE wildcards; ranking keeps the raw query
|
||||
const titleLikeQuery = query.replace(/[\\%_]/g, '\\$&');
|
||||
|
||||
const rankColumn = browseByFilters
|
||||
? sql<number>`0`.as('rank')
|
||||
: titleOnly
|
||||
? sql<number>`word_similarity(lower(f_unaccent(${titleQuery})), lower(f_unaccent(pages.title)))`.as(
|
||||
'rank',
|
||||
)
|
||||
: 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')
|
||||
@@ -47,23 +71,41 @@ export class SearchService {
|
||||
'creatorId',
|
||||
'createdAt',
|
||||
'updatedAt',
|
||||
sql<number>`ts_rank(tsv, to_tsquery('english', f_unaccent(${searchQuery})))`.as(
|
||||
'rank',
|
||||
),
|
||||
sql<string>`ts_headline('english', text_content, to_tsquery('english', f_unaccent(${searchQuery})),'MinWords=9, MaxWords=10, MaxFragments=3')`.as(
|
||||
'highlight',
|
||||
),
|
||||
rankColumn,
|
||||
highlightColumn,
|
||||
])
|
||||
.where(
|
||||
'tsv',
|
||||
'@@',
|
||||
sql<string>`to_tsquery('english', f_unaccent(${searchQuery}))`,
|
||||
.$if(!browseByFilters && !titleOnly, (qb) =>
|
||||
qb.where(
|
||||
'tsv',
|
||||
'@@',
|
||||
sql<string>`to_tsquery('english', f_unaccent(${searchQuery}))`,
|
||||
),
|
||||
)
|
||||
.$if(!browseByFilters && titleOnly, (qb) =>
|
||||
qb.where((eb) =>
|
||||
eb(
|
||||
sql`lower(f_unaccent(pages.title))`,
|
||||
'like',
|
||||
sql`lower(f_unaccent(${`%${titleLikeQuery}%`}))`,
|
||||
),
|
||||
),
|
||||
)
|
||||
.$if(Boolean(searchParams.creatorId), (qb) =>
|
||||
qb.where('creatorId', '=', searchParams.creatorId),
|
||||
)
|
||||
.$if(labelIds?.length > 0, (qb) =>
|
||||
qb.where(
|
||||
'id',
|
||||
'in',
|
||||
this.db
|
||||
.selectFrom('pageLabels')
|
||||
.select('pageId')
|
||||
.where('labelId', 'in', labelIds),
|
||||
),
|
||||
)
|
||||
.where('deletedAt', 'is', null)
|
||||
.orderBy('rank', 'desc')
|
||||
.$if(browseByFilters, (qb) => qb.orderBy('updatedAt', 'desc'))
|
||||
.$if(!browseByFilters, (qb) => qb.orderBy('rank', 'desc'))
|
||||
.limit(searchParams.limit || 25)
|
||||
.offset(searchParams.offset || 0);
|
||||
|
||||
@@ -71,8 +113,7 @@ export class SearchService {
|
||||
queryResults = queryResults.select((eb) => this.pageRepo.withSpace(eb));
|
||||
}
|
||||
|
||||
if (searchParams.spaceId) {
|
||||
// search by spaceId
|
||||
if (searchParams.spaceId && opts.userId) {
|
||||
queryResults = queryResults.where('spaceId', '=', searchParams.spaceId);
|
||||
} else if (opts.userId && !searchParams.spaceId) {
|
||||
// only search spaces the user is a member of
|
||||
|
||||
Reference in New Issue
Block a user