liqe
Lightweight and performant Lucene-like parser and search engine.
Usage
import {
filter,
highlight,
parse,
test,
} from 'liqe';
const persons = [
{
height: 180,
name: 'John Morton',
},
{
height: 175,
name: 'David Barker',
},
{
height: 170,
name: 'Thomas Castro',
},
];
Filter a collection:
filter(parse('height:>170'), persons);
// [
// {
// height: 180,
// name: 'John Morton',
// },
// {
// height: 175,
// name: 'David Barker',
// },
// ]
Test a single object:
test(parse('name:John'), persons[0]);
// true
test(parse('name:David'), persons[0]);
// false
Highlight matching fields and substrings:
test(highlight('name:john'), persons[0]);
// [
// {
// path: 'name',
// query: /(John)/,
// }
// ]
test(highlight('height:180'), persons[0]);
// [
// {
// path: 'height',
// }
// ]
Query Syntax
Liqe syntax cheat sheet
# search for "foo" term anywhere in the document (case insensitive)
foo
# search for "foo" term anywhere in the document (case sensitive)
'foo'
"foo"
# search for "foo" term in `name` field
name:foo
# search for "foo" term in `first` field, member of `name`, i.e.
# matches {name: {first: 'foo'}}
name.first:foo
# search using regex
name:/foo/
name:/foo/o
# search using wildcard
name:foo*bar
# boolean search
member:true
member:false
# null search
member:null
# search for age =, >, >=, <, <=
height:=100
height:>100
height:>=100
height:<100
height:<=100
# search for height in range (inclusive, exclusive)
height:[100 TO 200]
height:{100 TO 200}
# logical operators
name:foo AND height:=100
name:foo OR name:bar
# grouping
name:foo AND (bio:bar OR bio:baz)
Keyword matching
Search for word "foo" in any field (case insensitive).
foo
Search for word "foo" in the name
field.
name:foo
Search for name
field values matching /foo/i
regex.
name:/foo/i
Search for name
field values matching f*o
wildcard pattern.
name:f*o
Search for phrase "foo bar" in the name
field (case sensitive).
name:"foo bar"
Number matching
Search for value equal to 100 in the height
field.
height:=100
Search for value greater than 100 in the height
field.
height:>100
Search for value greater than or equal to 100 in the height
field.
height:>=100
Range matching
Search for value greater or equal to 100 and lower or equal to 200 in the height
field.
height:[100 TO 200]
Search for value greater than 100 and lower than 200 in the height
field.
height:{100 TO 200}
Wildcard matching
Search for any word that starts with "foo" in the name
field.
name:foo*
Search for any word that starts with "foo" and ends with bar in the name
field.
name:foo*bar
Logical Operators
Search for phrase "foo bar" in the name
field AND the phrase "quick fox" in the bio
field.
name:"foo bar" AND bio:"quick fox"
Search for either the phrase "foo bar" in the name
field AND the phrase "quick fox" in the bio
field, or the word "fox" in the name
field.
(name:"foo bar" AND bio:"quick fox") OR name:fox
Compatibility with Lucene
The following Lucene abilities are not supported:
Recipes
Handling syntax errors
In case of a syntax error, Liqe throws SyntaxError
.
import {
parse,
SyntaxError,
} from 'liqe';
try {
parse('foo bar');
} catch (error) {
if (error instanceof SyntaxError) {
console.error({
// Syntax error at line 1 column 5
message: error.message,
// 4
offset: error.offset,
// 1
offset: error.line,
// 5
offset: error.column,
});
} else {
throw error;
}
}
Highlighting matches
Consider using highlight-words
package to highlight Liqe matches.
Development
Compiling Parser
If you are going to modify parser, then use npm run watch
to run compiler in watch mode.
Benchmarking Changes
Before making any changes, capture the current benchmark on your machine using npm run benchmark
. Run benchmark again after making any changes. Before committing changes, ensure that performance is not negatively impacted.