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

lucene-query-generator

v0.2.0

Published

Generate escaped lucene query strings

Downloads

1,390

Readme

lucene-query-generator

Builds a lucene query string from an object. Escapes the field names and values to prevent errors on user input. Can be used in the browser or node, however to avoid injection must be run server side.

Installation

npm install lucene-query-generator

API

convert

Convert an object into a string query.

Arguments

  • terms: Object representing the query
  • options (optional): Object with the following
    • schema (optional): The field type mapping

Examples

Querying all fields

The simplest form of query

generator.convert({ $operands: [ 'hello world' ] }
// 'hello world'

Querying fielded data

Query specific fields using key value pairs to represent the field name and desired value respectively.

Defaults to the AND operator.

generator.convert({
  $operands: [{ name: 'gareth' }, { job: 'geek' }]
});
// 'name:gareth AND job:geek'

Specify the OR operator.

generator.convert({
  $operator: 'OR',
  $operands: [{ name: 'gareth' }, { job: 'geek' }]
});
// 'name:gareth OR job:geek'

Create a disjunction on one field.

generator.convert({
  $operands: [{ name: ['gareth', 'milan'] }]
});
// 'name:(gareth OR milan)'

null values will be ignored.

generator.convert({
  $operands: [{ name: 'gareth' }, { job: null }]
});
// 'name:gareth'

Specify the NOT operator.

generator.convert({
  $operands: [
    { name: 'gareth' },
    {
      $operator: 'not',
      $operands: [
        { job: 'geek' }
      ]
    }
  ]
});
// name:gareth AND NOT job:geek

Unlike searching all fields, values will be escaped automatically.

generator.convert({
  $operands: [
    { name: 'gareth-bowen' }
  ]
});
// name:"gareth-bowen"

Nested queries

generator.convert({
  $operator: 'and',
  $operands: [
    { name: 'gareth' },
    { 
      $operator: 'or',
      $operands: [
        { job: 'geek' },
        { job: 'musician' }
      ]
    }
  ]
});
// name:gareth AND (job:geek OR job:musician)

Field types

All field terms default to type string, which can be overridden by providing a schema. Available types are: string, int, long, float, double, boolean, date. Dates can be either Date objects or ISO Strings.

generator.convert(
  {
    $operands: [{
      name: 'gareth',
      seconds: 123456789,
      dob: new Date(123456789),
      dod: '2014-05-31T11:00:00.000Z'
    }]
  }, {
    schema: {
      seconds: 'long',
      dob: 'date',
      dod: 'date'
    }
  }
);
// name:gareth AND seconds<long>:123456789 AND dob<date>:"1970-01-02T10:17:36.789Z" AND dod<date>:"2014-05-31T11:00:00.000Z"

Escaping can be turned off for field values if you want to allow your users to be able to provide lucene expressions.

generator.convert(
  {
    $operands: [{
      name: 'gar?th'
    }]
  }, {
    schema: {
      name: {
        type: 'string',
        allowSpecialCharacters: true
      }
    }
  }
);
// name:gar?th

Range queries

Query for values matching a range with the $from and $to keys.

generator.convert(
  {
    $operands: [{
      name: { $from: 'gareth', $to: 'milan' },
      age: { $from: 55, $to: 63 }
    }]
  }, {
    schema: {
      age: 'int'
    }
  }
);
// name:[gareth TO milan] AND age<int>:[55 TO 63]

Development

npm install
npm test

Build Status

Builds brought to you courtesy of Travis CI.

Build Status