lucene-filter.js

Data filter for lucene queries
git clone git://git.finwo.net/lib/lucene-filter.js
Log | Files | Refs | README | LICENSE

README.md (2572B)


      1 # lucene-filter
      2 
      3 > Data filter for lucene queries
      4 
      5 Easily rate, order or filter objects by lucene queries.
      6 
      7 ---
      8 
      9 ## Installation
     10 
     11 ```bash
     12 npm install --save lucene-filter
     13 ```
     14 
     15 ## Usage
     16 
     17 For a better overview of what's possible, take a look at [lucene-query-parser][lucene-query-parser].
     18 
     19 ### Node.JS
     20 
     21 ```javascript
     22 
     23 // Either of these is supported
     24 const lucene = require('lucene-filter')(require('lucene'));
     25 const lucene = require('lucene-filter')(require('lucene-queryparser'));
     26 const lucene = require('lucene-filter')(require('lucene-query-parser'));
     27 ```
     28 
     29 ### Browser
     30 
     31 ```html
     32 // If requirejs is detected, registers as an anonymous module
     33 // Else, it'll attach to window.lucene
     34 <script src="https://unpkg.com/lucene-query-parser"></script>
     35 <script src="https://unpkg.com/lucene-filter"></script>
     36 ```
     37 
     38 ### Example
     39 
     40 `lucene-filter` transforms a lucene query into a function which returns whether an object matches the query, using the
     41 'boost' set by the query. Returning the data when matching is also possible using `.passthrough`.
     42 
     43 ```js
     44 const lucene = require('lucene-filter')(require('lucene'));
     45 let   result;
     46 
     47 // In the browser:
     48 // const lucene = window.lucene(window.lucenequeryparser);
     49 
     50 const data = [
     51   { name: 'C-3PO'           , description: 'Protocol droid.'                , species: 'Droid' },
     52   { name: 'R2-D2'           , description: 'Astromech droid built on Naboo.', species: 'Droid' },
     53   { name: 'Anakin Skywalker', description: 'Fallen Jedi, the chosen one.'   , species: 'Human' },
     54   { name: 'Obi-Wan Kenobi'  , description: 'Jedi Master.'                   , species: 'Human' },
     55   { name: 'Moon Moon'       , description: 'Mentally challenged wolf.'      , species: 'Wolf'  },
     56 ];
     57 
     58 // Prints an array with both R2-D2 and C-3PO
     59 console.log(data.filter(lucene('species: droid')));
     60 
     61 // Prints an array with only R2-D2
     62 console.log(data.filter(lucene('astromech')));
     63 
     64 // Prints an array with both jedi
     65 console.log(data.filter(lucene('jedi')));
     66 
     67 // Prints an array with only the outcast
     68 console.log(data.filter(lucene('name: "moon moon"')));
     69 
     70 // Prints Obi-Wan
     71 console.log(data.filter(lucene('species: human AND master')));
     72 
     73 ```
     74 
     75 ## Why
     76 
     77 I wanted something simple to use & not build a completely new query language without standards or documentation. This
     78 module uses either [lucene][lucene] or [lucene-query-parser][lucene-query-parser] to parse the given query into
     79 something easy-to-process & turns it into a simple-to-use function.
     80 
     81 [lucene]: https://www.npmjs.com/package/lucene
     82 [lucene-query-parser]: https://www.npmjs.com/package/lucene-query-parser