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

@archipel/sonar

v0.1.2

Published

A tantivy search engine

Downloads

2

Readme

sonar

A tantivy based search engine for Node.js

Example

const Sonar = require('@archipel/sonar')

(async function () {
  const catalog = new Sonar('./data')
  const schema = getSchema()
  const index = await catalog.openOrCreate('index-name', schema)
  const docs = getDocs()
  await index.add(docs)
  const results = await index.query('world')
  console.log('query results', results)
})()

function getDocs () {
  return [
    { id: '0', title: 'Hello world!', body: 'tell me more' },
    { id: '1', title: 'Ola mundo!', body: 'que pasa pues' }
  ]
}

function getSchema () {
  return [
    {
      name: 'title',
      type: 'text',
      options: {
        indexing: { record: 'position', tokenizer: 'en_stem' },
        stored: true
      }
    },
    {
      name: 'body',
      type: 'text',
      options: {
        indexing: { record: 'position', tokenizer: 'en_stem' },
        stored: true
      }
    },
    {
      name: 'id',
      type: 'text',
      options: { indexing: null, stored: true }
    },
  ]
}

Installation

npm install @archipel/sonar

A postinstall script automatically tries to download a precompiled binary for the rust/tantivy part. If unsuccessfull the script will try to compile it if a rust toolchain is present.

API

const catalog = new Sonar(storage)

storage is a file system path where the index will be stored.

const index = await catalog.openOrCreate(indexName, schema)

indexName is a string to identifiy the index. It should only contain characters valid in file system paths. schema is the index schema, expressed as a JSON-serializable object following the tantivy schema definition. Documentation is not centralized atm, see example above.

const index = await catalog.create(indexName, schema, opts)

Create an index. Will throw if an index by this indexName exists already. opts are:

  • ram: If true create an in-memory index

await index.add(docs)

docs is an array of documents with the same structure as the index schema.

const results = await index.query(query, [limit], [snippetField])

Query the index. At the moment only string queries are supported, see tantivy docs for details on the supported grammar. limit is the max number of documents to return (default 10). snippetField is the name of a field for which to return a result snippet with keywords highlighted (as HTML, with <b> tags)

const results = await catalog.multiQuery(query, indexes)

Query all indexes in the catalog. indexes is an array of index names.

To be expanded

Implementation details

The rust part is a wrapper around tantivy. It compiles to a binary. The binary is invoked with a storage path as only argument. It listens for newline-delimited JSON messages on STDIN, and replies in the same format.

The node part spawns the rust binary and communicates over the STDIO pipe. It adds a higher-level API around this simple RPC mechanism.

A npm postinstall step will try to download a precompiled binary of the rust part from Github releases. The binaries are compiled and deployed via Travis. If it cannot find a matching binary, it will try to compile if a rust toolchain is available. If the environment variable RUST_ENV=development is present, cargo run (without --release) will be invoked instead.