Skip to content

Commit

Permalink
wip(sanity): groq2024 pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
juice49 committed Nov 23, 2024
1 parent 9b30735 commit b9c31c8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/sanity/src/core/search/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export interface WeightedSearchResults {
export interface Groq2024SearchResults {
type: 'groq2024'
hits: SearchHit[]
nextCursor?: never
nextCursor?: string
}

/**
Expand Down
20 changes: 16 additions & 4 deletions packages/sanity/src/core/search/groq2024/createGroq2024Search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,22 @@ export const createGroq2024Search: SearchStrategyFactory<Groq2024SearchResults>
.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,
}
}),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ 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'}])
// TODO: `_score` not required.
const projectionFields = ['_type', '_id', '_score']
const projection = projectionFields.join(', ')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -158,6 +159,7 @@ export function SearchProvider({children, fullscreen}: SearchProviderProps) {
searchState.terms,
terms,
cursor,
strategy,
])

/**
Expand Down

0 comments on commit b9c31c8

Please sign in to comment.