WIP: IndexController

This commit is contained in:
mpostma 2021-01-13 17:50:36 +01:00
parent b07e21ab3c
commit ddd7789713
No known key found for this signature in database
GPG key ID: CBC8A7C1D7A28C3A
6 changed files with 230 additions and 55 deletions

View file

@ -3,7 +3,6 @@ mod updates;
pub use search::{SearchQuery, SearchResult};
use std::collections::HashMap;
use std::fs::create_dir_all;
use std::ops::Deref;
use std::sync::Arc;
@ -13,6 +12,7 @@ use sha2::Digest;
use crate::{option::Opt, updates::Settings};
use crate::updates::UpdateQueue;
use crate::index_controller::IndexController;
#[derive(Clone)]
pub struct Data {
@ -29,7 +29,7 @@ impl Deref for Data {
#[derive(Clone)]
pub struct DataInner {
pub indexes: Arc<Index>,
pub indexes: Arc<IndexController>,
pub update_queue: Arc<UpdateQueue>,
api_keys: ApiKeys,
options: Opt,
@ -62,9 +62,7 @@ impl ApiKeys {
impl Data {
pub fn new(options: Opt) -> anyhow::Result<Data> {
let db_size = options.max_mdb_size.get_bytes() as usize;
let path = options.db_path.join("main");
create_dir_all(&path)?;
let indexes = Index::new(&path, Some(db_size))?;
let indexes = IndexController::new(&options.db_path)?;
let indexes = Arc::new(indexes);
let update_queue = Arc::new(UpdateQueue::new(&options, indexes.clone())?);
@ -90,28 +88,26 @@ impl Data {
let displayed_attributes = self.indexes
.displayed_fields(&txn)?
.map(|fields| {println!("{:?}", fields); fields.iter().filter_map(|f| fields_map.name(*f).map(String::from)).collect()})
.map(|fields| fields.into_iter().map(String::from).collect())
.unwrap_or_else(|| vec!["*".to_string()]);
let searchable_attributes = self.indexes
.searchable_fields(&txn)?
.map(|fields| fields
.iter()
.filter_map(|f| fields_map.name(*f).map(String::from))
.into_iter()
.map(String::from)
.collect())
.unwrap_or_else(|| vec!["*".to_string()]);
let faceted_attributes = self.indexes
.faceted_fields(&txn)?
.iter()
.filter_map(|(f, t)| Some((fields_map.name(*f)?.to_string(), t.to_string())))
.collect::<HashMap<_, _>>()
.into();
let faceted_attributes = self.indexes.faceted_fields(&txn)?
.into_iter()
.map(|(k, v)| (k, v.to_string()))
.collect();
Ok(Settings {
displayed_attributes: Some(Some(displayed_attributes)),
searchable_attributes: Some(Some(searchable_attributes)),
faceted_attributes: Some(faceted_attributes),
faceted_attributes: Some(Some(faceted_attributes)),
criteria: None,
})
}

View file

@ -1,4 +1,3 @@
use std::borrow::Cow;
use std::collections::HashSet;
use std::mem;
use std::time::Instant;
@ -8,17 +7,22 @@ use serde::{Deserialize, Serialize};
use milli::{SearchResult as Results, obkv_to_json};
use meilisearch_tokenizer::{Analyzer, AnalyzerConfig};
use crate::error::Error;
use super::Data;
const DEFAULT_SEARCH_LIMIT: usize = 20;
const fn default_search_limit() -> usize { DEFAULT_SEARCH_LIMIT }
#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[allow(dead_code)]
pub struct SearchQuery {
q: Option<String>,
offset: Option<usize>,
limit: Option<usize>,
#[serde(default = "default_search_limit")]
limit: usize,
attributes_to_retrieve: Option<Vec<String>>,
attributes_to_crop: Option<Vec<String>>,
crop_length: Option<usize>,
@ -100,30 +104,18 @@ impl<'a, A: AsRef<[u8]>> Highlighter<'a, A> {
}
impl Data {
pub fn search<S: AsRef<str>>(&self, _index: S, search_query: SearchQuery) -> anyhow::Result<SearchResult> {
pub fn search<S: AsRef<str>>(&self, index: S, search_query: SearchQuery) -> anyhow::Result<SearchResult> {
let start = Instant::now();
let index = &self.indexes;
let rtxn = index.read_txn()?;
let mut search = index.search(&rtxn);
if let Some(query) = &search_query.q {
search.query(query);
}
if let Some(offset) = search_query.offset {
search.offset(offset);
}
let limit = search_query.limit.unwrap_or(DEFAULT_SEARCH_LIMIT);
search.limit(limit);
let Results { found_words, documents_ids, nb_hits, .. } = search.execute().unwrap();
let index = self.indexes
.get(index)?
.ok_or_else(|| Error::OpenIndex(format!("Index {} doesn't exists.", index.as_ref())))?;
let Results { found_words, documents_ids, nb_hits, .. } = index.search(search_query)?;
let fields_ids_map = index.fields_ids_map(&rtxn).unwrap();
let displayed_fields = match index.displayed_fields(&rtxn).unwrap() {
Some(fields) => Cow::Borrowed(fields),
None => Cow::Owned(fields_ids_map.iter().map(|(id, _)| id).collect()),
let displayed_fields = match index.displayed_fields_ids(&rtxn).unwrap() {
Some(fields) => fields,
None => fields_ids_map.iter().map(|(id, _)| id).collect(),
};
let attributes_to_highlight = match search_query.attributes_to_highlight {