diff --git a/src/searcher.rs b/src/searcher.rs index 528e54f..719f58b 100644 --- a/src/searcher.rs +++ b/src/searcher.rs @@ -276,6 +276,20 @@ impl Searcher { self.inner.segment_readers().len() } + /// Return the overall number of documents containing + /// the given term. + #[pyo3(signature = (field_name, field_value))] + fn doc_freq( + &self, + field_name: &str, + field_value: &Bound, + ) -> PyResult { + // Wrap the tantivy Searcher `doc_freq` method to return a PyResult. + let schema = self.inner.schema(); + let term = crate::make_term(schema, field_name, field_value)?; + self.inner.doc_freq(&term).map_err(to_pyerr) + } + /// Fetches a document from Tantivy's store given a DocAddress. /// /// Args: diff --git a/tantivy/tantivy.pyi b/tantivy/tantivy.pyi index cb3b253..316075f 100644 --- a/tantivy/tantivy.pyi +++ b/tantivy/tantivy.pyi @@ -337,6 +337,9 @@ class Searcher: def doc(self, doc_address: DocAddress) -> Document: pass + def doc_freq(self, field_name: str, field_value: Any) -> int: + pass + class IndexWriter: def add_document(self, doc: Document) -> int: pass diff --git a/tests/tantivy_test.py b/tests/tantivy_test.py index 6200ed7..a627eaa 100644 --- a/tests/tantivy_test.py +++ b/tests/tantivy_test.py @@ -64,6 +64,12 @@ class TestClass(object): assert len(result.hits) == 1 + def test_doc_freq(self, ram_index): + index = ram_index + searcher = index.searcher() + doc_freq = searcher.doc_freq("body", "and") + assert doc_freq == 3 + def test_and_aggregate(self, ram_index_numeric_fields): index = ram_index_numeric_fields query = Query.all_query()