2023-12-20 09:40:50 +00:00
|
|
|
use crate::{make_term, Schema};
|
2024-04-13 09:14:56 +00:00
|
|
|
use pyo3::{exceptions, prelude::*, types::PyAny, types::PyString};
|
2019-06-04 09:09:58 +00:00
|
|
|
use tantivy as tv;
|
|
|
|
|
|
|
|
/// Tantivy's Query
|
2024-01-21 20:16:34 +00:00
|
|
|
#[pyclass(frozen, module = "tantivy.tantivy")]
|
2019-06-04 09:09:58 +00:00
|
|
|
pub(crate) struct Query {
|
2020-09-19 13:17:07 +00:00
|
|
|
pub(crate) inner: Box<dyn tv::query::Query>,
|
2020-09-19 09:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Query {
|
2020-09-19 13:17:07 +00:00
|
|
|
pub(crate) fn get(&self) -> &dyn tv::query::Query {
|
|
|
|
&self.inner
|
2020-09-19 09:36:31 +00:00
|
|
|
}
|
2019-06-04 09:09:58 +00:00
|
|
|
}
|
|
|
|
|
2022-04-15 03:50:37 +00:00
|
|
|
#[pymethods]
|
|
|
|
impl Query {
|
2019-08-02 11:23:10 +00:00
|
|
|
fn __repr__(&self) -> PyResult<String> {
|
2020-09-19 09:36:31 +00:00
|
|
|
Ok(format!("Query({:?})", self.get()))
|
2019-06-04 09:09:58 +00:00
|
|
|
}
|
2023-12-20 09:40:50 +00:00
|
|
|
|
|
|
|
/// Construct a Tantivy's TermQuery
|
|
|
|
#[staticmethod]
|
|
|
|
#[pyo3(signature = (schema, field_name, field_value, index_option = "position"))]
|
|
|
|
pub(crate) fn term_query(
|
|
|
|
schema: &Schema,
|
|
|
|
field_name: &str,
|
|
|
|
field_value: &PyAny,
|
|
|
|
index_option: &str,
|
|
|
|
) -> PyResult<Query> {
|
|
|
|
let term = make_term(&schema.inner, field_name, field_value)?;
|
|
|
|
let index_option = match index_option {
|
|
|
|
"position" => tv::schema::IndexRecordOption::WithFreqsAndPositions,
|
|
|
|
"freq" => tv::schema::IndexRecordOption::WithFreqs,
|
|
|
|
"basic" => tv::schema::IndexRecordOption::Basic,
|
|
|
|
_ => return Err(exceptions::PyValueError::new_err(
|
|
|
|
"Invalid index option, valid choices are: 'basic', 'freq' and 'position'"
|
|
|
|
))
|
|
|
|
};
|
|
|
|
let inner = tv::query::TermQuery::new(term, index_option);
|
|
|
|
Ok(Query {
|
|
|
|
inner: Box::new(inner),
|
|
|
|
})
|
|
|
|
}
|
2024-03-31 11:56:22 +00:00
|
|
|
|
|
|
|
/// Construct a Tantivy's AllQuery
|
|
|
|
#[staticmethod]
|
|
|
|
pub(crate) fn all_query() -> PyResult<Query> {
|
|
|
|
let inner = tv::query::AllQuery {};
|
|
|
|
Ok(Query {
|
|
|
|
inner: Box::new(inner),
|
|
|
|
})
|
|
|
|
}
|
2024-04-13 09:14:56 +00:00
|
|
|
|
|
|
|
/// Construct a Tantivy's FuzzyTermQuery
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `schema` - Schema of the target index.
|
|
|
|
/// * `field_name` - Field name to be searched.
|
|
|
|
/// * `text` - String representation of the query term.
|
|
|
|
/// * `distance` - (Optional) Edit distance you are going to alow. When not specified, the default is 1.
|
|
|
|
/// * `transposition_cost_one` - (Optional) If true, a transposition cost will be 1; otherwise it will be 2. When not specified, the default is true.
|
|
|
|
/// * `prefix` - (Optional) If true, only prefix matched results are returned. When not specified, the default is false.
|
|
|
|
#[staticmethod]
|
|
|
|
#[pyo3(signature = (schema, field_name, text, distance = 1, transposition_cost_one = true, prefix = false))]
|
|
|
|
pub(crate) fn fuzzy_term_query(
|
|
|
|
schema: &Schema,
|
|
|
|
field_name: &str,
|
|
|
|
text: &PyString,
|
|
|
|
distance: u8,
|
|
|
|
transposition_cost_one: bool,
|
|
|
|
prefix: bool,
|
|
|
|
) -> PyResult<Query> {
|
|
|
|
let term = make_term(&schema.inner, field_name, &text)?;
|
|
|
|
let inner = if prefix {
|
|
|
|
tv::query::FuzzyTermQuery::new_prefix(
|
|
|
|
term,
|
|
|
|
distance,
|
|
|
|
transposition_cost_one,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
tv::query::FuzzyTermQuery::new(
|
|
|
|
term,
|
|
|
|
distance,
|
|
|
|
transposition_cost_one,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
Ok(Query {
|
|
|
|
inner: Box::new(inner),
|
|
|
|
})
|
|
|
|
}
|
2019-06-04 09:09:58 +00:00
|
|
|
}
|