Wrap Searcher.doc_freq. Fixes #344 (#345)

master
Caleb Hattingh 2024-09-12 17:23:52 +02:00 committed by GitHub
parent 2a5ae8962d
commit 74ca327de6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 0 deletions

View File

@ -276,6 +276,20 @@ impl Searcher {
self.inner.segment_readers().len() 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<PyAny>,
) -> PyResult<u64> {
// 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. /// Fetches a document from Tantivy's store given a DocAddress.
/// ///
/// Args: /// Args:

View File

@ -337,6 +337,9 @@ class Searcher:
def doc(self, doc_address: DocAddress) -> Document: def doc(self, doc_address: DocAddress) -> Document:
pass pass
def doc_freq(self, field_name: str, field_value: Any) -> int:
pass
class IndexWriter: class IndexWriter:
def add_document(self, doc: Document) -> int: def add_document(self, doc: Document) -> int:
pass pass

View File

@ -64,6 +64,12 @@ class TestClass(object):
assert len(result.hits) == 1 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): def test_and_aggregate(self, ram_index_numeric_fields):
index = ram_index_numeric_fields index = ram_index_numeric_fields
query = Query.all_query() query = Query.all_query()