probly search
2.0.1
由Rust編寫的全文搜索庫,以插入速度進行了優化,可完全控制評分計算。
最初,這是節點庫ndx的端口。
配方(標題)搜索50K文檔。
https://quantleaf.github.io/probly-search-demo/
進行得分的三種方式
ScoreCalculator性狀來完全自定義自己的評分功能。基於TRIE的動態倒置索引。
多個字段全文索引和搜索。
每場得分提升。
可配置的令牌。
與查詢擴展的免費文本查詢。
快速分配,但潛在刪除。
WASM兼容
請參閱集成測試。
請參閱食譜搜索演示項目
使用具有2個字段的文檔創建索引。查詢文檔,並刪除文檔。
use std :: collections :: HashSet ;
use probly_search :: {
index :: Index ,
query :: {
score :: default :: { bm25 , zero_to_one } ,
QueryResult ,
} ,
} ;
// A white space tokenizer
fn tokenizer ( s : & str ) -> Vec < Cow < str > > {
s . split ( ' ' ) . map ( Cow :: from ) . collect :: < Vec < _ > > ( )
}
// We have to provide extraction functions for the fields we want to index
// Title
fn title_extract ( d : & Doc ) -> Vec < & str > {
vec ! [ d.title.as_str ( ) ]
}
// Description
fn description_extract ( d : & Doc ) -> Vec < & str > {
vec ! [ d.description.as_str ( ) ]
}
// Create index with 2 fields
let mut index = Index :: < usize > :: new ( 2 ) ;
// Create docs from a custom Doc struct
let doc_1 = Doc {
id : 0 ,
title : "abc" . to_string ( ) ,
description : "dfg" . to_string ( ) ,
} ;
let doc_2 = Doc {
id : 1 ,
title : "dfgh" . to_string ( ) ,
description : "abcd" . to_string ( ) ,
} ;
// Add documents to index
index . add_document (
& [ title_extract , description_extract ] ,
tokenizer ,
doc_1 . id ,
& doc_1 ,
) ;
index . add_document (
& [ title_extract , description_extract ] ,
tokenizer ,
doc_2 . id ,
& doc_2 ,
) ;
// Search, expected 2 results
let mut result = index . query (
& "abc" ,
& mut bm25 :: new ( ) ,
tokenizer ,
& [ 1. , 1. ] ,
) ;
assert_eq ! ( result.len ( ) , 2 ) ;
assert_eq ! (
result [ 0 ] ,
QueryResult {
key: 0 ,
score: 0.6931471805599453
}
) ;
assert_eq ! (
result [ 1 ] ,
QueryResult {
key: 1 ,
score: 0.28104699650060755
}
) ;
// Remove documents from index
index . remove_document ( doc_1 . id ) ;
// Vacuum to remove completely
index . vacuum ( ) ;
// Search, expect 1 result
result = index . query (
& "abc" ,
& mut bm25 :: new ( ) ,
tokenizer ,
& [ 1. , 1. ] ,
) ;
assert_eq ! ( result.len ( ) , 1 ) ;
assert_eq ! (
result [ 0 ] ,
QueryResult {
key: 1 ,
score: 0.1166450426074421
}
) ;在BM25實現和零對一的實現中仔細測試以獲取更多查詢示例。
運行所有測試
cargo test運行所有基準
cargo bench麻省理工學院