Skip to content

Discovery Functions

Functions for relevance-ranked discovery over collections.

Summary

Function Signature Description
fuzzy_match string, string -> object Check if a single string value matches a query, returning match details
fuzzy_score string, string -> number Get the numeric match score between a value and query (higher is better match)
fuzzy_search array, string\|object, string -> array Search an array of objects by multiple fields, returning matches sorted by relevance score

Functions

fuzzy_match

Check if a single string value matches a query, returning match details

Signature: string, string -> object

Examples:

# Exact match
fuzzy_match('get_user', 'get_user') -> {matches: true, score: 1000, match_type: 'exact'}
# Prefix match
fuzzy_match('get_user_info', 'get') -> {matches: true, score: 800, match_type: 'prefix'}
# Contains match
fuzzy_match('hello_world', 'world') -> {matches: true, score: 600, match_type: 'contains'}
# No match with similarity
fuzzy_match('hello', 'xyz') -> {matches: false, score: 0, match_type: 'none', similarity: 0.3}

fuzzy_score

Get the numeric match score between a value and query (higher is better match)

Signature: string, string -> number

Examples:

# Exact match score (highest)
fuzzy_score('hello', 'hello') -> 1000
# Prefix match score
fuzzy_score('hello_world', 'hello') -> 800
# Contains match score
fuzzy_score('say_hello', 'hello') -> 600
# No match score
fuzzy_score('hello', 'goodbye') -> 0

Search an array of objects by multiple fields, returning matches sorted by relevance score

Signature: array, string|object, string -> array

Examples:

# Search with comma-separated fields
fuzzy_search(tools, 'name,description', 'user') -> [{item: {...}, score: 100, match_type: 'exact', matched_field: 'name'}, ...]
# Search with weighted fields
fuzzy_search(tools, `{"name": 10, "description": 5}`, 'cache') -> weighted search
# Access top match
fuzzy_search(tools, 'name,tags', 'redis')[0].item.name -> top match name