2020-12-24 19:58:34 +08:00
|
|
|
use std::collections::HashSet;
|
|
|
|
use std::mem;
|
|
|
|
use std::time::Instant;
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2020-12-24 19:58:34 +08:00
|
|
|
use serde_json::{Value, Map};
|
|
|
|
use serde::{Deserialize, Serialize};
|
2020-12-29 18:11:06 +08:00
|
|
|
use milli::{SearchResult as Results, obkv_to_json};
|
2020-12-24 19:58:34 +08:00
|
|
|
use meilisearch_tokenizer::{Analyzer, AnalyzerConfig};
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2021-01-14 00:50:36 +08:00
|
|
|
use crate::error::Error;
|
|
|
|
|
2020-12-29 18:11:06 +08:00
|
|
|
use super::Data;
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2020-12-24 19:58:34 +08:00
|
|
|
const DEFAULT_SEARCH_LIMIT: usize = 20;
|
|
|
|
|
2021-01-14 00:50:36 +08:00
|
|
|
const fn default_search_limit() -> usize { DEFAULT_SEARCH_LIMIT }
|
|
|
|
|
2020-12-24 19:58:34 +08:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
2020-12-29 18:11:06 +08:00
|
|
|
#[allow(dead_code)]
|
2020-12-24 19:58:34 +08:00
|
|
|
pub struct SearchQuery {
|
2021-01-14 18:27:07 +08:00
|
|
|
pub q: Option<String>,
|
|
|
|
pub offset: Option<usize>,
|
2021-01-14 00:50:36 +08:00
|
|
|
#[serde(default = "default_search_limit")]
|
2021-01-14 18:27:07 +08:00
|
|
|
pub limit: usize,
|
|
|
|
pub attributes_to_retrieve: Option<Vec<String>>,
|
|
|
|
pub attributes_to_crop: Option<Vec<String>>,
|
|
|
|
pub crop_length: Option<usize>,
|
|
|
|
pub attributes_to_highlight: Option<Vec<String>>,
|
|
|
|
pub filters: Option<String>,
|
|
|
|
pub matches: Option<bool>,
|
|
|
|
pub facet_filters: Option<Value>,
|
|
|
|
pub facets_distribution: Option<Vec<String>>,
|
2020-12-24 19:58:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct SearchResult {
|
|
|
|
hits: Vec<Map<String, Value>>,
|
|
|
|
nb_hits: usize,
|
|
|
|
query: String,
|
|
|
|
limit: usize,
|
|
|
|
offset: usize,
|
|
|
|
processing_time_ms: u128,
|
|
|
|
}
|
|
|
|
|
2020-12-29 18:11:06 +08:00
|
|
|
struct Highlighter<'a, A> {
|
|
|
|
analyzer: Analyzer<'a, A>,
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
2020-12-29 18:11:06 +08:00
|
|
|
impl<'a, A: AsRef<[u8]>> Highlighter<'a, A> {
|
|
|
|
fn new(stop_words: &'a fst::Set<A>) -> Self {
|
|
|
|
let analyzer = Analyzer::new(AnalyzerConfig::default_with_stopwords(stop_words));
|
|
|
|
Self { analyzer }
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
2020-12-29 18:11:06 +08:00
|
|
|
fn highlight_value(&self, value: Value, words_to_highlight: &HashSet<String>) -> Value {
|
|
|
|
match value {
|
|
|
|
Value::Null => Value::Null,
|
|
|
|
Value::Bool(boolean) => Value::Bool(boolean),
|
|
|
|
Value::Number(number) => Value::Number(number),
|
|
|
|
Value::String(old_string) => {
|
|
|
|
let mut string = String::new();
|
|
|
|
let analyzed = self.analyzer.analyze(&old_string);
|
|
|
|
for (word, token) in analyzed.reconstruct() {
|
|
|
|
if token.is_word() {
|
|
|
|
let to_highlight = words_to_highlight.contains(token.text());
|
|
|
|
if to_highlight { string.push_str("<mark>") }
|
|
|
|
string.push_str(word);
|
|
|
|
if to_highlight { string.push_str("</mark>") }
|
|
|
|
} else {
|
|
|
|
string.push_str(word);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Value::String(string)
|
|
|
|
},
|
|
|
|
Value::Array(values) => {
|
|
|
|
Value::Array(values.into_iter()
|
|
|
|
.map(|v| self.highlight_value(v, words_to_highlight))
|
|
|
|
.collect())
|
|
|
|
},
|
|
|
|
Value::Object(object) => {
|
|
|
|
Value::Object(object.into_iter()
|
|
|
|
.map(|(k, v)| (k, self.highlight_value(v, words_to_highlight)))
|
|
|
|
.collect())
|
|
|
|
},
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-23 00:53:13 +08:00
|
|
|
|
2020-12-29 18:11:06 +08:00
|
|
|
fn highlight_record(
|
2020-12-23 20:52:28 +08:00
|
|
|
&self,
|
2020-12-29 18:11:06 +08:00
|
|
|
object: &mut Map<String, Value>,
|
|
|
|
words_to_highlight: &HashSet<String>,
|
|
|
|
attributes_to_highlight: &HashSet<String>,
|
|
|
|
) {
|
|
|
|
// TODO do we need to create a string for element that are not and needs to be highlight?
|
|
|
|
for (key, value) in object.iter_mut() {
|
|
|
|
if attributes_to_highlight.contains(key) {
|
|
|
|
let old_value = mem::take(value);
|
|
|
|
*value = self.highlight_value(old_value, words_to_highlight);
|
|
|
|
}
|
2020-12-23 20:52:28 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-29 18:11:06 +08:00
|
|
|
}
|
2020-12-23 20:52:28 +08:00
|
|
|
|
2020-12-29 18:11:06 +08:00
|
|
|
impl Data {
|
2021-01-14 00:50:36 +08:00
|
|
|
pub fn search<S: AsRef<str>>(&self, index: S, search_query: SearchQuery) -> anyhow::Result<SearchResult> {
|
2020-12-24 19:58:34 +08:00
|
|
|
let start = Instant::now();
|
2021-01-14 00:50:36 +08:00
|
|
|
let index = self.indexes
|
2021-01-16 22:09:48 +08:00
|
|
|
.get(&index)?
|
2021-01-14 00:50:36 +08:00
|
|
|
.ok_or_else(|| Error::OpenIndex(format!("Index {} doesn't exists.", index.as_ref())))?;
|
2020-12-24 19:58:34 +08:00
|
|
|
|
2021-01-16 22:09:48 +08:00
|
|
|
let Results { found_words, documents_ids, nb_hits, limit, .. } = index.search(&search_query)?;
|
2020-12-24 19:58:34 +08:00
|
|
|
|
2021-01-14 01:29:17 +08:00
|
|
|
let fields_ids_map = index.fields_ids_map()?;
|
|
|
|
|
|
|
|
let displayed_fields = match index.displayed_fields_ids()? {
|
2021-01-14 00:50:36 +08:00
|
|
|
Some(fields) => fields,
|
|
|
|
None => fields_ids_map.iter().map(|(id, _)| id).collect(),
|
2020-12-24 19:58:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
let attributes_to_highlight = match search_query.attributes_to_highlight {
|
|
|
|
Some(fields) => fields.iter().map(ToOwned::to_owned).collect(),
|
|
|
|
None => HashSet::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let stop_words = fst::Set::default();
|
|
|
|
let highlighter = Highlighter::new(&stop_words);
|
|
|
|
let mut documents = Vec::new();
|
2021-01-14 01:29:17 +08:00
|
|
|
for (_id, obkv) in index.documents(&documents_ids)? {
|
2020-12-24 19:58:34 +08:00
|
|
|
let mut object = obkv_to_json(&displayed_fields, &fields_ids_map, obkv).unwrap();
|
|
|
|
highlighter.highlight_record(&mut object, &found_words, &attributes_to_highlight);
|
|
|
|
documents.push(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
let processing_time_ms = start.elapsed().as_millis();
|
|
|
|
|
|
|
|
let result = SearchResult {
|
|
|
|
hits: documents,
|
|
|
|
nb_hits,
|
|
|
|
query: search_query.q.unwrap_or_default(),
|
|
|
|
offset: search_query.offset.unwrap_or(0),
|
|
|
|
limit,
|
|
|
|
processing_time_ms,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
}
|