Skip to content

Commit 3c458c8

Browse files
juice49ricokahler
andauthoredMar 12, 2024
feat(core): add new search config API (#5948)
* feat(core): implement new search configuration --- Co-authored-by: Ash <ash@sanity.io> * chore(core): deprecate `__experimental_search` --- Co-authored-by: Ash <ash@sanity.io> * chore(test-studio): update author w/ new search config --- Co-authored-by: Ash <ash@sanity.io> * test(core): add `createTextSearch` tests --------- Co-authored-by: Rico Kahler <ricokahler@gmail.com>
1 parent 193b4b8 commit 3c458c8

40 files changed

+1155
-424
lines changed
 

‎dev/test-studio/schema/author.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
import {UserIcon as icon} from '@sanity/icons'
2-
import {type Rule} from 'sanity'
2+
import {defineField, defineType, type Rule} from 'sanity'
33

44
const AUTHOR_ROLES = [
55
{value: 'developer', title: 'Developer'},
66
{value: 'designer', title: 'Designer'},
77
{value: 'ops', title: 'Operations'},
88
]
99

10-
export default {
10+
export default defineType({
1111
name: 'author',
1212
type: 'document',
1313
title: 'Author',
1414
icon,
1515
description: 'This represents an author',
16-
// eslint-disable-next-line camelcase
17-
__experimental_search: [{path: 'name', weight: 10}],
1816
preview: {
1917
select: {
2018
title: 'name',
@@ -36,12 +34,15 @@ export default {
3634
},
3735
},
3836
fields: [
39-
{
37+
defineField({
4038
name: 'name',
4139
title: 'Name',
4240
type: 'string',
41+
options: {
42+
search: {weight: 100},
43+
},
4344
validation: (rule: Rule) => rule.required(),
44-
},
45+
}),
4546
{
4647
name: 'bestFriend',
4748
title: 'Best friend',
@@ -125,4 +126,4 @@ export default {
125126
},
126127
},
127128
}),
128-
}
129+
})

‎packages/@sanity/types/src/crossDatasetReference/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export interface CrossDatasetType {
4040
title?: string
4141
icon: ComponentType
4242
preview: PreviewConfig
43-
/** @alpha */
43+
/** @deprecated Unused. Configuring search is no longer supported for cross-dataset references. */
4444
__experimental_search: ObjectSchemaType['__experimental_search']
4545
}
4646

‎packages/@sanity/types/src/schema/definition/type/array.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import {
66
type IntrinsicTypeName,
77
type TypeAliasDefinition,
88
} from '../schemaDefinition'
9-
import {type BaseSchemaDefinition, type TitledListValue} from './common'
9+
import {type BaseSchemaDefinition, type SearchConfiguration, type TitledListValue} from './common'
1010

1111
/** @public */
12-
export interface ArrayOptions<V = unknown> {
12+
export interface ArrayOptions<V = unknown> extends SearchConfiguration {
1313
list?: TitledListValue<V>[] | V[]
1414
/**
1515
* layout: 'tags' only works for string array

‎packages/@sanity/types/src/schema/definition/type/common.ts

+21
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,24 @@ export interface EnumListProps<V = unknown> {
6969
layout?: 'radio' | 'dropdown'
7070
direction?: 'horizontal' | 'vertical'
7171
}
72+
73+
/** @public */
74+
export interface SearchConfiguration {
75+
search?: {
76+
/**
77+
* Defines a search weight for this field to prioritize its importance
78+
* during search operations in the Studio. This setting allows the specified
79+
* field to be ranked higher in search results compared to other fields.
80+
*
81+
* By default, all fields are assigned a weight of 1. However, if a field is
82+
* chosen as the `title` in the preview configuration's `select` option, it
83+
* will automatically receive a default weight of 10. Similarly, if selected
84+
* as the `subtitle`, the default weight is 5. Fields marked as
85+
* `hidden: true` (no function) are assigned a weight of 0 by default.
86+
*
87+
* Note: Search weight configuration is currently supported only for fields
88+
* of type string or portable text arrays.
89+
*/
90+
weight?: number
91+
}
92+
}

‎packages/@sanity/types/src/schema/definition/type/crossDatasetReference.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface CrossDatasetReferenceDefinition extends BaseSchemaDefinition {
1515
preview?: PreviewConfig
1616

1717
/**
18-
* @deprecated Configuring search is no longer supported
18+
* @deprecated Unused. Configuring search is no longer supported.
1919
*/
2020
__experimental_search?: {path: string | string[]; weight?: number; mapWith?: string}[]
2121
}[]

‎packages/@sanity/types/src/schema/definition/type/document.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export interface DocumentDefinition extends Omit<ObjectDefinition, 'type'> {
2424
options?: DocumentOptions
2525
validation?: ValidationBuilder<DocumentRule, SanityDocument>
2626
initialValue?: InitialValueProperty<any, Record<string, unknown>>
27-
/** @alpha */
27+
/** @deprecated Unused. Use the new field-level search config. */
2828
__experimental_search?: {path: string; weight: number; mapWith?: string}[]
2929
/** @alpha */
3030
__experimental_omnisearch_visibility?: boolean

‎packages/@sanity/types/src/schema/definition/type/string.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {type FieldReference} from '../../../validation'
22
import {type RuleDef, type ValidationBuilder} from '../../ruleBuilder'
33
import {type InitialValueProperty} from '../../types'
4-
import {type BaseSchemaDefinition, type EnumListProps} from './common'
4+
import {type BaseSchemaDefinition, type EnumListProps, type SearchConfiguration} from './common'
55

66
/** @public */
77
// eslint-disable-next-line @typescript-eslint/no-empty-interface
8-
export interface StringOptions extends EnumListProps<string> {}
8+
export interface StringOptions extends EnumListProps<string>, SearchConfiguration {}
99

1010
/** @public */
1111
export interface StringRule extends RuleDef<StringRule, string> {

‎packages/@sanity/types/src/schema/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ export interface ObjectSchemaType extends BaseSchemaType {
391391
// Note: `path` is a string in the _specification_, but converted to a
392392
// string/number array in the schema normalization/compilation step
393393
// a path segment is a number when specified like array.0.prop in preview config.
394-
/** @alpha */
394+
/** @deprecated Unused. Use the new field-level search config. */
395395
__experimental_search: {path: (string | number)[]; weight: number; mapWith?: string}[]
396396
/** @alpha */
397397
__experimental_omnisearch_visibility?: boolean
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export {createSearch, getSearchableTypes, getSearchTypesWithMaxDepth} from '../core/search'
1+
export {createSearch, getSearchableTypes} from '../core/search'
22
export {useSearchMaxFieldDepth} from '../core/studio/components/navbar/search/hooks/useSearchMaxFieldDepth'

‎packages/sanity/src/core/form/studio/inputs/client-adapters/reference.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import {type SanityClient} from '@sanity/client'
2+
import {DEFAULT_MAX_FIELD_DEPTH} from '@sanity/schema/_internal'
23
import {type ReferenceFilterSearchOptions, type ReferenceSchemaType} from '@sanity/types'
34
import {combineLatest, type Observable, of} from 'rxjs'
45
import {map, mergeMap, startWith, switchMap} from 'rxjs/operators'
56

67
import {type DocumentPreviewStore, getPreviewPaths, prepareForPreview} from '../../../../preview'
7-
import {createSearch, getSearchTypesWithMaxDepth} from '../../../../search'
8+
import {createSearch} from '../../../../search'
89
import {collate, type CollatedHit, getDraftId, getIdPair, isRecord} from '../../../../util'
910
import {type ReferenceInfo, type ReferenceSearchHit} from '../../../inputs/ReferenceInput/types'
1011

@@ -193,9 +194,10 @@ export function referenceSearch(
193194
options: ReferenceFilterSearchOptions,
194195
unstable_enableNewSearch: boolean,
195196
): Observable<ReferenceSearchHit[]> {
196-
const search = createSearch(getSearchTypesWithMaxDepth(type.to, options.maxFieldDepth), client, {
197+
const search = createSearch(type.to, client, {
197198
...options,
198199
unstable_enableNewSearch,
200+
maxDepth: options.maxFieldDepth || DEFAULT_MAX_FIELD_DEPTH,
199201
})
200202
return search(textTerm, {includeDrafts: true}).pipe(
201203
map(({hits}) => hits.map(({hit}) => hit)),

‎packages/sanity/src/core/form/studio/inputs/crossDatasetReference/datastores/search.ts

+8-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {type SanityClient} from '@sanity/client'
2-
import {resolveSearchConfigForBaseFieldPaths} from '@sanity/schema/_internal'
32
import {
43
type CrossDatasetReferenceSchemaType,
54
type ReferenceFilterSearchOptions,
@@ -22,20 +21,15 @@ export function search(
2221
type: CrossDatasetReferenceSchemaType,
2322
options: ReferenceFilterSearchOptions,
2423
): Observable<SearchHit[]> {
25-
const searchWeighted = createSearch(
26-
type.to.map((crossDatasetType) => ({
27-
name: crossDatasetType.type,
28-
// eslint-disable-next-line camelcase
29-
__experimental_search: resolveSearchConfigForBaseFieldPaths(
30-
crossDatasetType,
31-
options.maxFieldDepth,
32-
),
33-
})),
34-
client,
35-
options,
36-
)
24+
const searchStrategy = createSearch(type.to, client, {
25+
...options,
26+
maxDepth: options.maxFieldDepth,
27+
})
3728

38-
return searchWeighted(textTerm, {includeDrafts: false}).pipe(
29+
return searchStrategy(textTerm, {
30+
includeDrafts: false,
31+
isCrossDataset: true,
32+
}).pipe(
3933
map(({hits}) => hits.map(({hit}) => hit)),
4034
map(collate),
4135
map((collated) =>

‎packages/sanity/src/core/index.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,7 @@ export * from './i18n'
1212
export * from './presence'
1313
export * from './preview'
1414
export * from './schema'
15-
export type {
16-
SearchableType,
17-
SearchFactoryOptions,
18-
SearchOptions,
19-
SearchSort,
20-
SearchTerms,
21-
} from './search'
15+
export type {SearchFactoryOptions, SearchOptions, SearchSort, SearchTerms} from './search'
2216
export * from './store'
2317
export * from './studio'
2418
export * from './studioClient'

0 commit comments

Comments
 (0)
Please sign in to comment.