sonar tantivy
v0.3.3
node.js的基于触觉的搜索引擎
const IndexCatalog = require ( '@arso-project/sonar-tantivy' )
( async function ( ) {
const catalog = new IndexCatalog ( './data' )
const schema = getSchema ( )
const index = await catalog . openOrCreate ( 'index-name' , schema )
const docs = getDocs ( )
await index . add ( docs )
const results = await index . query ( 'world' )
console . log ( 'query results' , results )
} ) ( )
function getDocs ( ) {
return [
{ id : '0' , title : 'Hello world!' , body : 'tell me more' } ,
{ id : '1' , title : 'Ola mundo!' , body : 'que pasa pues' }
]
}
function getSchema ( ) {
return [
{
name : 'title' ,
type : 'text' ,
options : {
indexing : { record : 'position' , tokenizer : 'en_stem' } ,
stored : true
}
} ,
{
name : 'body' ,
type : 'text' ,
options : {
indexing : { record : 'position' , tokenizer : 'en_stem' } ,
stored : true
}
} ,
{
name : 'id' ,
type : 'text' ,
options : { indexing : null , stored : true }
} ,
]
} npm install @arso-project/sonar-tantivy postinstall脚本会自动尝试下载Rust/Tantivy部分的预编译二进制文件。如果不成功,如果存在锈工具链,脚本将尝试对其进行编译。
const IndexCatalog = require ( '@arso-project/sonar-tantivy' ) const catalog = new IndexCatalog(storage) storage是将存储索引的文件系统路径。
const index = await catalog.openOrCreate(indexName, schema) indexName是标识索引的字符串。它应仅包含在文件系统路径中有效的字符。 schema是索引模式,在tantivy模式定义下以JSON-Serializable对象表示。文档不是集中式ATM,请参见上面的示例。
const index = await catalog.create(indexName, schema, opts)创建一个索引。如果该indexName的索引已经存在,将投掷。 opts是:
ram :如果TRUE创建内存索引await index.add(docs) docs是与索引模式相同结构的文档阵列。
const results = await index.query(query, [limit], [snippetField])查询索引。目前,仅支持字符串查询,有关受支持的语法的详细信息,请参见Tantivy Docs。 limit是要返回的最大文档数(默认10)。 snippetField是一个字段的名称,该字段返回带有关键字的结果片段(如html,带有<b>标签)的名称
const results = await catalog.multiQuery(query, indexes)查询目录中的所有索引。 indexes是索引名称的数组。
要扩大
生锈的部分是tantivy周围的包装纸。它编译为二进制。将二进制用作为唯一的参数调用。它在Stdin上听取Newline限制的JSON消息的听力,并以相同的格式回复。
节点部分产生了生锈的二进制,并通过STDIO管道进行通信。它围绕着这种简单的RPC机制添加了高级API。
NPM postinstall步骤将尝试从GitHub版本下载Rust零件的预编译的二进制。二进制文件是通过Travis编译和部署的。如果找不到匹配的二进制文件,它将尝试编译如果有锈工具链可用。如果存在环境变量RUST_ENV=development ,则将调用cargo run (无--release )。