Skip to content

Commit

Permalink
feat(core): integrate with Text Search API ordering (#6001)
Browse files Browse the repository at this point in the history
* fix(core): reset global search results after ordering changes

* feat(core): finalise Text Search API ordering integration
  • Loading branch information
juice49 committed Mar 19, 2024
1 parent 9ab928f commit a59f4bc
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 19 deletions.
9 changes: 6 additions & 3 deletions packages/sanity/src/core/search/common/types.ts
Expand Up @@ -133,7 +133,10 @@ export interface TextSearchDocumentTypeConfiguration {
/**
* @internal
*/
export type TextSearchSort = Record<string, {order: SortDirection}>
export interface TextSearchOrder {
attribute: string
direction: SortDirection
}

export type TextSearchParams = {
query: {
Expand Down Expand Up @@ -181,9 +184,9 @@ export type TextSearchParams = {
*/
types?: Record<string, TextSearchDocumentTypeConfiguration>
/**
* Result sorting.
* Result ordering.
*/
sort?: TextSearchSort[]
order?: TextSearchOrder[]
}

export type TextSearchResponse<Attributes = Record<string, unknown>> = {
Expand Down
Expand Up @@ -2,7 +2,7 @@ import {describe, expect, it} from '@jest/globals'
import {Schema} from '@sanity/schema'
import {defineField, defineType} from '@sanity/types'

import {getDocumentTypeConfiguration, getSort} from './createTextSearch'
import {getDocumentTypeConfiguration, getOrder} from './createTextSearch'

const testType = Schema.compile({
types: [
Expand Down Expand Up @@ -201,7 +201,7 @@ describe('getDocumentTypeConfiguration', () => {
describe('getSort', () => {
it('transforms Studio sort options to valid Text Search sort options', () => {
expect(
getSort([
getOrder([
{
field: 'title',
direction: 'desc',
Expand All @@ -213,14 +213,12 @@ describe('getSort', () => {
]),
).toEqual([
{
title: {
order: 'desc',
},
attribute: 'title',
direction: 'desc',
},
{
_createdAt: {
order: 'asc',
},
attribute: '_createdAt',
direction: 'asc',
},
])
})
Expand Down
14 changes: 6 additions & 8 deletions packages/sanity/src/core/search/text-search/createTextSearch.ts
Expand Up @@ -11,10 +11,10 @@ import {
type SearchStrategyFactory,
type SearchTerms,
type TextSearchDocumentTypeConfiguration,
type TextSearchOrder,
type TextSearchParams,
type TextSearchResponse,
type TextSearchResults,
type TextSearchSort,
} from '../common'

const DEFAULT_LIMIT = 1000
Expand Down Expand Up @@ -73,12 +73,11 @@ export function getDocumentTypeConfiguration(
}, {})
}

export function getSort(sort: SearchSort[] = []): TextSearchSort[] {
return sort.map<TextSearchSort>(
export function getOrder(sort: SearchSort[] = []): TextSearchOrder[] {
return sort.map<TextSearchOrder>(
({field, direction}) => ({
[field]: {
order: direction,
},
attribute: field,
direction,
}),
{},
)
Expand Down Expand Up @@ -114,8 +113,7 @@ export const createTextSearch: SearchStrategyFactory<TextSearchResults> = (
...searchTerms.params,
},
types: getDocumentTypeConfiguration(searchOptions, searchTerms),
// TODO: `sort` is not supported by the Text Search API yet.
// sort: getSort(searchOptions.sort),
order: getOrder(searchOptions.sort),
includeAttributes: ['_id', '_type'],
fromCursor: searchOptions.cursor,
limit: searchOptions.limit ?? DEFAULT_LIMIT,
Expand Down
Expand Up @@ -285,3 +285,108 @@ Array [
]
`)
})

it('should reset results after ordering changes', () => {
const {result} = renderHook(() => useReducer(searchReducer, initialState))
const [, dispatch] = result.current

act(() =>
dispatch({
type: 'TERMS_QUERY_SET',
query: 'test query a',
}),
)

act(() =>
dispatch({
type: 'SEARCH_REQUEST_COMPLETE',
nextCursor: 'cursorA',
hits: [
{
hit: {
_type: 'person',
_id: 'personA',
},
},
{
hit: {
_type: 'person',
_id: 'personB',
},
},
],
}),
)

const [stateA] = result.current

expect(stateA.result.hits).toMatchInlineSnapshot(`
Array [
Object {
"hit": Object {
"_id": "personA",
"_type": "person",
},
},
Object {
"hit": Object {
"_id": "personB",
"_type": "person",
},
},
]
`)

act(() =>
dispatch({
type: 'ORDERING_SET',
ordering: {
titleKey: 'search.ordering.test-label',
sort: {
field: '_createdAt',
direction: 'desc',
},
},
}),
)

act(() =>
dispatch({
type: 'SEARCH_REQUEST_COMPLETE',
nextCursor: undefined,
hits: [
{
hit: {
_type: 'person',
_id: 'personB',
},
},
{
hit: {
_type: 'person',
_id: 'personC',
},
},
],
}),
)

const [stateB] = result.current

expect(stateB.result.hits).toMatchInlineSnapshot(`
Array [
Object {
"hit": Object {
"_id": "personB",
"_type": "person",
},
},
Object {
"hit": Object {
"_id": "personC",
"_type": "person",
},
},
]
`)
})
Expand Up @@ -181,6 +181,10 @@ export function searchReducer(state: SearchReducerState, action: SearchAction):
...state,
ordering: action.ordering,
terms: stripRecent(state.terms),
result: {
...state.result,
hasLocal: false,
},
}
case 'PAGE_INCREMENT':
return {
Expand Down

0 comments on commit a59f4bc

Please sign in to comment.