0.8.3 • Published 4 years ago

lucid-suggest v0.8.3

Weekly downloads
6
License
MIT
Repository
github
Last release
4 years ago

LucidSuggest

Autocomplete engine that works out of the box. Fast, easy to use, runs in browsers and Node.js. Built with Rust and WebAssembly.

Note: this package is pre-1.0, it hasn't been battle-tested in production, API may change.

Table of contents

Resources

Live demo.

API reference.

Code examples:

Getting started

Install:

npm install lucid-suggest

Initialize:

import {LucidSuggest} from 'lucid-suggest/en'

const suggest = new LucidSuggest()
suggest.addRecords([
    {id: 1, title: "Electric Toothbrush"},
    {id: 2, title: "Lightning to USB-C Cable"},
    {id: 3, title: "AA Alkaline Batteries"},
])

Search:

await suggest.search("batteries")
// returns:
// [
//   Hit { title: "AA Alkaline [Batteries]" }
// ]

Rendering results

By default LucidSuggest highlights hit titles using [ ]. The easiest way to change it is to use highlight helper function:

import {LucidSuggest, highlight} from 'lucid-suggest'

const suggest = new LucidSuggest()
const hits = await suggest.search("to")

hits.map(hit => ({
    value: hit.record.id.toString(),
    label: highlight(hit, '<strong>', '</strong>')
}))
// returns:
// [
//   {value: "1", label: "Electric <strong>To</strong>othbrush"},
//   {value: "2", label: "Lightning <strong>to</strong> USB-C Cable"},
// ]

Or you can directly operate on chunks of a highlighted text, which can come in handy if you need a more complex render logic:

const hits = await suggest.search("to")
hits.map(hit => ({
    value: hit.record.id.toString(),
    label: hit.chunks
        .map(c => c.highlight ? `<strong>${c.text}</strong>` : c.text)
        .join('')
}))
// returns:
// [
//   {value: "1", label: "Electric <strong>To</strong>othbrush"},
//   {value: "2", label: "Lightning <strong>to</strong> USB-C Cable"},
// ]

For examples of rendering in React or Vue, see Resources section.

Fulltext search features

When an exact match is unavailable, the best possible partial matches are returned:

await suggest.search("plastic toothbrush")
// returns:
// [
//   Hit { title: "Electric [Toothbrush]" }
// ]

Search as you type, results are provided from the first letter:

await suggest.search("c")
// returns:
// [
//   Hit { title: "Lightning to USB-C [C]able" }
// ]

Search algorithm is resilient to different kinds of typos:

await suggest.search("alkln")
// returns:
// [
//   Hit { title: "AA [Alkalin]e Batteries" }
// ]
await suggest.search("tooth brush")
// returns:
// [
//   Hit { title: "Electric [Toothbrush]" }
// ]

Stemming is used to handle different word forms:

await suggest.search("battery")
// returns:
// [
//   Hit { title: "AA Alkaline [Batteri]es" }
// ]

Function words (articles, prepositions, etc.) receive special treatment, so they don't occupy top positions every time you start typing a word:

await suggest.search("to")
// returns:
// [
//   Hit { title: "Electric [To]othbrush" },
//   Hit { title: "Lightning [to] USB-C Cable" },
// ]

Rating

Optional rating field can be used as a tie breaker: records with greater rating are ranked higher. Use priority, product popularity, or term frequency as rating to improve overall scoring.

For example, let's use state population as rating:

suggest.addRecords([
    {id: 1, rating:  3000, title: "Nevada"},
    {id: 2, rating:  8900, title: "New Jersey"},
    {id: 3, rating: 19500, title: "New York"},
])
await suggest.search("ne")
// returns:
// [
//   Hit { title: "[Ne]w York" },
//   Hit { title: "[Ne]w Jersey" },
//   Hit { title: "[Ne]vada" },
// ]

Supported languages

LanguageModule
Germanlucid-suggest/de
Englishlucid-suggest/en
Frenchlucid-suggest/fr
Spanishlucid-suggest/es
Portugueselucid-suggest/pt
Russianlucid-suggest/ru

Bundle sizes

Note that base64-encoded Wasm source constitutes the bulk of a bundle. Minifiers don't affect it, but gzip compresses it well.

langsizegzipped
de200K79K
en202K80K
es205K80K
es208K82K
pt205K81K
ru202K79K

Performance

LucidSuggest works best with shorter sentences, like shopping items or book titles. Using longer texts, like articles or movie descriptions, may lead to performance regressions and generally poor user experience.

LucidSuggest can handle large dataset. For example, for 10000 records, each containing 4-8 common English words, you can expect a typical search to take about 1 ms, so you can simply call it at every keystroke without using throttling or Web Workers.

Below are the detailed performance measurements, obtained using Node.js 14.3, Intel Core i7 (I7-9750H) 2.6 GHz.

Searching:

2-4 words4-8 words
100 records0.09 ms0.24 ms
1000 records0.27 ms0.48 ms
10 000 records0.45 ms0.68 ms
100 000 records1.40 ms2.00 ms

Indexing:

2-4 words4-8 words
100 records1 ms2 ms
1000 records11 ms16 ms
10 000 records88 ms160 ms
100 000 records810 ms1900 ms
0.8.1

4 years ago

0.8.3

4 years ago

0.8.2

4 years ago

0.8.0

4 years ago

0.7.3

4 years ago

0.7.2

4 years ago

0.7.1

4 years ago

0.7.0

4 years ago

0.6.2

4 years ago

0.5.0

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.0

4 years ago

0.3.1

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago