-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ruihang Xia <[email protected]>
- Loading branch information
Showing
12 changed files
with
567 additions
and
1 deletion.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2024 Greptime Team | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
pub mod create; | ||
pub mod error; | ||
pub mod search; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2024 Greptime Team | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::collections::BTreeMap; | ||
use std::path::Path; | ||
|
||
use snafu::ResultExt; | ||
use tantivy::schema::{OwnedValue, Schema, INDEXED, STORED, TEXT}; | ||
use tantivy::{Document, Index, IndexWriter, TantivyDocument}; | ||
|
||
use super::error::TantivySnafu; | ||
use crate::full_text_index::error::Result; | ||
|
||
pub struct FullTextIndexCreater { | ||
index: Index, | ||
writer: IndexWriter, | ||
count_field: tantivy::schema::Field, | ||
text_field: tantivy::schema::Field, | ||
|
||
row_count: usize, | ||
segment_size: usize, | ||
} | ||
|
||
impl FullTextIndexCreater { | ||
pub fn new<P>(segment_size: usize, path: P) -> Result<Self> | ||
where | ||
P: AsRef<Path>, | ||
{ | ||
// build schema | ||
let mut schema_builder = Schema::builder(); | ||
let count_field = schema_builder.add_i64_field("seg_count", INDEXED | STORED); | ||
let text_field = schema_builder.add_text_field("text", TEXT); | ||
let schema = schema_builder.build(); | ||
|
||
// build index | ||
let index = Index::create_in_dir(path, schema).context(TantivySnafu)?; | ||
|
||
// build writer | ||
// 100 MB | ||
let writer = index.writer(100_000_000).context(TantivySnafu)?; | ||
|
||
Ok(Self { | ||
index, | ||
writer, | ||
count_field, | ||
text_field, | ||
row_count: 0, | ||
segment_size, | ||
}) | ||
} | ||
|
||
pub fn push_string(&mut self, content: String) -> Result<()> { | ||
let mut doc = TantivyDocument::new(); | ||
doc.add_text(self.text_field, content); | ||
doc.add_i64(self.count_field, (self.row_count / self.segment_size) as _); | ||
self.writer.add_document(doc).context(TantivySnafu)?; | ||
self.row_count += 1; | ||
|
||
self.writer.commit().context(TantivySnafu)?; | ||
Ok(()) | ||
} | ||
|
||
pub fn finish(&mut self) -> Result<()> { | ||
self.row_count = 0; | ||
self.writer.commit().context(TantivySnafu)?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2024 Greptime Team | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use common_macro::stack_trace_debug; | ||
use snafu::{Location, Snafu}; | ||
use tantivy::directory::error::OpenDirectoryError; | ||
|
||
#[derive(Snafu)] | ||
#[snafu(visibility(pub))] | ||
#[stack_trace_debug] | ||
pub enum Error { | ||
#[snafu(display("Tantivy error"))] | ||
Tantivy { | ||
#[snafu(source)] | ||
error: tantivy::TantivyError, | ||
location: Location, | ||
}, | ||
|
||
#[snafu(display("Failed to open directory"))] | ||
OpenDirectory { | ||
#[snafu(source)] | ||
error: OpenDirectoryError, | ||
location: Location, | ||
}, | ||
|
||
#[snafu(display("Failed to parse tantivy query"))] | ||
ParseQuery { | ||
#[snafu(source)] | ||
error: tantivy::query::QueryParserError, | ||
location: Location, | ||
}, | ||
} | ||
|
||
pub type Result<T> = std::result::Result<T, Error>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2024 Greptime Team | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::collections::HashSet; | ||
use std::path::Path; | ||
|
||
use snafu::ResultExt; | ||
use tantivy::directory::MmapDirectory; | ||
use tantivy::query::QueryParser; | ||
use tantivy::schema::Value; | ||
use tantivy::{Index, IndexReader, TantivyDocument, TantivyError}; | ||
|
||
use super::error::ParseQuerySnafu; | ||
use crate::full_text_index::error::{OpenDirectorySnafu, Result, TantivySnafu}; | ||
|
||
pub struct FullTextIndexSearcher { | ||
index: Index, | ||
count_field: tantivy::schema::Field, | ||
text_field: tantivy::schema::Field, | ||
reader: IndexReader, | ||
} | ||
|
||
impl FullTextIndexSearcher { | ||
pub fn open<P>(path: P) -> Result<Self> | ||
where | ||
P: AsRef<Path>, | ||
{ | ||
let index = Index::open_in_dir(path).context(TantivySnafu)?; | ||
let schema = index.schema(); | ||
let count_field = schema.get_field("seg_count").unwrap(); | ||
let text_field = schema.get_field("text").unwrap(); | ||
let reader = index.reader().context(TantivySnafu)?; | ||
|
||
Ok(Self { | ||
index, | ||
count_field, | ||
text_field, | ||
reader, | ||
}) | ||
} | ||
|
||
pub fn search(&self, query: &str) -> Result<Vec<usize>> { | ||
let searcher = self.reader.searcher(); | ||
let query_parser = QueryParser::for_index(&self.index, vec![self.text_field]); | ||
let query = query_parser.parse_query(query).context(ParseQuerySnafu)?; | ||
let top_docs = searcher | ||
.search(&query, &tantivy::collector::TopDocs::with_limit(100)) | ||
.context(TantivySnafu)?; | ||
let mut result = HashSet::new(); | ||
for (_score, doc_address) in top_docs { | ||
let retrieved_doc = searcher | ||
.doc::<TantivyDocument>(doc_address) | ||
.context(TantivySnafu)?; | ||
let seg_count = retrieved_doc | ||
.get_first(self.count_field) | ||
.unwrap() | ||
.as_i64() | ||
.unwrap(); | ||
result.insert(seg_count); | ||
} | ||
Ok(result.into_iter().map(|x| x as _).collect()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,5 @@ | |
|
||
#![feature(iter_partition_in_place)] | ||
|
||
pub mod full_text_index; | ||
pub mod inverted_index; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters