Fine-tune handling of GIL for search (#124)
parent
91a422b49e
commit
76512f859b
|
@ -740,7 +740,7 @@ impl Document {
|
|||
{
|
||||
self.field_values
|
||||
.entry(field_name)
|
||||
.or_insert_with(Vec::new)
|
||||
.or_default()
|
||||
.push(Value::from(value));
|
||||
}
|
||||
|
||||
|
|
16
src/index.rs
16
src/index.rs
|
@ -308,19 +308,13 @@ impl Index {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Acquires a Searcher from the searcher pool.
|
||||
/// Returns a searcher
|
||||
///
|
||||
/// If no searcher is available during the call, note that
|
||||
/// this call will block until one is made available.
|
||||
///
|
||||
/// Searcher are automatically released back into the pool when
|
||||
/// they are dropped. If you observe this function to block forever
|
||||
/// you probably should configure the Index to have a larger
|
||||
/// searcher pool, or you are holding references to previous searcher
|
||||
/// for ever.
|
||||
fn searcher(&self, py: Python) -> Searcher {
|
||||
/// This method should be called every single time a search query is performed.
|
||||
/// The same searcher must be used for a given query, as it ensures the use of a consistent segment set.
|
||||
fn searcher(&self) -> Searcher {
|
||||
Searcher {
|
||||
inner: py.allow_threads(|| self.reader.searcher()),
|
||||
inner: self.reader.searcher(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -133,13 +133,14 @@ impl Searcher {
|
|||
#[pyo3(signature = (query, limit = 10, count = true, order_by_field = None, offset = 0))]
|
||||
fn search(
|
||||
&self,
|
||||
_py: Python,
|
||||
py: Python,
|
||||
query: &Query,
|
||||
limit: usize,
|
||||
count: bool,
|
||||
order_by_field: Option<&str>,
|
||||
offset: usize,
|
||||
) -> PyResult<SearchResult> {
|
||||
py.allow_threads(move || {
|
||||
let mut multicollector = MultiCollector::new();
|
||||
|
||||
let count_handle = if count {
|
||||
|
@ -153,7 +154,8 @@ impl Searcher {
|
|||
let collector = TopDocs::with_limit(limit)
|
||||
.and_offset(offset)
|
||||
.order_by_u64_field(order_by);
|
||||
let top_docs_handle = multicollector.add_collector(collector);
|
||||
let top_docs_handle =
|
||||
multicollector.add_collector(collector);
|
||||
let ret = self.inner.search(query.get(), &multicollector);
|
||||
|
||||
match ret {
|
||||
|
@ -167,11 +169,15 @@ impl Searcher {
|
|||
.collect();
|
||||
(r, result)
|
||||
}
|
||||
Err(e) => return Err(PyValueError::new_err(e.to_string())),
|
||||
Err(e) => {
|
||||
return Err(PyValueError::new_err(e.to_string()))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let collector = TopDocs::with_limit(limit).and_offset(offset);
|
||||
let top_docs_handle = multicollector.add_collector(collector);
|
||||
let collector =
|
||||
TopDocs::with_limit(limit).and_offset(offset);
|
||||
let top_docs_handle =
|
||||
multicollector.add_collector(collector);
|
||||
let ret = self.inner.search(query.get(), &multicollector);
|
||||
|
||||
match ret {
|
||||
|
@ -185,7 +191,9 @@ impl Searcher {
|
|||
.collect();
|
||||
(r, result)
|
||||
}
|
||||
Err(e) => return Err(PyValueError::new_err(e.to_string())),
|
||||
Err(e) => {
|
||||
return Err(PyValueError::new_err(e.to_string()))
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -193,6 +201,7 @@ impl Searcher {
|
|||
let count = count_handle.map(|h| h.extract(&mut multifruit));
|
||||
|
||||
Ok(SearchResult { hits, count })
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns the overall number of documents in the index.
|
||||
|
|
Loading…
Reference in New Issue