Node.js 용 Tantivy 기반 검색 엔진
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 : 진정한 경우 메모리 인덱스를 만듭니다 await index.add(docs) docs 인덱스 스키마와 동일한 구조를 가진 문서 배열입니다.
const results = await index.query(query, [limit], [snippetField]) 인덱스를 쿼리하십시오. 현재 문자열 쿼리 만 지원됩니다. 지원되는 문법에 대한 자세한 내용은 Tantivy 문서를 참조하십시오. limit 반환 할 문서의 최대 수입니다 (기본값 10). snippetField <b>
const results = await catalog.multiQuery(query, indexes) 카탈로그의 모든 인덱스를 쿼리하십시오. indexes 색인 이름의 배열입니다.
확장됩니다
녹슬 부분은 Tantivy 주변의 래퍼입니다. 이진으로 컴파일합니다. 이진은 스토리지 경로로 만 인수로 호출됩니다. Stdin에서 Newline-Elimited JSON 메시지를 듣고 동일한 형식으로 답장합니다.
노드 부품은 녹 바이너리를 생성하고 Stdio 파이프를 통해 통신합니다. 이 간단한 RPC 메커니즘에 더 높은 수준의 API를 추가합니다.
NPM postinstall 단계는 Github 릴리스에서 Rust 부품의 사전 컴파일 된 바이너리를 다운로드하려고합니다. 바이너리는 트래비스를 통해 편집되고 배치됩니다. 일치하는 바이너리를 찾을 수없는 경우 녹 도구 체인을 사용할 수있는 경우 컴파일하려고합니다. 환경 변수 RUST_ENV=development 있으면 cargo run ( --release 없이)이 대신 호출됩니다.