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-string-builder

v1.0.8

Published

Build Lucene queries by defining lucene query builders

Downloads

1,295

Readme

Lucene Query String Builder

NPM NPM Downloads Dependency Status Standard Code Style

Notice

Lucene Query String Builder is looking for a developer to help with the API. I'll continue performing the maintenance tasks.

Easily build your lucene string queries using small and pure functions.

Imagine having an API that leverages lucene for performing queries on the (indexed) database. In that case you might want to generate lucene query strings on the client/front end.

The usage section shows how you can leverage this lib for your purposes.

Setup

npm install lucene-query-string-builder --save

Features

  • escapes lucene special chars when creating a term string
  • contains all the operators lucene uses
  • simple lucene.builder function for defining a lucene query builder

Usage

Let's see how you can use lucene query string builder to define lucene query strings with simple JavaScript functions.

Assuming that the lucene global variable contains the lucene functions. This would be the default when loaded into a browser.


var findUserLuceneQueryString = lucene.builder(function(data){

  // just to make the example more readable;
  var _ = lucene;

  return _.group(_.and(
    _.field('eye-color', _.term(data.eye.color)),
    _.field('age', _.range(data.age.min, data.age.max))
  ));

});

var luceneQueryString = findUserLuceneQueryString({
  eye: { color: 'brown'},
  age: {
    min: 10,
    max: 20
  }
});

luceneQueryString === '( eye-color: "brown" AND age:{ 10 TO 20 } )' // => true

The functions are based on the lucene specifications found here: https://lucene.apache.org/core/2_9_4/queryparsersyntax.html#Terms


  var _ = lucene;

  /***
   * terms or term
   */

  _.term('hello'); // => '"hello"'

  _.terms('hello world'); // => '"hello world"'


  /***
   * field
   */

  _.field('hello', _.term('world')); // => 'hello: "world"'


  /***
   * or/and/not
   *
   * These functions are variadic and all work the same way. This example only
     shows the or but it works similar with and and not
   */

  _.or(_.term('hello'), _.term('world')); // => '"hello" OR "world"'

  _.or(_.term('hello'), _.term('you'), _.term('world')); // => '"hello" OR "you" OR "world"'


  /***
   * group
   *
   * Is a variadic function too
   */

  _.group(_.term('hello'), _.term('you'), _.term('world')); // => '( "hello" "you" "world" )'


  /***
   * range
   *
   * Takes two strings and 2 booleans.
   */

  /* combined with the field function to query for ages between 10 and 20 */
  _.field('age', _.range(10, 20)); // => 'age: { 10 TO 20 }'


  /***
   * fuzzy
   */

  _.fuzzy(_.term('hello'), 0.2); // => '"hello"~0.2'


  /***
   * proximity
   */

  _.proximity("a", "c", 2); // => '"a b"'~2


  /***
   * required
   */

  _.required(_.term('required')); // => '+"required"'

Tests

set -eo pipefail

{
  npm i
  npm prune
} > /dev/null

npx standard --fix
npx nyc npm t

> [email protected] test
> tape ./test/index.js

TAP version 13
# builder
ok 1 should be strictly equal
# range
ok 2 should be strictly equal
ok 3 should be strictly equal
ok 4 should be strictly equal
ok 5 should be strictly equal
# group
ok 6 should be strictly equal
# term
ok 7 should be strictly equal
ok 8 should be strictly equal
# fuzzy
ok 9 should throw
ok 10 should be strictly equal
ok 11 should be strictly equal
ok 12 should be strictly equal
ok 13 should be strictly equal
ok 14 should be strictly equal
ok 15 should be strictly equal
# proximity
ok 16 should be strictly equal
ok 17 should throw
ok 18 should throw
ok 19 should throw
ok 20 should throw
ok 21 should throw
# or/and/not
ok 22 should be strictly equal
ok 23 should be strictly equal
ok 24 should be strictly equal
ok 25 should be strictly equal
ok 26 should be strictly equal
ok 27 should be strictly equal

1..27
# tests 27
# pass  27

# ok

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |   90.32 |    94.44 |   88.24 |   90.91 |                   
 index.js |   90.32 |    94.44 |   88.24 |   90.91 | 69-72,165-167     
----------|---------|----------|---------|---------|-------------------

Contributing

I have not gotten the chance to use this lib in my own projects. Please share your thoughts, issues and improvements.

  • Make sure your dependencies are installed by running: npm run-script setup
  • Then start editing the index.js
  • You should add and/or edit the tests in test/index.js
  • Run your tests and see what happens

When performing pull request make sure to not add the dist files. This is left to the maintainers(s) of the library. They are responsible to version and avoid code breakages.

You can perform your own build with npm run-script build to make a lucine.js and a lucine.min.js

notice

I am currently not using this repository in any of my projects. Therefore I am looking for people that are able to make LQSB more useful for them and others.

Road map

  • split all functions into separate files
  • tasks for running tests on dist/lucene.js and dist/lucene.min.js

License

The MIT License (MIT)