fuzzy-search-lib
v1.0.0
Published
A flexible fuzzy search library supporting both MongoDB and PostgreSQL
Downloads
10
Maintainers
Readme
Fuzzy Search Library
A flexible fuzzy search library supporting both MongoDB and PostgreSQL databases with multiple algorithms.
Features
- 🔍 Multiple Algorithms: Levenshtein, Jaro-Winkler, and Sørensen-Dice coefficient
- 🔄 Cross-Database Support: Works with both MongoDB and PostgreSQL
- ⚙️ Configurable: Customizable thresholds, limits, and sorting options
- 📦 TypeScript Support: Full TypeScript definitions included
- 🚀 Easy Integration: Simple API with adapter pattern
- 📊 Performance Optimized: Caching and query optimization
Installation
npm install fuzzy-search-libQuick Start
With MongoDB
import { FuzzySearch } from 'fuzzy-search-lib';
const searcher = new FuzzySearch({
database: 'mongodb',
connectionString: 'mongodb://localhost:27017/mydb',
collection: 'users'
});
await searcher.connect();
const results = await searcher.search('john', {
fields: ['name', 'email'],
threshold: 0.6,
limit: 10
});
await searcher.disconnect();With PostgreSQL
import { FuzzySearch } from 'fuzzy-search-lib';
const searcher = new FuzzySearch({
database: 'postgresql',
connectionString: 'postgresql://user:pass@localhost:5432/mydb',
table: 'users'
});
await searcher.connect();
const results = await searcher.search('john', {
fields: ['first_name', 'last_name'],
threshold: 0.7
});
await searcher.disconnect();API
FuzzySearch Constructor
new FuzzySearch(config: FuzzySearchConfig)FuzzySearchConfig Properties:
database: 'mongodb' | 'postgresql'connectionString: Database connection stringcollection: Collection name (required for MongoDB)table: Table name (required for PostgreSQL)
Search Method
search(query: string, options: SearchOptions): Promise<SearchResult<T>>SearchOptions Properties:
fields: Array of field names to search inthreshold: Minimum similarity score (0-1, default: 0.5)limit: Maximum number of results (default: 10)offset: Number of results to skip (default: 0)algorithm: Algorithm to use ('levenshtein' | 'jaro-winkler' | 'dice-coefficient', default: 'levenshtein')
Algorithms
Levenshtein Distance
Measures the minimum number of single-character edits required to change one word into another.
Jaro-Winkler Distance
Designed for short strings like person names, gives more favorable ratings to strings that match from the beginning.
Sørensen-Dice Coefficient
Measures the similarity between two strings based on the number of common bigrams.
License
MIT
