Gathering detailed insights and metrics for pi-redredisearch
Gathering detailed insights and metrics for pi-redredisearch
Gathering detailed insights and metrics for pi-redredisearch
Gathering detailed insights and metrics for pi-redredisearch
npm install pi-redredisearch
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
RedRediSearch is a Node.js wrapper library for the RediSearch Redis module. It is more-or-less syntactically compatible with Reds, another Node.js search library. RedRediSearch and RediSearch can provide full-text searching that is much faster than the original Reds library (see Benchmarks).
If you are upgrading from Reds, you'll need to make your createSearch
asynchronous and re-index your data. Otherwise, your app-level logic and code should be compatible.
$ npm install redredisearch
The first thing you'll want to do is create a Search
instance, which allows you to pass a key
, used for namespacing within RediSearch so that you may have several searches in the same Redis database. You may specify your own node_redis instance with the redredisearch.setClient
function.
1redredisearch.createSearch('pets',{}, function(err, search) {
2 /* ... */
3});
You can then add items to the index with the Search#index
function.
1var strs = []; 2strs.push('Tobi wants four dollars'); 3strs.push('Tobi only wants $4'); 4strs.push('Loki is really fat'); 5strs.push('Loki, Jane, and Tobi are ferrets'); 6strs.push('Manny is a cat'); 7strs.push('Luna is a cat'); 8strs.push('Mustachio is a cat'); 9 10redredisearch.createSearch('pets',{}, function(err,search) { 11 strs.forEach(function(str, i){ search.index(str, i); }); 12});
To perform a query against the index simply invoke Search#query()
with a string, and pass a callback, which receives an array of ids when present, or an empty array otherwise.
1search 2 .query('Tobi dollars') 3 .end(function(err, ids){ 4 if (err) throw err; 5 console.log('Search results for "%s":', query); 6 ids.forEach(function(id){ 7 console.log(' - %s', strs[id]); 8 }); 9 });
By default, queries are an intersection of the search words. The previous example would yield the following output since only one string contains both "Tobi" and "dollars":
Search results for "Tobi dollars":
- Tobi wants four dollars
We can tweak the query to perform a union by passing either "union" or "or" to Search#type()
in redredisearch.search()
between Search#query()
and Search#end()
, indicating that any of the constants computed may be present for the id
to match.
1search 2 .query('tobi dollars') 3 .type('or') 4 .end(function(err, ids){ 5 if (err) throw err; 6 console.log('Search results for "%s":', query); 7 ids.forEach(function(id){ 8 console.log(' - %s', strs[id]); 9 }); 10 });
The union search would yield the following since three strings contain either "Tobi" or "dollars":
Search results for "tobi dollars":
- Tobi wants four dollars
- Tobi only wants $4
- Loki, Jane, and Tobi are ferrets
RediSearch has an advanced query syntax that can be used by using the 'direct' search type. See the RediSearch documentation for this syntax.
1search 2 .query('(hello|hella) (world|werld)') 3 .type('direct') 4 .end(function(err, ids){ 5 /* ... */ 6 });
Also included in the package is the RediSearch Suggestion API. This has no corollary in the Reds module. The Suggestion API is ideal for auto-complete type situations and is entirely separate from the Search API.
1var suggestions = redredisearch.suggestion('my-suggestion-list'); 2 3suggestions.add( 4 'redis', // add 'redis' 5 2, // with a 'score' of 2, this affects the position in the results, higher = higher up in results 6 function(err,sizeOfSuggestionList) { /* ... */ } // callback 7); 8suggestions.add( 9 'redisearch', 10 5, 11 function(err,sizeOfSuggestionList) { /* ... */ } 12); 13suggestions.add( 14 'reds', 15 1, 16 function(err,sizeOfSuggestionList) { /* ... */ } 17); 18 19/* ... */ 20 21sugggestions.get( 22 're', // prefix - will find anything starting with "re" 23 function(err, returnedSuggestions) { 24 /* returnedSuggestions is set to [ "redisearch", "redis", "reds" ] */ 25 } 26); 27 28sugggestions.get( 29 'redis', // prefix - will find anything starting with "redis", so not "reds" 30 function(err, returnedSuggestions) { 31 /* returnedSuggestions is set to [ "redisearch", "redis" ] */ 32 } 33)
There is also a fuzzy
opt and maxResults
that can either be set by chaining or by passing an object in the second argument in the constructor.
1redredisearch.createSearch(key, options, fn) : Search 2redredisearch.setClient(inClient) 3redredisearch.createClient() 4redredisearch.confirmModule(cb) 5redredisearch.words(str) : Array 6redredisearch.suggestionList(key,opts) : Suggestion 7Search#index(text, id[, fn]) 8Search#remove(id[, fn]); 9Search#query(text, fn[, type]) : Query 10Query#type(type) 11Query#between(str) 12Query#end(fn) 13Suggestion#fuzzy(isFuzzy) 14Suggestion#maxResults(maxResults) 15Suggestion#add(str,score,fn) 16Suggestion#get(prefix,fn) 17Suggestion#del(str,fn) 18
Examples:
1var search = redredisearch.createSearch('misc'); 2search.index('Foo bar baz', 'abc'); 3search.index('Foo bar', 'bcd'); 4search.remove('bcd'); 5search.query('foo bar').end(function(err, ids){});
When compared to Reds, RedRediSearch is much faster at indexing and somewhat faster at query:
Indexing - documents / second
Module | Tiny | Small | Medium | Large |
---|---|---|---|---|
Reds | 122 | 75 | 10 | 0 |
RediRediSearch | 1,256 | 501 | 132 | 5 |
Query - queries / second
Module | 1 term | 2 terms / AND | 2 terms / OR | 3 terms / AND | 3 terms / OR | Long* / AND | Long* / OR |
---|---|---|---|---|---|---|---|
Reds | 8,754 | 8,765 | 8,389 | 7,622 | 7,193 | 1,649 | 1,647 |
RedRediSearch | 10,955 | 12,945 | 10,054 | 12,769 | 8,389 | 6,456 | 12,311 |
The "Long" query string is taken from the Canadian Charter of Rights and Freedoms: "Everyone has the following fundamental freedoms: (a) freedom of conscience and religion; (b) freedom of thought, belief, opinion and expression, including freedom of the press and other media of communication; (c) freedom of peaceful assembly; and (d) freedom of association." (Used because I just had it open in another tab...)
(The MIT License)
No vulnerabilities found.
No security vulnerabilities found.