mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-30 00:34:26 +01:00
Merge pull request #45 from meilisearch/facet-distributions
facets distribution
This commit is contained in:
commit
a7bd0681a0
@ -1,4 +1,4 @@
|
|||||||
use std::collections::HashSet;
|
use std::collections::{HashSet, BTreeMap};
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
@ -6,7 +6,7 @@ use anyhow::{bail, Context};
|
|||||||
use either::Either;
|
use either::Either;
|
||||||
use heed::RoTxn;
|
use heed::RoTxn;
|
||||||
use meilisearch_tokenizer::{Analyzer, AnalyzerConfig};
|
use meilisearch_tokenizer::{Analyzer, AnalyzerConfig};
|
||||||
use milli::{obkv_to_json, FacetCondition, Index};
|
use milli::{obkv_to_json, FacetCondition, Index, facet::FacetValue};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::{Map, Value};
|
use serde_json::{Map, Value};
|
||||||
|
|
||||||
@ -34,7 +34,7 @@ pub struct SearchQuery {
|
|||||||
pub filters: Option<String>,
|
pub filters: Option<String>,
|
||||||
pub matches: Option<bool>,
|
pub matches: Option<bool>,
|
||||||
pub facet_filters: Option<Value>,
|
pub facet_filters: Option<Value>,
|
||||||
pub facets_distribution: Option<Vec<String>>,
|
pub facet_distributions: Option<Vec<String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SearchQuery {
|
impl SearchQuery {
|
||||||
@ -96,13 +96,27 @@ impl SearchQuery {
|
|||||||
documents.push(object);
|
documents.push(object);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let nb_hits = candidates.len();
|
||||||
|
|
||||||
|
let facet_distributions = match self.facet_distributions {
|
||||||
|
Some(ref fields) => {
|
||||||
|
let mut facet_distribution = index.facets_distribution(&rtxn);
|
||||||
|
if fields.iter().all(|f| f != "*") {
|
||||||
|
facet_distribution.facets(fields);
|
||||||
|
}
|
||||||
|
Some(facet_distribution.candidates(candidates).execute()?)
|
||||||
|
}
|
||||||
|
None => None,
|
||||||
|
};
|
||||||
|
|
||||||
Ok(SearchResult {
|
Ok(SearchResult {
|
||||||
hits: documents,
|
hits: documents,
|
||||||
nb_hits: candidates.len(),
|
nb_hits,
|
||||||
query: self.q.clone().unwrap_or_default(),
|
query: self.q.clone().unwrap_or_default(),
|
||||||
limit: self.limit,
|
limit: self.limit,
|
||||||
offset: self.offset.unwrap_or_default(),
|
offset: self.offset.unwrap_or_default(),
|
||||||
processing_time_ms: before_search.elapsed().as_millis(),
|
processing_time_ms: before_search.elapsed().as_millis(),
|
||||||
|
facet_distributions,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -116,6 +130,8 @@ pub struct SearchResult {
|
|||||||
limit: usize,
|
limit: usize,
|
||||||
offset: usize,
|
offset: usize,
|
||||||
processing_time_ms: u128,
|
processing_time_ms: u128,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
facet_distributions: Option<BTreeMap<String, BTreeMap<FacetValue, u64>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Highlighter<'a, A> {
|
struct Highlighter<'a, A> {
|
||||||
|
@ -27,7 +27,7 @@ pub struct SearchQueryGet {
|
|||||||
filters: Option<String>,
|
filters: Option<String>,
|
||||||
matches: Option<bool>,
|
matches: Option<bool>,
|
||||||
facet_filters: Option<String>,
|
facet_filters: Option<String>,
|
||||||
facets_distribution: Option<String>,
|
facet_distributions: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<SearchQueryGet> for SearchQuery {
|
impl TryFrom<SearchQueryGet> for SearchQuery {
|
||||||
@ -46,8 +46,8 @@ impl TryFrom<SearchQueryGet> for SearchQuery {
|
|||||||
.attributes_to_highlight
|
.attributes_to_highlight
|
||||||
.map(|attrs| attrs.split(",").map(String::from).collect::<HashSet<_>>());
|
.map(|attrs| attrs.split(",").map(String::from).collect::<HashSet<_>>());
|
||||||
|
|
||||||
let facets_distribution = other
|
let facet_distributions = other
|
||||||
.facets_distribution
|
.facet_distributions
|
||||||
.map(|attrs| attrs.split(",").map(String::from).collect::<Vec<_>>());
|
.map(|attrs| attrs.split(",").map(String::from).collect::<Vec<_>>());
|
||||||
|
|
||||||
let facet_filters = match other.facet_filters {
|
let facet_filters = match other.facet_filters {
|
||||||
@ -66,7 +66,7 @@ impl TryFrom<SearchQueryGet> for SearchQuery {
|
|||||||
filters: other.filters,
|
filters: other.filters,
|
||||||
matches: other.matches,
|
matches: other.matches,
|
||||||
facet_filters,
|
facet_filters,
|
||||||
facets_distribution,
|
facet_distributions,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user