npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redis-autosuggest

v1.0.2

Published

A redis completer using Sebastian's trie algorithm: https://gist.github.com/574044

Downloads

6

Readme

redis-autosuggest for node.js

Installation

npm install redis-autosuggest

Usage

There are two subprojects within this project. These are the completer and the importer. The constructor of the Importer and the Completer must get two mandatory parameters. The first parameter ('shops' in the example above) is the namespace that the search terms will save and the second one is the redis connection.

Importer

Get an importer and provide a unique namespace that the terms will save:

$ node
> const Importer = require('redis-autosuggest/lib/Importer');
> const importer = new Importer('shops', redis);
> importer.addFromFile(`${__dirname}/shops.csv`);

Add completions for a document:

> importer.addCompletions(text, id, score);
> importer.addCompletions(text, id, score);

id and score are optional. If id is null, it will be ignored.

If an id is provided, it will be concatenated with text in the final lookup table making a compound key of the form id:text. The idea here is to give you a way to stash a database key and a chunk of text together, so you can find your way back to some document you have stored elsewhere. E.g., if you're auto-completing a search by title, you could use the key to help retrieve the entire document.

Completer

Get a completer and provide a unique namespace for your app:

$ node
> const Completer = require('redis-autosuggest');
> const completer = new Completer('shops', redis);
> completer.search('Zara');

It's up to me to remember that I used a key and deal with that when I process the text.

Shops example - importer

Assuming a CSV with stores:

  • 1,Virgin Active
  • 2,Action Store
  • 3,Acton Shoes
  • 4,Active Boots

The importer will insert into the shops:autocomplete sorted set values like that:

  • 0 acton*
  • 0 acton
  • 0 acto
  • 0 active*
  • 0 active
  • 0 activ
  • 0 action*
  • 0 action
  • 0 actio
  • 0 acti
  • 0 act

At the same time for each one of the starred values above we are creating another set that will include the exact phrases. For example, the shops:docs:active set will include the phrase: 0 1:Virgin Active 0 4:Active Boots

Shops example - search

Having a term like 'act' the results that we should get back are:

  • 1:Virgin Active
  • 2:Action Store
  • 3:Acton Shoes
  • 4:Active Boots

Getting the term 'act' we are getting from shops:autocomplete sorted set the results from the act and the next 50. We will iterate over these results and we will keep only the ones that they end up with a star ( * ). The terms that they end with a star we know that there is another set (e.g. shops:docs:active, shops:docs:acton, shops:docs:action) that will hold all the phrases that matched this starred term. We do this for all the starred terms that we found in the shops:autocomplete and finally we end up with a set of phrases. This is what we give back as a response.