Skip to content

Commit 452616d

Browse files
committed
Expose Tantivy's TermSetQuery
1 parent 7e57a00 commit 452616d

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

src/query.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,32 @@ impl Query {
8989
})
9090
}
9191

92+
/// Construct a Tantivy's TermSetQuery
93+
#[staticmethod]
94+
#[pyo3(signature = (schema, field_name, field_values))]
95+
pub(crate) fn term_set_query(
96+
schema: &Schema,
97+
field_name: &str,
98+
field_values: Vec<&PyAny>,
99+
) -> PyResult<Query> {
100+
let mut terms: Vec<tv::Term> = Vec::with_capacity(field_values.len());
101+
for field_value in field_values {
102+
let term = make_term(&schema.inner, field_name, &field_value);
103+
if let Ok(term) = term {
104+
terms.push(term);
105+
} else {
106+
return Err(exceptions::PyTypeError::new_err(format!(
107+
"Cannot create a term from the value: {}",
108+
field_value
109+
)));
110+
}
111+
}
112+
let inner = tv::query::TermSetQuery::new(terms);
113+
Ok(Query {
114+
inner: Box::new(inner),
115+
})
116+
}
117+
92118
/// Construct a Tantivy's AllQuery
93119
#[staticmethod]
94120
pub(crate) fn all_query() -> PyResult<Query> {

tantivy/tantivy.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ class Query:
197197
) -> Query:
198198
pass
199199

200+
@staticmethod
201+
def term_set_query(schema: Schema, field_name: str, field_values: Sequence[Any]) -> Query:
202+
pass
203+
200204
@staticmethod
201205
def all_query() -> Query:
202206
pass

tests/tantivy_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,35 @@ def test_term_query(self, ram_index):
765765
searched_doc = index.searcher().doc(doc_address)
766766
assert searched_doc["title"] == ["The Old Man and the Sea"]
767767

768+
def test_term_set_query(self, ram_index):
769+
index = ram_index
770+
771+
# Should match 1 document that contains both terms
772+
terms = ["old", "man"]
773+
query = Query.term_set_query(index.schema, "title", terms)
774+
result = index.searcher().search(query, 10)
775+
assert len(result.hits) == 1
776+
_, doc_address = result.hits[0]
777+
searched_doc = index.searcher().doc(doc_address)
778+
assert searched_doc["title"] == ["The Old Man and the Sea"]
779+
780+
# Should not match any document since the term does not exist in the index
781+
terms = ["a long term that does not exist in the index"]
782+
query = Query.term_set_query(index.schema, "title", terms)
783+
result = index.searcher().search(query, 10)
784+
assert len(result.hits) == 0
785+
786+
# Should not match any document when the terms list is empty
787+
terms = []
788+
query = Query.term_set_query(index.schema, "title", terms)
789+
result = index.searcher().search(query, 10)
790+
assert len(result.hits) == 0
791+
792+
# Should fail to create the query due to the invalid list object in the terms list
793+
with pytest.raises(TypeError, match = r"Cannot create a term from the value: \[\]"):
794+
terms = ["old", [], "man"]
795+
query = Query.term_set_query(index.schema, "title", terms)
796+
768797
def test_all_query(self, ram_index):
769798
index = ram_index
770799
query = Query.all_query()

0 commit comments

Comments
 (0)