post review fixes

This commit is contained in:
mpostma
2021-02-01 19:51:47 +01:00
parent 17c463ca61
commit 9af0a08122
10 changed files with 233 additions and 158 deletions

View File

@ -3,14 +3,14 @@ mod updates;
pub use search::{SearchQuery, SearchResult};
use std::fs::create_dir_all;
use std::ops::Deref;
use std::sync::Arc;
use std::fs::create_dir_all;
use sha2::Digest;
use crate::{option::Opt, index_controller::Settings};
use crate::index_controller::{IndexController, LocalIndexController};
use crate::{option::Opt, index_controller::Settings};
#[derive(Clone)]
pub struct Data {
@ -67,7 +67,7 @@ impl Data {
options.max_mdb_size.get_bytes(),
options.max_udb_size.get_bytes(),
)?;
let indexes = Arc::new(index_controller);
let index_controller = Arc::new(index_controller);
let mut api_keys = ApiKeys {
master: options.clone().master_key,
@ -77,7 +77,7 @@ impl Data {
api_keys.generate_missing_api_keys();
let inner = DataInner { index_controller: indexes, options, api_keys };
let inner = DataInner { index_controller, options, api_keys };
let inner = Arc::new(inner);
Ok(Data { inner })

View File

@ -2,11 +2,11 @@ use std::collections::HashSet;
use std::mem;
use std::time::Instant;
use serde_json::{Value, Map};
use serde::{Deserialize, Serialize};
use milli::{Index, obkv_to_json, FacetCondition};
use meilisearch_tokenizer::{Analyzer, AnalyzerConfig};
use anyhow::bail;
use meilisearch_tokenizer::{Analyzer, AnalyzerConfig};
use milli::{Index, obkv_to_json, FacetCondition};
use serde::{Deserialize, Serialize};
use serde_json::{Value, Map};
use crate::index_controller::IndexController;
use super::Data;
@ -37,7 +37,6 @@ pub struct SearchQuery {
impl SearchQuery {
pub fn perform(&self, index: impl AsRef<Index>) -> anyhow::Result<SearchResult>{
let index = index.as_ref();
let before_search = Instant::now();
let rtxn = index.read_txn().unwrap();
@ -47,6 +46,9 @@ impl SearchQuery {
search.query(query);
}
search.limit(self.limit);
search.offset(self.offset.unwrap_or_default());
if let Some(ref condition) = self.facet_condition {
if !condition.trim().is_empty() {
let condition = FacetCondition::from_str(&rtxn, &index, &condition).unwrap();
@ -54,11 +56,7 @@ impl SearchQuery {
}
}
if let Some(offset) = self.offset {
search.offset(offset);
}
let milli::SearchResult { documents_ids, found_words, nb_hits, limit, } = search.execute()?;
let milli::SearchResult { documents_ids, found_words, candidates } = search.execute()?;
let mut documents = Vec::new();
let fields_ids_map = index.fields_ids_map(&rtxn).unwrap();
@ -81,9 +79,9 @@ impl SearchQuery {
Ok(SearchResult {
hits: documents,
nb_hits,
nb_hits: candidates.len(),
query: self.q.clone().unwrap_or_default(),
limit,
limit: self.limit,
offset: self.offset.unwrap_or_default(),
processing_time_ms: before_search.elapsed().as_millis(),
})
@ -94,7 +92,7 @@ impl SearchQuery {
#[serde(rename_all = "camelCase")]
pub struct SearchResult {
hits: Vec<Map<String, Value>>,
nb_hits: usize,
nb_hits: u64,
query: String,
limit: usize,
offset: usize,

View File

@ -6,21 +6,20 @@ use futures_util::stream::StreamExt;
use tokio::io::AsyncWriteExt;
use super::Data;
use crate::index_controller::{IndexController, Settings, UpdateResult, UpdateMeta};
use crate::index_controller::updates::UpdateStatus;
use crate::index_controller::{IndexController, Settings};
use crate::index_controller::UpdateStatus;
impl Data {
pub async fn add_documents<B, E, S>(
pub async fn add_documents<B, E>(
&self,
index: S,
index: impl AsRef<str> + Send + Sync + 'static,
method: IndexDocumentsMethod,
format: UpdateFormat,
mut stream: impl futures::Stream<Item=Result<B, E>> + Unpin,
) -> anyhow::Result<UpdateStatus<UpdateMeta, UpdateResult, String>>
) -> anyhow::Result<UpdateStatus>
where
B: Deref<Target = [u8]>,
E: std::error::Error + Send + Sync + 'static,
S: AsRef<str> + Send + Sync + 'static,
{
let file = tokio::task::spawn_blocking(tempfile::tempfile).await?;
let file = tokio::fs::File::from_std(file?);
@ -38,26 +37,26 @@ impl Data {
let mmap = unsafe { memmap::Mmap::map(&file)? };
let index_controller = self.index_controller.clone();
let update = tokio::task::spawn_blocking(move ||index_controller.add_documents(index, method, format, &mmap[..])).await??;
let update = tokio::task::spawn_blocking(move || index_controller.add_documents(index, method, format, &mmap[..])).await??;
Ok(update.into())
}
pub async fn update_settings<S: AsRef<str> + Send + Sync + 'static>(
pub async fn update_settings(
&self,
index: S,
index: impl AsRef<str> + Send + Sync + 'static,
settings: Settings
) -> anyhow::Result<UpdateStatus<UpdateMeta, UpdateResult, String>> {
let indexes = self.index_controller.clone();
let update = tokio::task::spawn_blocking(move || indexes.update_settings(index, settings)).await??;
) -> anyhow::Result<UpdateStatus> {
let index_controller = self.index_controller.clone();
let update = tokio::task::spawn_blocking(move || index_controller.update_settings(index, settings)).await??;
Ok(update.into())
}
#[inline]
pub fn get_update_status<S: AsRef<str>>(&self, index: S, uid: u64) -> anyhow::Result<Option<UpdateStatus<UpdateMeta, UpdateResult, String>>> {
pub fn get_update_status(&self, index: impl AsRef<str>, uid: u64) -> anyhow::Result<Option<UpdateStatus>> {
self.index_controller.update_status(index, uid)
}
pub fn get_updates_status(&self, index: &str) -> anyhow::Result<Vec<UpdateStatus<UpdateMeta, UpdateResult, String>>> {
pub fn get_updates_status(&self, index: impl AsRef<str>) -> anyhow::Result<Vec<UpdateStatus>> {
self.index_controller.all_update_status(index)
}
}