From 0b1cf15ef2dde8877b289a956e2677ac56f0a426 Mon Sep 17 00:00:00 2001 From: Ash Date: Sat, 23 Nov 2024 22:34:34 +0000 Subject: [PATCH] wip(sanity): groq2024 pagination --- .../sanity/src/core/search/common/types.ts | 2 +- .../search/groq2024/createGroq2024Search.ts | 20 +++++++++++++++---- .../core/search/groq2024/createSearchQuery.ts | 1 + .../search/contexts/search/SearchProvider.tsx | 4 +++- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/packages/sanity/src/core/search/common/types.ts b/packages/sanity/src/core/search/common/types.ts index 6bf04c794a7..c5ec8a3f44c 100644 --- a/packages/sanity/src/core/search/common/types.ts +++ b/packages/sanity/src/core/search/common/types.ts @@ -96,7 +96,7 @@ export interface WeightedSearchResults { export interface Groq2024SearchResults { type: 'groq2024' hits: SearchHit[] - nextCursor?: never + nextCursor?: string } /** diff --git a/packages/sanity/src/core/search/groq2024/createGroq2024Search.ts b/packages/sanity/src/core/search/groq2024/createGroq2024Search.ts index f30c8b40cb9..2d2aae3f22f 100644 --- a/packages/sanity/src/core/search/groq2024/createGroq2024Search.ts +++ b/packages/sanity/src/core/search/groq2024/createGroq2024Search.ts @@ -47,10 +47,22 @@ export const createGroq2024Search: SearchStrategyFactory .pipe( // TODO: Can perspectives help here? factoryOptions.unique ? map(removeDupes) : tap(), - map((hits) => ({ - type: 'groq2024', - hits: hits.map((hit) => ({hit})), - })), + map((hits) => { + const lastId = hits.at(-1)?._id + const lastScore = hits.at(-1)?._score + + // TODO: This cursor must change if not sorting by `_score`. + const nextCursor = + hits.length === 0 + ? undefined + : `(_score < ${lastScore} || (_score == ${lastScore} && _id > "${lastId}"))` + + return { + type: 'groq2024', + hits: hits.map((hit) => ({hit})), + nextCursor, + } + }), ) } } diff --git a/packages/sanity/src/core/search/groq2024/createSearchQuery.ts b/packages/sanity/src/core/search/groq2024/createSearchQuery.ts index 591f582da1e..1b7c8c914b7 100644 --- a/packages/sanity/src/core/search/groq2024/createSearchQuery.ts +++ b/packages/sanity/src/core/search/groq2024/createSearchQuery.ts @@ -91,6 +91,7 @@ export function createSearchQuery( options.filter ? `(${options.filter})` : false, searchTerms.filter ? `(${searchTerms.filter})` : false, '!(_id in path("versions.**"))', + options.cursor, ].filter((baseFilter) => typeof baseFilter === 'string') const sortOrder = toOrderClause(options?.sort ?? [{field: '_score', direction: 'desc'}]) diff --git a/packages/sanity/src/core/studio/components/navbar/search/contexts/search/SearchProvider.tsx b/packages/sanity/src/core/studio/components/navbar/search/contexts/search/SearchProvider.tsx index e0bc413acb8..244f23963c1 100644 --- a/packages/sanity/src/core/studio/components/navbar/search/contexts/search/SearchProvider.tsx +++ b/packages/sanity/src/core/studio/components/navbar/search/contexts/search/SearchProvider.tsx @@ -129,7 +129,8 @@ export function SearchProvider({children, fullscreen}: SearchProviderProps) { `findability-source: global`, `findability-filter-count:${completeFilters.length}`, ], - limit: SEARCH_LIMIT, + // `groq2024` supports pagination. Therefore, fetch fewer results. + limit: strategy === 'groq2024' ? 200 : SEARCH_LIMIT, skipSortByScore: ordering.ignoreScore, ...(ordering.sort ? {sort: [ordering.sort]} : {}), cursor: cursor || undefined, @@ -158,6 +159,7 @@ export function SearchProvider({children, fullscreen}: SearchProviderProps) { searchState.terms, terms, cursor, + strategy, ]) /**