Node suggestive search
Don't let spelling mistakes prevent your users from finding what they were looking for! This node module was built to help type-ahead and dropdown search boxes and also correct misspelled searches (did you mean?).
This module is compatible with:
- Redis Node.JS Client 2.8.0, Client to connect with Redis https://www.npmjs.com/package/redis
- MongoDB Node.JS Driver 2.2.19, Driver to connect with MongoDB http://mongodb.github.io/node-mongodb-native/
- NeDB 1.8.0, The JavaScript Database from Louis Chatriot https://github.com/louischatriot/nedb
Installation, tests
Module name on npm is "node-suggestive-search".
npm install node-suggestive-search --save # Install the latest version in your project
Example of usage
https://node-suggestive-search.herokuapp.com
API
- Setting options
- Loading a database
- Searching for items
- Getting words suggestions
- Getting items suggestions
- Insert items
- Remove items
Setting options
This module supports Redis, MongoDB and NeDB but you can use an in-memory volatile cache.
Configuration without database (in-memory):
var nss = ;
Configuration without database with cache (in-memory):
var nss = ;
Configuration using Redis:
var nss = ;
Configuration using MongoDB:
var nss = ;
Configuration using NeDB with a datafile:
var nss = ;
Configuration using NeDB without a datafile (in-memory):
var nss = ;
There is also an option to include stop-words:
var nss = ; //current built-in available stopwords dictionary//1033 - en-us - English - United States//1036 - fr-fr - French - France//1040 - it-it - Italian - Italy//1046 - pt-br - Portuguese - Brazil//1048 - ro - Romanian - Romania//2057 - en-gb - English - Great Britain//2070 - pt-pt - Portuguese - Portugal
The "cache" option enables an in-memory copy of the data structure boosting the performance. Turn off this option if you have more than one instance accessing the same database.
Loading a database
Build a dictionary composed by items and words that need to be searched.
Example of a JSON to be imported (Items.json):
"itemName":"WHISKY RED LABEL" "itemId":"1" "keywords":"FANCY" "itemName":"WHISKY BLACK LABEL" "itemId":"2" "keywords":"EXPENSIVE" "itemName":"BLACK FOREST LABELY HAM L/S" "itemId":"3" "itemName":"PESTO PARMESAN HAM" "itemId":"4" "itemName":"DELI SWEET SLICE SMOKED HAM" "itemId":"5"
Load the JSON from file
//you can change the charset to match your filenss;
Load the JSON from file with your properties names
nss;
Load the JSON from string
let jSonString = `[{"itemName":"WHISKY RED LABEL", "itemId":"1", "keywords": "fancy"},{ "itemName":"WHISKY BLACK LABEL", "itemId":"2"}]`; nss;
Load the JSON from string with additional fields (price, popularity and thumbImg). You can insert any additional field excluding itemId, itemName and keywords.
let jSonString = `[{"itemName":"WHISKY RED LABEL", "itemId":"1", "keywords":"fancy", "price":25.57, "popularity":1, "thumbImg":"whisky-red-label.png"},{ "itemName":"WHISKY BLACK LABEL", "itemId":"2", "price":19.99, "popularity":0.9, "thumbImg":"whisky-black-label.png"}]`; nss;
Load the JSON from string with your properties names
let jSonString = `[{"nm":"WHISKY RED LABEL", "id":"1", "kw": "fancy"},{ "nm":"WHISKY BLACK LABEL", "id":"2"}]`; nss;
Load the JSON from string with your properties names and additional fields (price, popularity and thumbImg). You can insert any additional field excluding itemId, itemName and keywords.
let jSonString = `[{"nm":"WHISKY RED LABEL", "id":"1", "kw":"fancy", "price":25.57, "popularity":1, "thumbImg":"whisky-red-label.png"},{ "nm":"WHISKY BLACK LABEL", "id":"2", "price":19.99, "popularity":0.9, "thumbImg":"whisky-black-label.png"}]`; nss;
Searching for items
Getting itemsId from searched words.
Examples of how to call the api and responses:
nss; //did you mean search result with misspelled search criterianss; //did you mean search result with misspelled search criterianss; //query with paramenter returnItemsJson = true nss; //query with paramenter returnItemsJson = true and ordering by popularity, desc using an object on a database loaded with additional fieldslet orderByObject = field: "popularity" direction: "desc"; nss; //query with paramenter returnItemsJson = true and ordering by popularity, desc using a function on a database loaded with additional fieldslet orderByFunc = { return xpopularity < ypopularity; }; nss; //quoted search criterianss; //quoted search criterianss; //dashed search criterianss; //slashed search criterianss; //double quoted search criterianss;
Getting words suggestions
Getting words suggestions to fill dropdown boxes or type-ahead text fields.
Examples of how to call the api and responses:
nss; nss; nss;
Getting items suggestions
Getting items suggestions to fill dropdown boxes or type-ahead text fields.
Examples of how to call the api and responses:
nss; nss; nss; nss; //get one item suggestions ordering by price, asc using a function and omitting the direction.let orderByObject = field: "price"; nss; //get one item suggestions ordering by price, asc using a function.let orderByFunc = { return xprice > yprice; }; nss;
Insert items
Insert a new item into the database.
Examples of how to call the api and responses:
let newItem = "itemId": "VODKA ABSOLUT" "itemName": "6" "keywords": "Keyword1, keyword2..." ; nss;
Insert an item with your properties names.
let newItem = "id": "VODKA ABSOLUT" "nm": "6" "kw": "Keyword1, keyword2..." ; nss;
Insert an item with additional fields (price, popularity and thumbImg). You can insert any additional field excluding itemId, itemName and keywords.
let newItem = "itemId": "VODKA ABSOLUT" "itemName": "6" "keywords": "Keyword1, keyword2..." "price": 2557 "popularity": 1 "thumbImg": "vodka-absolute.png" ; nss;
Remove items
Remove an item from the database.
Examples of how to call the api and responses:
let itemId = "6"; nss;
Roadmap
- Catalog (several dictionaries)
- More databases support
- Inject your database plugin
- Browser version.
- Translate JS to TS
Pull requests
If you submit a pull request, thanks! There are a couple of rules to follow to make it manageable:
- The pull request should be atomic, i.e. contain only one feature. If it contains more, please submit multiple pull requests.
- Please stick to the current coding style. It's important that the code uses a coherent style for readability.
- Update the readme accordingly.
- Last but not least: The goal here is simplicity.
Bug reporting guidelines
If you report a bug, thank you! That said for the process to be manageable please strictly adhere to the following guidelines. I'll not be able to handle bug reports that don't:
- Your bug report should be a self-containing project with a package.json for any dependencies you need. I need to run through a simple
npm install; node bugreport.js
. - It should use assertions to showcase the expected vs actual behavior.
- Simplify as much as you can. Strip all your application-specific code.
- Please explain precisely in the issue.
- The code should be Javascript.
Donations
Please consider donating some if your app is successful or you learned something valuable here.
Keep in mind that I am maintaining this repository on my free time so thank you for considering a donation. 👍