Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Tantivy's FuzzyTermQuery #233

Merged
merged 3 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/query.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{make_term, Schema};
use pyo3::{exceptions, prelude::*, types::PyAny};
use pyo3::{exceptions, prelude::*, types::PyAny, types::PyString};
use tantivy as tv;

/// Tantivy's Query
Expand Down Expand Up @@ -52,4 +52,46 @@ impl Query {
inner: Box::new(inner),
})
}

/// Construct a Tantivy's FuzzyTermQuery
#[staticmethod]
#[pyo3(signature = (schema, field_name, text, distance = 1, transposition_cost_one = true))]
adamreichold marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) fn fuzzy_term_query(
schema: &Schema,
field_name: &str,
text: &PyString,
distance: u8,
transposition_cost_one: bool,
) -> PyResult<Query> {
let term = make_term(&schema.inner, field_name, &text)?;
let inner = tv::query::FuzzyTermQuery::new(
term,
distance,
transposition_cost_one,
);
Ok(Query {
inner: Box::new(inner),
})
}

/// Construct a Tantivy's FuzzyTermQuery of the term prefix
#[staticmethod]
#[pyo3(signature = (schema, field_name, text, distance = 1, transposition_cost_one = true))]
pub(crate) fn fuzzy_term_query_prefix(
adamreichold marked this conversation as resolved.
Show resolved Hide resolved
schema: &Schema,
field_name: &str,
text: &PyString,
distance: u8,
transposition_cost_one: bool,
) -> PyResult<Query> {
let term = make_term(&schema.inner, field_name, &text)?;
let inner = tv::query::FuzzyTermQuery::new_prefix(
term,
distance,
transposition_cost_one,
);
Ok(Query {
inner: Box::new(inner),
})
}
}
8 changes: 8 additions & 0 deletions tantivy/tantivy.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ class Query:
def all_query() -> Query:
pass

@staticmethod
def fuzzy_term_query(schema: Schema, field_name: str, text: str, distance: int = 1, transposition_cost_one: bool = True) -> Query:
pass

@staticmethod
def fuzzy_term_query_prefix(schema: Schema, field_name: str, text: str, distance: int = 1, transposition_cost_one: bool = True) -> Query:
pass


class Order(Enum):
Asc = 1
Expand Down
24 changes: 24 additions & 0 deletions tests/tantivy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,3 +771,27 @@ def test_all_query(self, ram_index):

result = index.searcher().search(query, 10)
assert len(result.hits) == 3

def test_fuzzy_term_query(self, ram_index):
index = ram_index
query = Query.fuzzy_term_query(index.schema, "title", "ice")

# the query "ice" should match "mice"
result = index.searcher().search(query, 10)
assert len(result.hits) == 1
_, doc_address = result.hits[0]
searched_doc = index.searcher().doc(doc_address)
assert searched_doc["title"] == ["Of Mice and Men"]

def test_fuzzy_term_query_prefix(self, ram_index):
index = ram_index
query = Query.fuzzy_term_query_prefix(index.schema, "title", "man")

# the query "man" should match both "man" and "men"
result = index.searcher().search(query, 10)
assert len(result.hits) == 2
titles = set()
for _, doc_address in result.hits:
titles.update(index.searcher().doc(doc_address)["title"])
assert titles == {"The Old Man and the Sea", "Of Mice and Men"}

Loading