firestore-search-helper
v1.0.1
Published
[](https://www.npmjs.com/package/firestore-search-helper) 
Readme
firestore-search-helper
A lightweight and simple full text search for firestore.
Installation
You can install the package using npm or yarn:
npm install firestore-search-helperyarn add firestore-search-helperGetting Started
The core workflow:
- Make a field searchable: Make a string field "searchable" using the
makeSearchableutility. - Query by a searchable field: Use the
searchByfunction to perform a fuzzy search on the field.
Here is a quick example:
import { makeSearchable } from 'firestore-search-helper';
addDoc(db, "users",
{
name: 'John Doe',
email: '[email protected]',
...makeSearchable('name','John Doe',['autoComplete'])
}
)import { searchBy } from 'firestore-search-helper';
const results = searchBy('name', 'Joh', 'autoComplete');makeSearchable Props
| Prop name | Description | Default value | Example values |
| ------------------------ | ------------------------------------------------------ | ------------------ | ---------------------------------------- |
| key | string : The field name. | n/a | name |
| value | string : value of the field. | n/a | John Doe |
| searchType | Array<searchType> : The search types for the field | n/a | ['autoComplete', 'fullTextSearch'] |
searchBy Props
| Prop name | Description | Default value | Example values |
| ------------------------ | --------------------------------------------------------------------- | ------------------ | ---------------------------------------- |
| key | string : The field name. | n/a | name |
| value | string : value of the field. | n/a | John Doe |
| searchType | searchType : The search types for the field | n/a | 'autoComplete' or 'fullTextSearch' |
| srict | boolean : Whether to match exact (true) or similar (false) works only for fullTextSearch | true | 'true' |
How it works
The makeSearchable function generates n-grams (substrings of a given length) for the specified fields and stores them in a searchable property on each object. When searchBy is called, it generates n-grams for the search query and finds the objects that have the most matching n-grams. This approach allows for efficient and typo-tolerant searching.
Contributing
Contributions are welcome! Please feel free to submit a pull request or open an issue.
License
This project is licensed under the MIT License.
