Zoonk.FuzzySearch (Zoonk v0.1.0-dev)

View Source

Provides fuzzy search functionality using Jaro distance algorithm.

This module allows searching through collections of items with approximate string matching, useful for implementing search features with typo tolerance.

Summary

Functions

Performs a fuzzy search on a list of items with a simple two-step approach.

Functions

search(items, query, match_fn \\ & &1, threshold \\ 0.75)

Performs a fuzzy search on a list of items with a simple two-step approach.

The search works in two phases:

  1. First, attempts exact substring matching (case-insensitive)
  2. If no exact matches are found, falls back to fuzzy matching using Jaro distance

This approach prioritizes precision - exact matches are always preferred over fuzzy matches, eliminating false positives while still providing typo tolerance when needed.

The fuzzy matching algorithm checks both the full text and individual words within the text, making it effective at handling typos in multi-word scenarios.

Parameters

  • items - List of items to search through
  • query - The search query string
  • match_fn - Function that returns the text to match against for each item (optional, defaults to identity function)
  • threshold - Minimum Jaro distance score required for fuzzy matches (default: 0.75, lowered to 0.6 for better typo detection)

Examples

# Exact substring matching (case-insensitive)
iex> FuzzySearch.search(["Settings", "Profile", "Help"], "sett")
["Settings"]

# Fuzzy matching kicks in only when no exact matches exist
iex> FuzzySearch.search(["profile", "project"], "profle")
["profile"]

# Fuzzy matching works with multi-word text by checking individual words
iex> FuzzySearch.search(["User Profile", "Profile Settings"], "profle")
["User Profile", "Profile Settings"]

# Search through complex data structures
iex> FuzzySearch.search([%{name: "Getting Started"}, %{name: "Profile"}], "start", & &1.name)
[%{name: "Getting Started"}]

# Multiple exact matches are returned
iex> FuzzySearch.search(["Settings", "Set Password"], "set")
["Settings", "Set Password"]