Skip to content

Expose Tantivy's TermSetQuery #249

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

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 20 additions & 0 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,26 @@ impl Query {
})
}

/// Construct a Tantivy's TermSetQuery
#[staticmethod]
#[pyo3(signature = (schema, field_name, field_values))]
pub(crate) fn term_set_query(
schema: &Schema,
field_name: &str,
field_values: Vec<&PyAny>,
) -> PyResult<Query> {
let terms = field_values
.into_iter()
.map(|field_value| {
make_term(&schema.inner, field_name, &field_value)
})
.collect::<Result<Vec<_>, _>>()?;
let inner = tv::query::TermSetQuery::new(terms);
Ok(Query {
inner: Box::new(inner),
})
}

/// Construct a Tantivy's AllQuery
#[staticmethod]
pub(crate) fn all_query() -> PyResult<Query> {
Expand Down
4 changes: 4 additions & 0 deletions tantivy/tantivy.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ class Query:
) -> Query:
pass

@staticmethod
def term_set_query(schema: Schema, field_name: str, field_values: Sequence[Any]) -> Query:
pass

@staticmethod
def all_query() -> Query:
pass
Expand Down
29 changes: 29 additions & 0 deletions tests/tantivy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,35 @@ def test_term_query(self, ram_index):
searched_doc = index.searcher().doc(doc_address)
assert searched_doc["title"] == ["The Old Man and the Sea"]

def test_term_set_query(self, ram_index):
index = ram_index

# Should match 1 document that contains both terms
terms = ["old", "man"]
query = Query.term_set_query(index.schema, "title", terms)
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"] == ["The Old Man and the Sea"]

# Should not match any document since the term does not exist in the index
terms = ["a long term that does not exist in the index"]
query = Query.term_set_query(index.schema, "title", terms)
result = index.searcher().search(query, 10)
assert len(result.hits) == 0

# Should not match any document when the terms list is empty
terms = []
query = Query.term_set_query(index.schema, "title", terms)
result = index.searcher().search(query, 10)
assert len(result.hits) == 0

# Should fail to create the query due to the invalid list object in the terms list
with pytest.raises(ValueError, match = r"Can't create a term for Field `title` with value `\[\]`"):
terms = ["old", [], "man"]
query = Query.term_set_query(index.schema, "title", terms)

def test_all_query(self, ram_index):
index = ram_index
query = Query.all_query()
Expand Down