Skip to content

Commit

Permalink
fix(base): fix regression causing drafts to be included as results in…
Browse files Browse the repository at this point in the history
… reference search (#2441)
  • Loading branch information
bjoerge committed Apr 20, 2021
1 parent f20ee86 commit 02bd577
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {joinPath} from '../../util/searchUtils'
import {removeDupes} from '../../util/draftUtils'
import {tokenize} from '../common/tokenize'
import {applyWeights} from './applyWeights'
import {WeightedHit, WeightedSearchOptions} from './types'
import {WeightedHit, WeightedSearchOptions, SearchOptions} from './types'

const combinePaths = flow([flatten, union, compact])

Expand All @@ -25,7 +25,7 @@ export function createWeightedSearch(
client: SanityClient,
options: WeightedSearchOptions = {}
): (query: string) => Observable<WeightedHit[]> {
const {filter, params, includeDrafts} = options
const {filter, params} = options
const searchSpec = types.map((type) => ({
typeName: type.name,
paths: type.__experimental_search.map((config) => ({
Expand All @@ -44,15 +44,15 @@ export function createWeightedSearch(
)

// this is the actual search function that takes the search string and returns the hits
return function search(queryString: string) {
return function search(queryString: string, searchOpts: SearchOptions = {}) {
const terms = uniq(compact(tokenize(toLower(queryString))))
const constraints = terms
.map((term, i) => combinedSearchPaths.map((joinedPath) => `${joinedPath} match $t${i}`))
.filter((constraint) => constraint.length > 0)

const filters = [
'_type in $__types',
includeDrafts === false && `!(_id in path('drafts.**'))`,
searchOpts.includeDrafts === false && `!(_id in path('drafts.**'))`,
...constraints.map((constraint) => `(${constraint.join('||')})`),
filter ? `(${filter})` : '',
].filter(Boolean)
Expand Down
3 changes: 3 additions & 0 deletions packages/@sanity/base/src/search/weighted/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,8 @@ export interface WeightedHit {
export interface WeightedSearchOptions {
filter?: string
params?: Record<string, unknown>
}

export interface SearchOptions {
includeDrafts?: boolean
}

0 comments on commit 02bd577

Please sign in to comment.