Skip to content

Commit

Permalink
fix: forcing all search strategies to use vX API whilst releases not …
Browse files Browse the repository at this point in the history
…supported in named versions (#8283)
  • Loading branch information
jordanl17 authored Jan 16, 2025
1 parent d52c0ac commit cc1dc74
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ import {createWeightedSearch} from './createWeightedSearch'

// Mock client
vi.mock('../../hooks', () => ({
useClient: () => ({observable: {fetch: vi.fn()}}),
useClient: () => ({
observable: {
/** @todo revert to this once releases is stable in
* non-experimental content lake API
*/
//fetch: vi.fn()
withConfig: vi.fn().mockReturnValue({fetch: vi.fn()}),
},
}),
}))

const mockSchema = Schema.compile({
Expand All @@ -32,11 +40,17 @@ const {
const search = createWeightedSearch(getSearchableTypes(mockSchema), client, {unique: true})

beforeEach(() => {
;(client.observable.fetch as Mock).mockReset()
;(client.observable.fetch as Mock).mockReturnValue(searchHits)
;(client.observable.withConfig as Mock).mockReset()
;(client.observable.withConfig as Mock).mockReturnValue({fetch: () => searchHits})
})

describe('createWeightedSearch', () => {
it('overrides to use vX api version', async () => {
await lastValueFrom(search({query: 'harry', types: []} as SearchTerms))

expect(client.observable.withConfig).toHaveBeenCalledWith({apiVersion: 'vX'})
})

it('should order hits by score by default', async () => {
const result = await lastValueFrom(search({query: 'harry', types: []} as SearchTerms))

Expand Down
28 changes: 18 additions & 10 deletions packages/sanity/src/core/search/weighted/createWeightedSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,23 @@ export const createWeightedSearch: SearchStrategyFactory<WeightedSearchResults>
...searchOptions,
})

return client.observable.fetch<SanityDocumentLike[]>(query, params, options).pipe(
factoryOptions.unique ? map(removeDupes) : tap(),
// Assign weighting and scores based on current search terms.
// No scores will be assigned when terms are empty.
map((hits) => applyWeights(searchSpec, hits, terms)),
// Optionally skip client-side score sorting.
// This can be relevant when ordering results by specific fields, especially dates.
searchOptions?.skipSortByScore ? tap() : map((hits) => sortBy(hits, (hit) => -hit.score)),
map((hits) => ({type: 'weighted', hits})),
)
return client.observable
.withConfig({
/** @todo remove defined apiVersion once perspective is supported in
* non-experimental content lake API
*/
apiVersion: 'vX',
})
.fetch<SanityDocumentLike[]>(query, params, options)
.pipe(
factoryOptions.unique ? map(removeDupes) : tap(),
// Assign weighting and scores based on current search terms.
// No scores will be assigned when terms are empty.
map((hits) => applyWeights(searchSpec, hits, terms)),
// Optionally skip client-side score sorting.
// This can be relevant when ordering results by specific fields, especially dates.
searchOptions?.skipSortByScore ? tap() : map((hits) => sortBy(hits, (hit) => -hit.score)),
map((hits) => ({type: 'weighted', hits})),
)
}
}

0 comments on commit cc1dc74

Please sign in to comment.