chore: migrate store to redb
This commit is contained in:
1 parent
7f0286dc40
commit
9a8b6e52e9
12 files changed
+1954
-1365
No files matched your search
+41
-18
@@ -1,13 +1,13 @@
|
||||
use tantivy::schema::*;
|
||||
use tantivy::{doc, Index, IndexWriter, IndexReader, ReloadPolicy};
|
||||
use crate::models::{Adr, Entity, Snippet, Task};
|
||||
use std::sync::Mutex;
|
||||
use crate::models::{Entity, Task, Adr, Snippet};
|
||||
use tantivy::schema::*;
|
||||
use tantivy::{Index, IndexReader, IndexWriter, ReloadPolicy, doc};
|
||||
|
||||
pub struct MemoryIndex {
|
||||
index: Index,
|
||||
reader: IndexReader,
|
||||
writer: Mutex<IndexWriter>,
|
||||
|
||||
|
||||
// Schema fields
|
||||
pub id_field: Field,
|
||||
pub title_field: Field,
|
||||
@@ -17,7 +17,7 @@ pub struct MemoryIndex {
|
||||
}
|
||||
|
||||
impl MemoryIndex {
|
||||
pub fn new() -> tantivy::Result<Self> {
|
||||
pub fn new(store_dir: &std::path::Path) -> tantivy::Result<Self> {
|
||||
let mut schema_builder = Schema::builder();
|
||||
let id_field = schema_builder.add_text_field("id", STRING | STORED);
|
||||
let title_field = schema_builder.add_text_field("title", TEXT | STORED);
|
||||
@@ -25,14 +25,17 @@ impl MemoryIndex {
|
||||
let type_field = schema_builder.add_text_field("type", STRING | STORED);
|
||||
let namespace_field = schema_builder.add_text_field("namespace", STRING | STORED);
|
||||
let schema = schema_builder.build();
|
||||
|
||||
let index = Index::create_in_ram(schema.clone());
|
||||
|
||||
let index_dir = store_dir.join("tantivy_index");
|
||||
std::fs::create_dir_all(&index_dir).unwrap();
|
||||
let index = Index::open_in_dir(&index_dir).unwrap_or_else(|_| Index::create_in_dir(&index_dir, schema.clone()).unwrap());
|
||||
|
||||
let writer = index.writer(50_000_000)?;
|
||||
let reader = index
|
||||
.reader_builder()
|
||||
.reload_policy(ReloadPolicy::OnCommitWithDelay)
|
||||
.try_into()?;
|
||||
|
||||
|
||||
Ok(Self {
|
||||
index,
|
||||
reader,
|
||||
@@ -71,23 +74,43 @@ impl MemoryIndex {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn search(&self, query: &str, namespace: Option<&str>) -> tantivy::Result<Vec<(String, String)>> {
|
||||
pub fn search(
|
||||
&self,
|
||||
query: &str,
|
||||
namespace: Option<&str>,
|
||||
) -> tantivy::Result<Vec<(String, String)>> {
|
||||
let searcher = self.reader.searcher();
|
||||
let query_parser = tantivy::query::QueryParser::for_index(&self.index, vec![self.title_field, self.body_field]);
|
||||
let query_parser = tantivy::query::QueryParser::for_index(
|
||||
&self.index,
|
||||
vec![self.title_field, self.body_field],
|
||||
);
|
||||
let q = query_parser.parse_query(query)?;
|
||||
|
||||
let top_docs = searcher.search(&q, &tantivy::collector::TopDocs::with_limit(50).order_by_score())?;
|
||||
|
||||
let top_docs = searcher.search(
|
||||
&q,
|
||||
&tantivy::collector::TopDocs::with_limit(50).order_by_score(),
|
||||
)?;
|
||||
let mut results = Vec::new();
|
||||
for (_score, doc_address) in top_docs {
|
||||
let retrieved_doc = searcher.doc::<tantivy::TantivyDocument>(doc_address)?;
|
||||
let id = retrieved_doc.get_first(self.id_field).and_then(|v| v.as_str()).unwrap_or("").to_string();
|
||||
let doc_type = retrieved_doc.get_first(self.type_field).and_then(|v| v.as_str()).unwrap_or("").to_string();
|
||||
let doc_ns = retrieved_doc.get_first(self.namespace_field).and_then(|v| v.as_str()).unwrap_or("");
|
||||
if let Some(ns) = namespace {
|
||||
if doc_ns != ns && doc_ns != "global" {
|
||||
let id = retrieved_doc
|
||||
.get_first(self.id_field)
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
let doc_type = retrieved_doc
|
||||
.get_first(self.type_field)
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
let doc_ns = retrieved_doc
|
||||
.get_first(self.namespace_field)
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("");
|
||||
if let Some(ns) = namespace
|
||||
&& doc_ns != ns && doc_ns != "global" {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
results.push((id, doc_type));
|
||||
}
|
||||
Ok(results)
|
||||
|
||||
Reference in new issue
Block a user