轻量级、高性能的类似 Lucene 的解析器、序列化器和搜索引擎。
最初构建 Liqe 是为了通过 cli 启用 Roarr 日志过滤。从那时起,我一直将这个项目作为一种爱好/智力练习来完善。我已经看到它被各种需要高级搜索的 CLI 和 Web 应用程序所采用。据我所知,它是目前 JavaScript 中最完整的类 Lucene 语法解析器和序列化器,也是一个兼容的内存搜索引擎。
Liqe 用例包括:
请注意,Liqe AST 被视为公共 API,即,人们可以使用 Liqe 查询语言 (LQL) 实现自己的搜索机制。
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 ( parse ( 'height:>170' ) , persons ) ;
// [
// {
// height: 180,
// name: 'John Morton',
// },
// {
// height: 175,
// name: 'David Barker',
// },
// ]
测试单个对象:
test ( parse ( 'name:John' ) , persons [ 0 ] ) ;
// true
test ( parse ( 'name:David' ) , persons [ 0 ] ) ;
// false
突出显示匹配字段和子字符串:
highlight ( parse ( 'name:john' ) , persons [ 0 ] ) ;
// [
// {
// path: 'name',
// query: /(John)/,
// }
// ]
highlight ( parse ( 'height:180' ) , persons [ 0 ] ) ;
// [
// {
// path: 'height',
// }
// ]
Liqe 使用 Liqe 查询语言 (LQL),该语言深受 Lucene 的启发,但以各种方式对其进行了扩展,以提供更强大的搜索体验。
# 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 `full name` field
'full name' : foo
"full 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
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 }
# boolean operators
name :foo AND height : = 100
name :foo OR name : bar
# unary operators
NOT foo
- foo
NOT foo : bar
- foo :bar
name :foo AND NOT ( bio :bar OR bio : baz )
# implicit AND boolean operator
name :foo height : = 100
# grouping
name :foo AND ( bio :bar OR bio : baz )
在任何字段中搜索单词“foo”(不区分大小写)。
foo
在name
字段中搜索单词“foo”。
name :foo
搜索与/foo/i
正则表达式匹配的name
字段值。
name :/foo / i
搜索与f*o
通配符模式匹配的name
字段值。
name :f * o
搜索与f?o
通配符模式匹配的name
字段值。
name :f? o
在name
字段中搜索短语“foo bar”(区分大小写)。
name :"foo bar"
在height
字段中搜索等于 100 的值。
height : = 100
在height
字段中搜索大于 100 的值。
height :>100
在height
字段中搜索大于或等于 100 的值。
height :>=100
在height
字段中搜索大于或等于 100 且小于或等于 200 的值。
height : [ 100 TO 200 ]
在height
字段中搜索大于 100 且小于 200 的值。
height : { 100 TO 200 }
在name
字段中搜索以“foo”开头的任何单词。
name :foo *
在name
字段中搜索以“foo”开头并以“bar”结尾的任何单词。
name :foo * bar
在name
字段中搜索以“foo”开头,后跟单个任意字符的任何单词。
name :foo?
在name
字段中搜索以“foo”开头、后跟单个任意字符并立即以“bar”结尾的任何单词。
name :foo? bar
在name
字段中搜索短语“foo bar”,并在bio
字段中搜索短语“quick Fox”。
name :"foo bar" AND bio : "quick fox"
在name
字段中搜索短语“foo bar”并在bio
字段中搜索短语“quick Fox”,或者在name
字段中搜索单词“fox”。
( name :"foo bar" AND bio : "quick fox" ) OR name : fox
序列化器允许将 Liqe 令牌转换回原始搜索查询。
import {
parse ,
serialize ,
} from 'liqe' ;
const tokens = parse ( 'foo:bar' ) ;
// {
// expression: {
// location: {
// start: 4,
// },
// quoted: false,
// type: 'LiteralExpression',
// value: 'bar',
// },
// field: {
// location: {
// start: 0,
// },
// name: 'foo',
// path: ['foo'],
// quoted: false,
// type: 'Field',
// },
// location: {
// start: 0,
// },
// operator: {
// location: {
// start: 3,
// },
// operator: ':',
// type: 'ComparisonOperator',
// },
// type: 'Tag',
// }
serialize ( tokens ) ;
// 'foo:bar'
import {
type BooleanOperatorToken ,
type ComparisonOperatorToken ,
type EmptyExpression ,
type FieldToken ,
type ImplicitBooleanOperatorToken ,
type ImplicitFieldToken ,
type LiteralExpressionToken ,
type LogicalExpressionToken ,
type RangeExpressionToken ,
type RegexExpressionToken ,
type TagToken ,
type UnaryOperatorToken ,
} from 'liqe' ;
有 11 个 AST 标记描述已解析的 Liqe 查询。
如果您正在构建序列化器,则必须实现所有这些序列化器以完全覆盖所有可能的查询输入。请参阅内置序列化器的示例。
import {
isSafeUnquotedExpression ,
} from 'liqe' ;
/**
* Determines if an expression requires quotes.
* Use this if you need to programmatically manipulate the AST
* before using a serializer to convert the query back to text.
*/
isSafeUnquotedExpression ( expression : string ) : boolean ;
不支持以下 Lucene 功能:
如果出现语法错误,Liqe 会抛出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 ;
}
}
考虑使用highlight-words
包来突出显示 Liqe 匹配项。
如果要修改解析器,请使用npm run watch
在监视模式下运行编译器。
在进行任何更改之前,请使用npm run benchmark
捕获计算机上的当前基准测试。进行任何更改后再次运行基准测试。在提交更改之前,请确保性能不会受到负面影响。