Skip to content

Commit a59f4bc

Browse files
authoredMar 19, 2024
feat(core): integrate with Text Search API ordering (#6001)
* fix(core): reset global search results after ordering changes * feat(core): finalise Text Search API ordering integration
1 parent 9ab928f commit a59f4bc

File tree

5 files changed

+127
-19
lines changed

5 files changed

+127
-19
lines changed
 

‎packages/sanity/src/core/search/common/types.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ export interface TextSearchDocumentTypeConfiguration {
133133
/**
134134
* @internal
135135
*/
136-
export type TextSearchSort = Record<string, {order: SortDirection}>
136+
export interface TextSearchOrder {
137+
attribute: string
138+
direction: SortDirection
139+
}
137140

138141
export type TextSearchParams = {
139142
query: {
@@ -181,9 +184,9 @@ export type TextSearchParams = {
181184
*/
182185
types?: Record<string, TextSearchDocumentTypeConfiguration>
183186
/**
184-
* Result sorting.
187+
* Result ordering.
185188
*/
186-
sort?: TextSearchSort[]
189+
order?: TextSearchOrder[]
187190
}
188191

189192
export type TextSearchResponse<Attributes = Record<string, unknown>> = {

‎packages/sanity/src/core/search/text-search/createTextSearch.test.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {describe, expect, it} from '@jest/globals'
22
import {Schema} from '@sanity/schema'
33
import {defineField, defineType} from '@sanity/types'
44

5-
import {getDocumentTypeConfiguration, getSort} from './createTextSearch'
5+
import {getDocumentTypeConfiguration, getOrder} from './createTextSearch'
66

77
const testType = Schema.compile({
88
types: [
@@ -201,7 +201,7 @@ describe('getDocumentTypeConfiguration', () => {
201201
describe('getSort', () => {
202202
it('transforms Studio sort options to valid Text Search sort options', () => {
203203
expect(
204-
getSort([
204+
getOrder([
205205
{
206206
field: 'title',
207207
direction: 'desc',
@@ -213,14 +213,12 @@ describe('getSort', () => {
213213
]),
214214
).toEqual([
215215
{
216-
title: {
217-
order: 'desc',
218-
},
216+
attribute: 'title',
217+
direction: 'desc',
219218
},
220219
{
221-
_createdAt: {
222-
order: 'asc',
223-
},
220+
attribute: '_createdAt',
221+
direction: 'asc',
224222
},
225223
])
226224
})

‎packages/sanity/src/core/search/text-search/createTextSearch.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import {
1111
type SearchStrategyFactory,
1212
type SearchTerms,
1313
type TextSearchDocumentTypeConfiguration,
14+
type TextSearchOrder,
1415
type TextSearchParams,
1516
type TextSearchResponse,
1617
type TextSearchResults,
17-
type TextSearchSort,
1818
} from '../common'
1919

2020
const DEFAULT_LIMIT = 1000
@@ -73,12 +73,11 @@ export function getDocumentTypeConfiguration(
7373
}, {})
7474
}
7575

76-
export function getSort(sort: SearchSort[] = []): TextSearchSort[] {
77-
return sort.map<TextSearchSort>(
76+
export function getOrder(sort: SearchSort[] = []): TextSearchOrder[] {
77+
return sort.map<TextSearchOrder>(
7878
({field, direction}) => ({
79-
[field]: {
80-
order: direction,
81-
},
79+
attribute: field,
80+
direction,
8281
}),
8382
{},
8483
)
@@ -114,8 +113,7 @@ export const createTextSearch: SearchStrategyFactory<TextSearchResults> = (
114113
...searchTerms.params,
115114
},
116115
types: getDocumentTypeConfiguration(searchOptions, searchTerms),
117-
// TODO: `sort` is not supported by the Text Search API yet.
118-
// sort: getSort(searchOptions.sort),
116+
order: getOrder(searchOptions.sort),
119117
includeAttributes: ['_id', '_type'],
120118
fromCursor: searchOptions.cursor,
121119
limit: searchOptions.limit ?? DEFAULT_LIMIT,

‎packages/sanity/src/core/studio/components/navbar/search/contexts/search/reducer.test.ts

+105
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,108 @@ Array [
285285
]
286286
`)
287287
})
288+
289+
it('should reset results after ordering changes', () => {
290+
const {result} = renderHook(() => useReducer(searchReducer, initialState))
291+
const [, dispatch] = result.current
292+
293+
act(() =>
294+
dispatch({
295+
type: 'TERMS_QUERY_SET',
296+
query: 'test query a',
297+
}),
298+
)
299+
300+
act(() =>
301+
dispatch({
302+
type: 'SEARCH_REQUEST_COMPLETE',
303+
nextCursor: 'cursorA',
304+
hits: [
305+
{
306+
hit: {
307+
_type: 'person',
308+
_id: 'personA',
309+
},
310+
},
311+
{
312+
hit: {
313+
_type: 'person',
314+
_id: 'personB',
315+
},
316+
},
317+
],
318+
}),
319+
)
320+
321+
const [stateA] = result.current
322+
323+
expect(stateA.result.hits).toMatchInlineSnapshot(`
324+
Array [
325+
Object {
326+
"hit": Object {
327+
"_id": "personA",
328+
"_type": "person",
329+
},
330+
},
331+
Object {
332+
"hit": Object {
333+
"_id": "personB",
334+
"_type": "person",
335+
},
336+
},
337+
]
338+
`)
339+
340+
act(() =>
341+
dispatch({
342+
type: 'ORDERING_SET',
343+
ordering: {
344+
titleKey: 'search.ordering.test-label',
345+
sort: {
346+
field: '_createdAt',
347+
direction: 'desc',
348+
},
349+
},
350+
}),
351+
)
352+
353+
act(() =>
354+
dispatch({
355+
type: 'SEARCH_REQUEST_COMPLETE',
356+
nextCursor: undefined,
357+
hits: [
358+
{
359+
hit: {
360+
_type: 'person',
361+
_id: 'personB',
362+
},
363+
},
364+
{
365+
hit: {
366+
_type: 'person',
367+
_id: 'personC',
368+
},
369+
},
370+
],
371+
}),
372+
)
373+
374+
const [stateB] = result.current
375+
376+
expect(stateB.result.hits).toMatchInlineSnapshot(`
377+
Array [
378+
Object {
379+
"hit": Object {
380+
"_id": "personB",
381+
"_type": "person",
382+
},
383+
},
384+
Object {
385+
"hit": Object {
386+
"_id": "personC",
387+
"_type": "person",
388+
},
389+
},
390+
]
391+
`)
392+
})

‎packages/sanity/src/core/studio/components/navbar/search/contexts/search/reducer.ts

+4
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ export function searchReducer(state: SearchReducerState, action: SearchAction):
181181
...state,
182182
ordering: action.ordering,
183183
terms: stripRecent(state.terms),
184+
result: {
185+
...state.result,
186+
hasLocal: false,
187+
},
184188
}
185189
case 'PAGE_INCREMENT':
186190
return {

0 commit comments

Comments
 (0)
Please sign in to comment.