2021-03-16 01:11:10 +08:00
|
|
|
use std::collections::{BTreeMap, HashSet};
|
2021-03-04 18:56:32 +08:00
|
|
|
use std::mem;
|
2021-03-16 01:11:10 +08:00
|
|
|
use std::time::Instant;
|
2021-03-04 18:56:32 +08:00
|
|
|
|
|
|
|
use anyhow::bail;
|
2021-03-16 01:11:10 +08:00
|
|
|
use either::Either;
|
2021-03-04 18:56:32 +08:00
|
|
|
use heed::RoTxn;
|
|
|
|
use meilisearch_tokenizer::{Analyzer, AnalyzerConfig};
|
2021-03-16 01:11:10 +08:00
|
|
|
use milli::{facet::FacetValue, FacetCondition, MatchingWords};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use serde_json::{Map, Value};
|
2021-03-04 18:56:32 +08:00
|
|
|
|
|
|
|
use super::Index;
|
|
|
|
|
|
|
|
pub const DEFAULT_SEARCH_LIMIT: usize = 20;
|
|
|
|
|
|
|
|
const fn default_search_limit() -> usize {
|
|
|
|
DEFAULT_SEARCH_LIMIT
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
pub struct SearchQuery {
|
|
|
|
pub q: Option<String>,
|
|
|
|
pub offset: Option<usize>,
|
|
|
|
#[serde(default = "default_search_limit")]
|
|
|
|
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<HashSet<String>>,
|
|
|
|
pub filters: Option<String>,
|
|
|
|
pub matches: Option<bool>,
|
|
|
|
pub facet_filters: Option<Value>,
|
|
|
|
pub facet_distributions: Option<Vec<String>>,
|
|
|
|
}
|
|
|
|
|
2021-04-19 16:13:13 +08:00
|
|
|
#[derive(Debug, Clone, Serialize)]
|
|
|
|
pub struct SearchHit {
|
|
|
|
#[serde(flatten)]
|
|
|
|
pub document: Map<String, Value>,
|
|
|
|
#[serde(rename = "_formatted", skip_serializing_if = "Map::is_empty")]
|
|
|
|
pub formatted: Map<String, Value>,
|
|
|
|
}
|
|
|
|
|
2021-03-04 18:56:32 +08:00
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct SearchResult {
|
2021-04-19 16:13:13 +08:00
|
|
|
pub hits: Vec<SearchHit>,
|
2021-03-04 18:56:32 +08:00
|
|
|
pub nb_hits: u64,
|
2021-03-24 02:13:22 +08:00
|
|
|
pub exhaustive_nb_hits: bool,
|
2021-03-04 18:56:32 +08:00
|
|
|
pub query: String,
|
|
|
|
pub limit: usize,
|
|
|
|
pub offset: usize,
|
|
|
|
pub processing_time_ms: u128,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub facet_distributions: Option<BTreeMap<String, BTreeMap<FacetValue, u64>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Index {
|
|
|
|
pub fn perform_search(&self, query: SearchQuery) -> anyhow::Result<SearchResult> {
|
|
|
|
let before_search = Instant::now();
|
|
|
|
let rtxn = self.read_txn()?;
|
|
|
|
|
|
|
|
let mut search = self.search(&rtxn);
|
|
|
|
|
|
|
|
if let Some(ref query) = query.q {
|
|
|
|
search.query(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
search.limit(query.limit);
|
|
|
|
search.offset(query.offset.unwrap_or_default());
|
|
|
|
|
|
|
|
if let Some(ref facets) = query.facet_filters {
|
|
|
|
if let Some(facets) = parse_facets(facets, self, &rtxn)? {
|
|
|
|
search.facet_condition(facets);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let milli::SearchResult {
|
|
|
|
documents_ids,
|
2021-03-12 02:40:18 +08:00
|
|
|
matching_words,
|
2021-03-04 18:56:32 +08:00
|
|
|
candidates,
|
|
|
|
..
|
|
|
|
} = search.execute()?;
|
|
|
|
let mut documents = Vec::new();
|
|
|
|
let fields_ids_map = self.fields_ids_map(&rtxn).unwrap();
|
|
|
|
|
2021-03-16 01:11:10 +08:00
|
|
|
let fields_to_display =
|
|
|
|
self.fields_to_display(&rtxn, query.attributes_to_retrieve, &fields_ids_map)?;
|
2021-03-04 18:56:32 +08:00
|
|
|
|
|
|
|
let stop_words = fst::Set::default();
|
|
|
|
let highlighter = Highlighter::new(&stop_words);
|
|
|
|
|
|
|
|
for (_id, obkv) in self.documents(&rtxn, documents_ids)? {
|
2021-03-16 01:11:10 +08:00
|
|
|
let mut object =
|
|
|
|
milli::obkv_to_json(&fields_to_display, &fields_ids_map, obkv).unwrap();
|
2021-03-04 18:56:32 +08:00
|
|
|
if let Some(ref attributes_to_highlight) = query.attributes_to_highlight {
|
2021-03-12 02:40:18 +08:00
|
|
|
highlighter.highlight_record(&mut object, &matching_words, attributes_to_highlight);
|
2021-03-04 18:56:32 +08:00
|
|
|
}
|
2021-04-19 16:13:13 +08:00
|
|
|
let hit = SearchHit {
|
|
|
|
document: object,
|
|
|
|
formatted: Map::new(),
|
|
|
|
};
|
|
|
|
documents.push(hit);
|
2021-03-04 18:56:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let nb_hits = candidates.len();
|
|
|
|
|
|
|
|
let facet_distributions = match query.facet_distributions {
|
|
|
|
Some(ref fields) => {
|
|
|
|
let mut facet_distribution = self.facets_distribution(&rtxn);
|
|
|
|
if fields.iter().all(|f| f != "*") {
|
|
|
|
facet_distribution.facets(fields);
|
|
|
|
}
|
|
|
|
Some(facet_distribution.candidates(candidates).execute()?)
|
|
|
|
}
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let result = SearchResult {
|
2021-03-23 17:47:19 +08:00
|
|
|
exhaustive_nb_hits: false, // not implemented yet
|
2021-03-04 18:56:32 +08:00
|
|
|
hits: documents,
|
|
|
|
nb_hits,
|
|
|
|
query: query.q.clone().unwrap_or_default(),
|
|
|
|
limit: query.limit,
|
|
|
|
offset: query.offset.unwrap_or_default(),
|
|
|
|
processing_time_ms: before_search.elapsed().as_millis(),
|
|
|
|
facet_distributions,
|
|
|
|
};
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_facets_array(
|
|
|
|
txn: &RoTxn,
|
|
|
|
index: &Index,
|
2021-03-15 23:52:05 +08:00
|
|
|
arr: &[Value],
|
2021-03-04 18:56:32 +08:00
|
|
|
) -> anyhow::Result<Option<FacetCondition>> {
|
|
|
|
let mut ands = Vec::new();
|
|
|
|
for value in arr {
|
|
|
|
match value {
|
|
|
|
Value::String(s) => ands.push(Either::Right(s.clone())),
|
|
|
|
Value::Array(arr) => {
|
|
|
|
let mut ors = Vec::new();
|
|
|
|
for value in arr {
|
|
|
|
match value {
|
|
|
|
Value::String(s) => ors.push(s.clone()),
|
|
|
|
v => bail!("Invalid facet expression, expected String, found: {:?}", v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ands.push(Either::Left(ors));
|
|
|
|
}
|
|
|
|
v => bail!(
|
|
|
|
"Invalid facet expression, expected String or [String], found: {:?}",
|
|
|
|
v
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FacetCondition::from_array(txn, &index.0, ands)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Highlighter<'a, A> {
|
|
|
|
analyzer: Analyzer<'a, A>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, A: AsRef<[u8]>> Highlighter<'a, A> {
|
|
|
|
pub fn new(stop_words: &'a fst::Set<A>) -> Self {
|
2021-04-01 22:49:11 +08:00
|
|
|
let mut config = AnalyzerConfig::default();
|
|
|
|
config.stop_words(stop_words);
|
|
|
|
|
|
|
|
let analyzer = Analyzer::new(config);
|
2021-03-04 18:56:32 +08:00
|
|
|
|
|
|
|
Self { analyzer }
|
|
|
|
}
|
|
|
|
|
2021-03-12 02:40:18 +08:00
|
|
|
pub fn highlight_value(&self, value: Value, words_to_highlight: &MatchingWords) -> Value {
|
2021-03-04 18:56:32 +08:00
|
|
|
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() {
|
2021-03-12 02:40:18 +08:00
|
|
|
let to_highlight = words_to_highlight.matches(token.text());
|
2021-03-04 18:56:32 +08:00
|
|
|
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
|
2021-03-16 01:11:10 +08:00
|
|
|
.into_iter()
|
|
|
|
.map(|v| self.highlight_value(v, words_to_highlight))
|
|
|
|
.collect(),
|
2021-03-04 18:56:32 +08:00
|
|
|
),
|
|
|
|
Value::Object(object) => Value::Object(
|
|
|
|
object
|
2021-03-16 01:11:10 +08:00
|
|
|
.into_iter()
|
|
|
|
.map(|(k, v)| (k, self.highlight_value(v, words_to_highlight)))
|
|
|
|
.collect(),
|
2021-03-04 18:56:32 +08:00
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn highlight_record(
|
|
|
|
&self,
|
|
|
|
object: &mut Map<String, Value>,
|
2021-03-12 02:40:18 +08:00
|
|
|
words_to_highlight: &MatchingWords,
|
2021-03-04 18:56:32 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_facets(
|
|
|
|
facets: &Value,
|
|
|
|
index: &Index,
|
|
|
|
txn: &RoTxn,
|
|
|
|
) -> anyhow::Result<Option<FacetCondition>> {
|
|
|
|
match facets {
|
|
|
|
// Disabled for now
|
|
|
|
//Value::String(expr) => Ok(Some(FacetCondition::from_str(txn, index, expr)?)),
|
|
|
|
Value::Array(arr) => parse_facets_array(txn, index, arr),
|
2021-03-16 01:11:10 +08:00
|
|
|
v => bail!("Invalid facet expression, expected Array, found: {:?}", v),
|
2021-03-04 18:56:32 +08:00
|
|
|
}
|
|
|
|
}
|