support for offset to do record pagination

master
sourcepirate 2020-09-05 23:51:54 +08:00
parent 8a7eac3e78
commit 89e1cabc58
2 changed files with 5 additions and 4 deletions

View File

@ -13,7 +13,7 @@ crate-type = ["cdylib"]
[dependencies] [dependencies]
chrono = "0.4.11" chrono = "0.4.11"
tantivy = "0.12.0" tantivy = "0.13.0"
itertools = "0.9.0" itertools = "0.9.0"
futures = "0.3.4" futures = "0.3.4"

View File

@ -98,12 +98,13 @@ impl Searcher {
/// Returns `SearchResult` object. /// Returns `SearchResult` object.
/// ///
/// Raises a ValueError if there was an error with the search. /// Raises a ValueError if there was an error with the search.
#[args(limit = 10, count = true)] #[args(limit = 10, offset = 0, count = true)]
fn search( fn search(
&self, &self,
_py: Python, _py: Python,
query: &Query, query: &Query,
limit: usize, limit: usize,
offset: usize,
count: bool, count: bool,
order_by_field: Option<&str>, order_by_field: Option<&str>,
) -> PyResult<SearchResult> { ) -> PyResult<SearchResult> {
@ -119,7 +120,7 @@ impl Searcher {
if let Some(order_by) = order_by_field { if let Some(order_by) = order_by_field {
let field = get_field(&self.inner.index().schema(), order_by)?; let field = get_field(&self.inner.index().schema(), order_by)?;
let collector = let collector =
TopDocs::with_limit(limit).order_by_u64_field(field); TopDocs::with_limit(limit).and_offset(offset).order_by_u64_field(field);
let top_docs_handle = multicollector.add_collector(collector); let top_docs_handle = multicollector.add_collector(collector);
let ret = self.inner.search(&query.inner, &multicollector); let ret = self.inner.search(&query.inner, &multicollector);
@ -137,7 +138,7 @@ impl Searcher {
Err(e) => return Err(ValueError::py_err(e.to_string())), Err(e) => return Err(ValueError::py_err(e.to_string())),
} }
} else { } else {
let collector = TopDocs::with_limit(limit); let collector = TopDocs::with_limit(limit).and_offset(offset);
let top_docs_handle = multicollector.add_collector(collector); let top_docs_handle = multicollector.add_collector(collector);
let ret = self.inner.search(&query.inner, &multicollector); let ret = self.inner.search(&query.inner, &multicollector);