From a59f4bc620aaab685e72d670f35f02f2db1bd773 Mon Sep 17 00:00:00 2001 From: Ash Date: Tue, 19 Mar 2024 09:14:38 +0000 Subject: [PATCH] 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 --- .../sanity/src/core/search/common/types.ts | 9 +- .../text-search/createTextSearch.test.ts | 14 +-- .../search/text-search/createTextSearch.ts | 14 +-- .../search/contexts/search/reducer.test.ts | 105 ++++++++++++++++++ .../navbar/search/contexts/search/reducer.ts | 4 + 5 files changed, 127 insertions(+), 19 deletions(-) diff --git a/packages/sanity/src/core/search/common/types.ts b/packages/sanity/src/core/search/common/types.ts index af8501323e5..82e142bcf4e 100644 --- a/packages/sanity/src/core/search/common/types.ts +++ b/packages/sanity/src/core/search/common/types.ts @@ -133,7 +133,10 @@ export interface TextSearchDocumentTypeConfiguration { /** * @internal */ -export type TextSearchSort = Record +export interface TextSearchOrder { + attribute: string + direction: SortDirection +} export type TextSearchParams = { query: { @@ -181,9 +184,9 @@ export type TextSearchParams = { */ types?: Record /** - * Result sorting. + * Result ordering. */ - sort?: TextSearchSort[] + order?: TextSearchOrder[] } export type TextSearchResponse> = { diff --git a/packages/sanity/src/core/search/text-search/createTextSearch.test.ts b/packages/sanity/src/core/search/text-search/createTextSearch.test.ts index d93e1563f85..0c1702b52d2 100644 --- a/packages/sanity/src/core/search/text-search/createTextSearch.test.ts +++ b/packages/sanity/src/core/search/text-search/createTextSearch.test.ts @@ -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: [ @@ -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', @@ -213,14 +213,12 @@ describe('getSort', () => { ]), ).toEqual([ { - title: { - order: 'desc', - }, + attribute: 'title', + direction: 'desc', }, { - _createdAt: { - order: 'asc', - }, + attribute: '_createdAt', + direction: 'asc', }, ]) }) diff --git a/packages/sanity/src/core/search/text-search/createTextSearch.ts b/packages/sanity/src/core/search/text-search/createTextSearch.ts index f456c479b50..3f12edc6aca 100644 --- a/packages/sanity/src/core/search/text-search/createTextSearch.ts +++ b/packages/sanity/src/core/search/text-search/createTextSearch.ts @@ -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 @@ -73,12 +73,11 @@ export function getDocumentTypeConfiguration( }, {}) } -export function getSort(sort: SearchSort[] = []): TextSearchSort[] { - return sort.map( +export function getOrder(sort: SearchSort[] = []): TextSearchOrder[] { + return sort.map( ({field, direction}) => ({ - [field]: { - order: direction, - }, + attribute: field, + direction, }), {}, ) @@ -114,8 +113,7 @@ export const createTextSearch: SearchStrategyFactory = ( ...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, diff --git a/packages/sanity/src/core/studio/components/navbar/search/contexts/search/reducer.test.ts b/packages/sanity/src/core/studio/components/navbar/search/contexts/search/reducer.test.ts index 57265ecab5b..e8b29e0310f 100644 --- a/packages/sanity/src/core/studio/components/navbar/search/contexts/search/reducer.test.ts +++ b/packages/sanity/src/core/studio/components/navbar/search/contexts/search/reducer.test.ts @@ -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", + }, + }, +] +`) +}) diff --git a/packages/sanity/src/core/studio/components/navbar/search/contexts/search/reducer.ts b/packages/sanity/src/core/studio/components/navbar/search/contexts/search/reducer.ts index 00504877d94..76234ddff4f 100644 --- a/packages/sanity/src/core/studio/components/navbar/search/contexts/search/reducer.ts +++ b/packages/sanity/src/core/studio/components/navbar/search/contexts/search/reducer.ts @@ -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 {