Skip to content

Commit

Permalink
fixup! docs(core): update docs for applyWeights/createSearchQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
robinpyon committed Jul 18, 2023
1 parent 1004aa7 commit c0aa906
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
14 changes: 7 additions & 7 deletions packages/@sanity/base/src/search/weighted/applyWeights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ type SearchScore = [number, string]
* Calculates a score (between 0 and 1) indicating general search relevance of an array of
* search tokens within a specific string.
*
* @internal
* @param searchTerms - All search terms
* @param value - The string to match against
* @returns A [score, story] pair containing the search score as well as a human readable explanation
* @internal
*/
export const calculateScore = (searchTerms: string[], value: string): SearchScore => {
// Separate search terms by phrases (wrapped with quotes) and words.
Expand All @@ -31,11 +31,11 @@ const stringify = (value: unknown): string =>
* Applies path weights from a supplied SearchSpec to existing search hits to create _weighted_ hits
* augmented with search ranking and human readable explanations.
*
* @internal
* @param searchSpec - SearchSpec containing path weighting
* @param hits - SearchHit objects to augment
* @param terms - All search terms
* @returns WeightedHit array containing search scores and ranking explanations
* @internal
*/
export function applyWeights(
searchSpec: SearchSpec[],
Expand Down Expand Up @@ -91,10 +91,10 @@ export function applyWeights(
* - "of london" is included in the target value, and 9 out of 22 characters match (score: 9/22 = ~0.408)
* - non-exact matches have their score divided in half (final score: ~0.204)
*
* @internal
* @param uniqueSearchPhrases - All search phrases
* @param value - The string to match against
* @returns SearchScore containing the search score as well as a human readable explanation
* @internal
*/
export function calculatePhraseScore(uniqueSearchPhrases: string[], value: string): SearchScore {
const sanitizedValue = value.toLowerCase().trim()
Expand All @@ -121,10 +121,10 @@ export function calculatePhraseScore(uniqueSearchPhrases: string[], value: strin
* - "bar" is included in the target value, and 3 out of 7 non-whitespace characters match (score: 3/7)
* - all values are accumulated and have their score devidied by half (final score: ~0.357)
*
* @internal
* @param uniqueSearchTerms - A string array of search terms
* @param value - The string to match against
* @returns SearchScore containing the search score as well as a human readable explanation
* @internal
*/
export function calculateCharacterScore(uniqueSearchTerms: string[], value: string): SearchScore {
const sanitizedValue = value.toLowerCase().trim()
Expand All @@ -151,10 +151,10 @@ export function calculateCharacterScore(uniqueSearchTerms: string[], value: stri
* - 4 out of 5 words match (score: 4/5 = 0.8)
* - non-exact matches have their score divided in half (final score: 0.4)
*
* @internal
* @param uniqueSearchTerms - All search terms
* @param value - The string to match against
* @returns SearchScore containing the search score as well as a human readable explanation
* @internal
*/
export function calculateWordScore(uniqueSearchTerms: string[], value: string): SearchScore {
const uniqueValueTerms = uniq(compact(words(toLower(value))))
Expand All @@ -170,9 +170,9 @@ export function calculateWordScore(uniqueSearchTerms: string[], value: string):
/**
* Partition search terms by phrases (wrapped with quotes) and words.
*
* @internal
* @param searchTerms - All search terms
* @returns Partitioned phrases and words
* @internal
*/
export function partitionAndSanitizeSearchTerms(
searchTerms: string[]
Expand All @@ -193,10 +193,10 @@ export function partitionAndSanitizeSearchTerms(
* Returns matching array indices of `values` containing _any_ member of `uniqueSearchTerms`.
* When comparing for matches, members of `values` are stringified, trimmed and lowercased.
*
* @internal
* @param uniqueSearchTerms - All search terms
* @param values - Values to match against (members are stringified)
* @returns All matching indices in `values`
* @internal
*/
export function findMatchingIndices(uniqueSearchTerms: string[], values: unknown[]): number[] {
const {phrases: uniqueSearchPhrases, words: uniqueSearchWords} = partitionAndSanitizeSearchTerms(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ const combinePaths = flow([flatten, union, compact])
* Create search specs from supplied searchable types.
* Search specs contain weighted paths which are used to construct GROQ queries for search.
*
* @internal
* @param types - Searchable document types to create specs from.
* @param optimizedIndexPaths - If true, will will convert all `__experimental_search` paths containing numbers into array syntax.
* E.g. ['cover', 0, 'cards', 0, 'title'] => "cover[].cards[].title"
Expand All @@ -57,6 +56,7 @@ const combinePaths = flow([flatten, union, compact])
* @param maxAttributes - Maximum number of _unique_ searchable attributes to include across all types.
* User-provided paths (e.g. with __experimental_search) do not count towards this limit.
* @returns All matching search specs and `hasIndexedPaths`, a boolean indicating whether any paths contain indices.
* @internal
*/
export function createSearchSpecs(
types: SearchableType[],
Expand Down Expand Up @@ -162,9 +162,9 @@ function createConstraints(terms: string[], specs: SearchSpec[]) {
*
* Phrases wrapped in quotes are assigned relevance scoring differently from regular words.
*
* @internal
* @param query - A string to convert into individual tokens
* @returns All extracted tokens
* @internal
*/
export function extractTermsFromQuery(query: string): string[] {
const quotedQueries = [] as string[]
Expand Down Expand Up @@ -192,10 +192,10 @@ export function extractTermsFromQuery(query: string): string[] {
/**
* Generate search query data based off provided search terms and options.
*
* @internal
* @param searchTerms - SearchTerms containing a string query and any number of searchable document types.
* @param searchOpts - Optional search configuration.
* @returns GROQ query, params and options to be used to fetch search results.
* @internal
*/
export function createSearchQuery(
searchTerms: SearchTerms,
Expand Down

0 comments on commit c0aa906

Please sign in to comment.