lucene-filter.js

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

commit 1085024142dcb23fff49ad820b78dd24f6bbb76f
parent 875790a525bdaa0b5985f998d1684398f0de0212
Author: finwo <finwo@pm.me>
Date:   Tue, 27 Nov 2018 17:02:30 +0100

Fixed brainfart

Diffstat:
MREADME.md | 19++++++++++++-------
Mpackage.json | 6++----
Msrc/index.js | 11++++++++++-
3 files changed, 24 insertions(+), 12 deletions(-)

diff --git a/README.md b/README.md @@ -34,20 +34,23 @@ const lucene = require('lucene-filter'); ### Example +`lucene-filter` transforms a lucene query into a function which returns whether an object matches the query, using the +'boost' set by the query. Returning the data when matching is also possible using `.passthrough`. + ```js const lucene = require('lucene-filter'); let result; const data = [ - { name: 'C-3PO' , description: 'Protocol droid.' , race: 'Droid' }, - { name: 'R2-D2' , description: 'Astromech droid built on Naboo.', race: 'Droid' }, - { name: 'Anakin Skywalker', description: 'Fallen Jedi, the chosen one.' , race: 'Human' }, - { name: 'Obi-Wan Kenobi' , description: 'Jedi Master.' , race: 'Human' }, - { name: 'Moon Moon' , description: 'Mentally challenged wolf.' , race: 'Wolf' }, + { name: 'C-3PO' , description: 'Protocol droid.' , species: 'Droid' }, + { name: 'R2-D2' , description: 'Astromech droid built on Naboo.', species: 'Droid' }, + { name: 'Anakin Skywalker', description: 'Fallen Jedi, the chosen one.' , species: 'Human' }, + { name: 'Obi-Wan Kenobi' , description: 'Jedi Master.' , species: 'Human' }, + { name: 'Moon Moon' , description: 'Mentally challenged wolf.' , species: 'Wolf' }, ]; // Prints an array with both R2-D2 and C-3PO -console.log(data.filter(lucene('droid'))); +console.log(data.filter(lucene('species: droid'))); // Prints an array with only R2-D2 console.log(data.filter(lucene('astromech'))); @@ -56,7 +59,9 @@ console.log(data.filter(lucene('astromech'))); console.log(data.filter(lucene('jedi'))); // Prints an array with only the outcast -console.log(data.filter(lucene('wolf'))); +console.log(data.filter(lucene('name: "moon moon"'))); + + ``` ## Why diff --git a/package.json b/package.json @@ -32,11 +32,9 @@ "keywords": [ "lucene", "filter", - "data", "query", - "rating", - "score", - "scoring" + "search", + "engine" ], "optionalDependencies": { "lucene": "^2.0.2", diff --git a/src/index.js b/src/index.js @@ -45,8 +45,17 @@ const lucene = module.exports = function compiler(query) { return () => 0; }; +// Return the data when matching +lucene.passthrough = function(query) { + let match = lucene(query); + return function(data) { + if(match(data)) return data; + return undefined; + }; +}; + // Add filters & operators -lucene.filters = require('./filters')({lucene}); +lucene.filters = require('./filters'); lucene.operators = require('./operators'); // Browser exports