Skip to content

Commit

Permalink
fix(core): removes deprecated textSearch strategy (#8524)
Browse files Browse the repository at this point in the history
Co-authored-by: Ash <[email protected]>
  • Loading branch information
pedrobonamin and juice49 authored Feb 10, 2025
1 parent d1e08dd commit bcef9d7
Show file tree
Hide file tree
Showing 11 changed files with 18 additions and 745 deletions.
2 changes: 1 addition & 1 deletion packages/@sanity/types/src/search/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @public
*/
export const searchStrategies = ['groqLegacy', 'textSearch', 'groq2024'] as const
export const searchStrategies = ['groqLegacy', 'groq2024'] as const

/**
* @public
Expand Down
14 changes: 7 additions & 7 deletions packages/sanity/src/core/config/__tests__/resolveConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ describe('search strategy selection', () => {
},
})

expect(workspaceB.search.strategy).toBe('textSearch')
expect(workspaceB.search.strategy).toBe('groq2024')
})

it('gives precedence to `strategy`', async () => {
Expand All @@ -340,11 +340,11 @@ describe('search strategy selection', () => {
dataset,
search: {
enableLegacySearch: true,
strategy: 'textSearch',
strategy: 'groq2024',
},
})

expect(workspaceA.search.strategy).toBe('textSearch')
expect(workspaceA.search.strategy).toBe('groq2024')

const workspaceB = await createWorkspaceFromConfig({
projectId,
Expand Down Expand Up @@ -387,7 +387,7 @@ describe('search strategy selection', () => {
},
})

expect(workspaceB.search.strategy).toBe('textSearch')
expect(workspaceB.search.strategy).toBe('groq2024')

const workspaceC = await createWorkspaceFromConfig({
projectId,
Expand All @@ -409,7 +409,7 @@ describe('search strategy selection', () => {
dataset,
plugins: [
getSearchOptionsPlugin({
strategy: 'textSearch',
strategy: 'groq2024',
}),
],
search: {
Expand All @@ -424,15 +424,15 @@ describe('search strategy selection', () => {
dataset,
plugins: [
getSearchOptionsPlugin({
strategy: 'textSearch',
strategy: 'groq2024',
}),
],
search: {
enableLegacySearch: true,
},
})

expect(workspaceE.search.strategy).toBe('textSearch')
expect(workspaceE.search.strategy).toBe('groq2024')

const workspaceF = await createWorkspaceFromConfig({
projectId,
Expand Down
4 changes: 2 additions & 2 deletions packages/sanity/src/core/config/configPropertyReducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ export const legacySearchEnabledReducer: ConfigPropertyReducer<boolean, ConfigCo
* `enableLegacySearch` option.
*
* If the project currently enables the Text Search API search strategy by setting
* `enableLegacySearch` to `false`, this is mapped to the `textSearch` strategy.
* `enableLegacySearch` to `false`, this is mapped to the `groq2024` strategy.
*
* Any explicitly defined `strategy` value will take precedence over the value inferred from
* `enableLegacySearch`.
Expand Down Expand Up @@ -496,7 +496,7 @@ export const searchStrategyReducer = ({

// The strategy has been implicitly defined.
if (typeof enableLegacySearch === 'boolean') {
return [enableLegacySearch ? 'groqLegacy' : 'textSearch', currentExplicit]
return [enableLegacySearch ? 'groqLegacy' : 'groq2024', currentExplicit]
}

return [currentImplicit, currentExplicit]
Expand Down
1 change: 0 additions & 1 deletion packages/sanity/src/core/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,6 @@ export interface PluginOptions {
*
* - `"groqLegacy"` (default): Use client-side tokenization and schema introspection to search
* using the GROQ Query API.
* - `"textSearch"` (deprecated): Perform full text searching using the Text Search API.
* - `"groq2024"`: (experimental) Perform full text searching using the GROQ Query API and its
* new `text::matchQuery` function.
*/
Expand Down
115 changes: 1 addition & 114 deletions packages/sanity/src/core/search/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,6 @@ export interface SearchFactoryOptions {
perspective?: ClientPerspective
}

/**
* @internal
*/
export interface TextSearchResults {
type: 'text'
hits: SearchHit[]
nextCursor?: string
}

/**
* @internal
*/
Expand All @@ -103,9 +94,7 @@ export interface Groq2024SearchResults {
/**
* @internal
*/
export type SearchStrategyFactory<
TResult extends TextSearchResults | WeightedSearchResults | Groq2024SearchResults,
> = (
export type SearchStrategyFactory<TResult extends WeightedSearchResults | Groq2024SearchResults> = (
types: (SchemaType | CrossDatasetType)[],
client: SanityClient,
commonOpts: SearchFactoryOptions,
Expand Down Expand Up @@ -141,105 +130,3 @@ export type SearchSort = {
field: string
mapWith?: string
}

/**
* @internal
*/
export interface TextSearchDocumentTypeConfiguration {
weights?: Record<string, number>
}

/**
* @internal
*/
export interface TextSearchOrder {
attribute: string
direction: SortDirection
}

export type TextSearchParams = {
query: {
string: string
}
/**
* A GROQ filter expression you can use to limit the scope of the search.
* See https://www.sanity.io/docs/query-cheat-sheet for tips.
*
* Example:
*
* ```
* "_type in ['book', 'author'] && select(_type == 'book' => publishedAt != null, true)"
* ```
*/
filter?: string
/**
* Parameters for the GROQ filter expression.
*/
params?: Record<string, unknown>
/**
* A list of document paths to include in the response. If not provided, all
* attributes are returned.
*
* Example:
*
* ```
* ["title", "author.name", "skus[]._ref", "skus[]._type"]
* ```
*
*/
includeAttributes?: string[]
/**
* The number of results to return. See API documentation for default value.
*/
limit?: number
/**
* The cursor to start from. This is returned in the `nextCursor` field of the
* response, if there are more results than the `limit` parameter. Use this
* parameter to paginate, keeping the query the same, but changing the cursor.
*/
fromCursor?: string
/**
* Configuration for individual document types.
*/
types?: Record<string, TextSearchDocumentTypeConfiguration>
/**
* Result ordering.
*/
order?: TextSearchOrder[]
perspective?: ClientPerspective
}

export type TextSearchResponse<Attributes = Record<string, unknown>> = {
ms: number
stats: {
estimatedTotalCount: number
}
hits: TextSearchHit<Attributes>[]
nextCursor: string
}

export type TextSearchHit<Attributes = Record<string, unknown>> = {
/**
* The document ID
*/
id: string
rankingInfo: {
matchScore: number
}
/**
* The document attributes, limited to `includeAttributes` list if provided in
* the query.
*/
attributes: Attributes
/**
* The highlights are a map of document paths to SearchResultHighlight
* objects. This tells you which field matched the query.
*/
highlights: Record<string, TextSearchHighlight>
}

export type TextSearchHighlight = {
value: string
matchLevel: 'full' | 'partial'
matchedWords: string[]
}
13 changes: 6 additions & 7 deletions packages/sanity/src/core/search/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,27 @@ import {versionedClient} from '../studioClient'
import {
type Groq2024SearchResults,
type SearchStrategyFactory,
type TextSearchResults,
type WeightedSearchResults,
} from './common'
import {createGroq2024Search} from './groq2024'
import {createTextSearch} from './text-search'
import {createWeightedSearch} from './weighted'

const searchStrategies = {
groqLegacy: createWeightedSearch,
textSearch: createTextSearch,
groq2024: createGroq2024Search,
} satisfies Record<
SearchStrategy,
SearchStrategyFactory<TextSearchResults | WeightedSearchResults | Groq2024SearchResults>
SearchStrategyFactory<WeightedSearchResults | Groq2024SearchResults>
>

const DEFAULT_SEARCH_STRATEGY: SearchStrategy = 'groqLegacy'

/** @internal */
export const createSearch: SearchStrategyFactory<
TextSearchResults | WeightedSearchResults | Groq2024SearchResults
> = (searchableTypes, client, options) => {
export const createSearch: SearchStrategyFactory<WeightedSearchResults | Groq2024SearchResults> = (
searchableTypes,
client,
options,
) => {
const factory = searchStrategies[options.strategy ?? DEFAULT_SEARCH_STRATEGY]

return factory(
Expand Down
Loading

0 comments on commit bcef9d7

Please sign in to comment.