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 )。