2023-09-13 10:03:39 +00:00
|
|
|
use crate::to_pyerr;
|
|
|
|
use pyo3::prelude::*;
|
|
|
|
use tantivy as tv;
|
2024-05-03 21:35:19 +00:00
|
|
|
// Bring the trait into scope to use methods like `as_str()` on `OwnedValue`.
|
|
|
|
use tantivy::schema::Value;
|
2023-09-13 10:03:39 +00:00
|
|
|
|
2024-03-22 00:47:33 +00:00
|
|
|
/// Tantivy Snippet
|
2023-09-13 10:03:39 +00:00
|
|
|
///
|
2024-03-22 00:47:33 +00:00
|
|
|
/// Snippet contains a fragment of a document, and some highlighted
|
|
|
|
/// parts inside it.
|
2024-01-21 20:16:34 +00:00
|
|
|
#[pyclass(module = "tantivy.tantivy")]
|
2023-09-13 10:03:39 +00:00
|
|
|
pub(crate) struct Snippet {
|
|
|
|
pub(crate) inner: tv::Snippet,
|
|
|
|
}
|
|
|
|
|
2024-01-21 20:16:34 +00:00
|
|
|
#[pyclass(module = "tantivy.tantivy")]
|
2023-09-13 10:03:39 +00:00
|
|
|
pub(crate) struct Range {
|
|
|
|
#[pyo3(get)]
|
|
|
|
start: usize,
|
|
|
|
#[pyo3(get)]
|
|
|
|
end: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl Snippet {
|
|
|
|
pub fn to_html(&self) -> PyResult<String> {
|
|
|
|
Ok(self.inner.to_html())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn highlighted(&self) -> Vec<Range> {
|
|
|
|
let highlighted = self.inner.highlighted();
|
|
|
|
let results = highlighted
|
|
|
|
.iter()
|
|
|
|
.map(|r| Range {
|
|
|
|
start: r.start,
|
|
|
|
end: r.end,
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
results
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-21 20:16:34 +00:00
|
|
|
#[pyclass(module = "tantivy.tantivy")]
|
2023-09-13 10:03:39 +00:00
|
|
|
pub(crate) struct SnippetGenerator {
|
|
|
|
pub(crate) field_name: String,
|
|
|
|
pub(crate) inner: tv::SnippetGenerator,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl SnippetGenerator {
|
|
|
|
#[staticmethod]
|
|
|
|
pub fn create(
|
|
|
|
searcher: &crate::Searcher,
|
|
|
|
query: &crate::Query,
|
|
|
|
schema: &crate::Schema,
|
|
|
|
field_name: &str,
|
|
|
|
) -> PyResult<SnippetGenerator> {
|
|
|
|
let field = schema
|
|
|
|
.inner
|
|
|
|
.get_field(field_name)
|
|
|
|
.or(Err("field not found"))
|
|
|
|
.map_err(to_pyerr)?;
|
|
|
|
let generator =
|
|
|
|
tv::SnippetGenerator::create(&searcher.inner, query.get(), field)
|
|
|
|
.map_err(to_pyerr)?;
|
|
|
|
|
2024-02-05 11:01:26 +00:00
|
|
|
Ok(SnippetGenerator {
|
2023-09-13 10:03:39 +00:00
|
|
|
field_name: field_name.to_string(),
|
|
|
|
inner: generator,
|
2024-02-05 11:01:26 +00:00
|
|
|
})
|
2023-09-13 10:03:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn snippet_from_doc(&self, doc: &crate::Document) -> crate::Snippet {
|
|
|
|
let text: String = doc
|
|
|
|
.iter_values_for_field(&self.field_name)
|
2024-05-03 21:35:19 +00:00
|
|
|
.flat_map(|ov| ov.as_str())
|
2023-09-13 10:03:39 +00:00
|
|
|
.collect::<Vec<&str>>()
|
|
|
|
.join(" ");
|
|
|
|
|
|
|
|
let result = self.inner.snippet(&text);
|
|
|
|
Snippet { inner: result }
|
|
|
|
}
|
2024-08-08 10:36:04 +00:00
|
|
|
|
|
|
|
pub fn set_max_num_chars(&mut self, max_num_chars: usize) {
|
|
|
|
self.inner.set_max_num_chars(max_num_chars);
|
|
|
|
}
|
2023-09-13 10:03:39 +00:00
|
|
|
}
|