mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-22 21:04:27 +01:00
smarter field_id name passing
This commit is contained in:
parent
e5126af458
commit
347045adf2
@ -12,9 +12,9 @@ use crate::facets::FacetFilter;
|
|||||||
use either::Either;
|
use either::Either;
|
||||||
use sdset::SetOperation;
|
use sdset::SetOperation;
|
||||||
|
|
||||||
use meilisearch_schema::{Schema, FieldId};
|
use meilisearch_schema::FieldId;
|
||||||
|
|
||||||
pub struct QueryBuilder<'c, 'f, 'd, 'i, 'q> {
|
pub struct QueryBuilder<'c, 'f, 'd, 'i> {
|
||||||
criteria: Criteria<'c>,
|
criteria: Criteria<'c>,
|
||||||
searchable_attrs: Option<ReorderedAttrs>,
|
searchable_attrs: Option<ReorderedAttrs>,
|
||||||
filter: Option<Box<dyn Fn(DocumentId) -> bool + 'f>>,
|
filter: Option<Box<dyn Fn(DocumentId) -> bool + 'f>>,
|
||||||
@ -25,7 +25,7 @@ pub struct QueryBuilder<'c, 'f, 'd, 'i, 'q> {
|
|||||||
facets: Option<Vec<(FieldId, String)>>,
|
facets: Option<Vec<(FieldId, String)>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'c, 'f, 'd, 'i, 'q> QueryBuilder<'c, 'f, 'd, 'i, 'q> {
|
impl<'c, 'f, 'd, 'i> QueryBuilder<'c, 'f, 'd, 'i> {
|
||||||
pub fn new(index: &'i store::Index) -> Self {
|
pub fn new(index: &'i store::Index) -> Self {
|
||||||
QueryBuilder::with_criteria(
|
QueryBuilder::with_criteria(
|
||||||
index,
|
index,
|
||||||
@ -39,7 +39,7 @@ impl<'c, 'f, 'd, 'i, 'q> QueryBuilder<'c, 'f, 'd, 'i, 'q> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// sets facet attributes for which to return the count
|
/// sets facet attributes for which to return the count
|
||||||
pub fn set_facets(&mut self, facets: Option<&'q [FieldId]>) {
|
pub fn set_facets(&mut self, facets: Option<Vec<(FieldId, String)>>) {
|
||||||
self.facets = facets;
|
self.facets = facets;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,7 +87,6 @@ impl<'c, 'f, 'd, 'i, 'q> QueryBuilder<'c, 'f, 'd, 'i, 'q> {
|
|||||||
reader: &heed::RoTxn<MainT>,
|
reader: &heed::RoTxn<MainT>,
|
||||||
query: &str,
|
query: &str,
|
||||||
range: Range<usize>,
|
range: Range<usize>,
|
||||||
schema: &Schema,
|
|
||||||
) -> MResult<SortResult> {
|
) -> MResult<SortResult> {
|
||||||
let facets_docids = match self.facet_filter {
|
let facets_docids = match self.facet_filter {
|
||||||
Some(facets) => {
|
Some(facets) => {
|
||||||
@ -127,16 +126,14 @@ impl<'c, 'f, 'd, 'i, 'q> QueryBuilder<'c, 'f, 'd, 'i, 'q> {
|
|||||||
let facet_count_docids = match self.facets {
|
let facet_count_docids = match self.facets {
|
||||||
Some(field_ids) => {
|
Some(field_ids) => {
|
||||||
let mut facet_count_map = HashMap::new();
|
let mut facet_count_map = HashMap::new();
|
||||||
for field_id in field_ids {
|
for (field_id, field_name) in field_ids {
|
||||||
if let Some(field_name) = schema.name(*field_id) {
|
|
||||||
let mut key_map = HashMap::new();
|
let mut key_map = HashMap::new();
|
||||||
for pair in self.index.facets.field_document_ids(reader, *field_id)? {
|
for pair in self.index.facets.field_document_ids(reader, field_id)? {
|
||||||
let (facet_key, document_ids) = pair?;
|
let (facet_key, document_ids) = pair?;
|
||||||
let value = facet_key.value();
|
let value = facet_key.value();
|
||||||
key_map.insert(value.to_string(), document_ids);
|
key_map.insert(value.to_string(), document_ids);
|
||||||
}
|
}
|
||||||
facet_count_map.insert(field_name.to_string(), key_map);
|
facet_count_map.insert(field_name, key_map);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Some(facet_count_map)
|
Some(facet_count_map)
|
||||||
}
|
}
|
||||||
|
@ -366,7 +366,7 @@ impl Index {
|
|||||||
pub fn query_builder_with_criteria<'c, 'f, 'd, 'i>(
|
pub fn query_builder_with_criteria<'c, 'f, 'd, 'i>(
|
||||||
&'i self,
|
&'i self,
|
||||||
criteria: Criteria<'c>,
|
criteria: Criteria<'c>,
|
||||||
) -> QueryBuilder<'c, 'f, 'd, 'i, 'q> {
|
) -> QueryBuilder<'c, 'f, 'd, 'i> {
|
||||||
QueryBuilder::with_criteria(self, criteria)
|
QueryBuilder::with_criteria(self, criteria)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,7 +52,7 @@ pub struct SearchBuilder<'a> {
|
|||||||
filters: Option<String>,
|
filters: Option<String>,
|
||||||
matches: bool,
|
matches: bool,
|
||||||
facet_filters: Option<FacetFilter>,
|
facet_filters: Option<FacetFilter>,
|
||||||
facets: Option<Vec<FieldId>>
|
facets: Option<Vec<(FieldId, String)>>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> SearchBuilder<'a> {
|
impl<'a> SearchBuilder<'a> {
|
||||||
@ -102,12 +102,12 @@ impl<'a> SearchBuilder<'a> {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_facets(&mut self, facets: Vec<FieldId>) -> &SearchBuilder {
|
pub fn add_facets(&mut self, facets: Vec<(FieldId, String)>) -> &SearchBuilder {
|
||||||
self.facets = Some(facets);
|
self.facets = Some(facets);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn search(&self, reader: &heed::RoTxn<MainT>) -> Result<SearchResult, ResponseError> {
|
pub fn search(self, reader: &heed::RoTxn<MainT>) -> Result<SearchResult, ResponseError> {
|
||||||
let schema = self
|
let schema = self
|
||||||
.index
|
.index
|
||||||
.main
|
.main
|
||||||
@ -124,8 +124,8 @@ impl<'a> SearchBuilder<'a> {
|
|||||||
|
|
||||||
if let Some(filter_expression) = &self.filters {
|
if let Some(filter_expression) = &self.filters {
|
||||||
let filter = Filter::parse(filter_expression, &schema)?;
|
let filter = Filter::parse(filter_expression, &schema)?;
|
||||||
query_builder.with_filter(move |id| {
|
|
||||||
let index = &self.index;
|
let index = &self.index;
|
||||||
|
query_builder.with_filter(move |id| {
|
||||||
let reader = &reader;
|
let reader = &reader;
|
||||||
let filter = &filter;
|
let filter = &filter;
|
||||||
match filter.test(reader, index, id) {
|
match filter.test(reader, index, id) {
|
||||||
@ -140,8 +140,9 @@ impl<'a> SearchBuilder<'a> {
|
|||||||
|
|
||||||
if let Some(field) = self.index.main.distinct_attribute(reader)? {
|
if let Some(field) = self.index.main.distinct_attribute(reader)? {
|
||||||
if let Some(field_id) = schema.id(&field) {
|
if let Some(field_id) = schema.id(&field) {
|
||||||
|
let index = &self.index;
|
||||||
query_builder.with_distinct(1, move |id| {
|
query_builder.with_distinct(1, move |id| {
|
||||||
match self.index.document_attribute_bytes(reader, id, field_id) {
|
match index.document_attribute_bytes(reader, id, field_id) {
|
||||||
Ok(Some(bytes)) => {
|
Ok(Some(bytes)) => {
|
||||||
let mut s = SipHasher::new();
|
let mut s = SipHasher::new();
|
||||||
bytes.hash(&mut s);
|
bytes.hash(&mut s);
|
||||||
@ -153,11 +154,11 @@ impl<'a> SearchBuilder<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
query_builder.set_facet_filters(self.facet_filters.as_ref());
|
query_builder.set_facet_filters(self.facet_filters);
|
||||||
query_builder.set_facets(self.facets.as_deref());
|
query_builder.set_facets(self.facets);
|
||||||
|
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let result = query_builder.query(reader, &self.query, self.offset..(self.offset + self.limit), &schema);
|
let result = query_builder.query(reader, &self.query, self.offset..(self.offset + self.limit));
|
||||||
let search_result = result.map_err(ResponseError::search_documents)?;
|
let search_result = result.map_err(ResponseError::search_documents)?;
|
||||||
let time_ms = start.elapsed().as_millis() as usize;
|
let time_ms = start.elapsed().as_millis() as usize;
|
||||||
|
|
||||||
|
@ -176,9 +176,13 @@ fn prepare_facet_list(facets: &str, schema: &Schema, facet_attrs: &[FieldId]) ->
|
|||||||
Value::Array(vals) => {
|
Value::Array(vals) => {
|
||||||
let wildcard = Value::String("*".to_string());
|
let wildcard = Value::String("*".to_string());
|
||||||
if vals.iter().any(|f| f == &wildcard) {
|
if vals.iter().any(|f| f == &wildcard) {
|
||||||
return Ok(Vec::from(facet_attrs));
|
let attrs = facet_attrs
|
||||||
|
.iter()
|
||||||
|
.filter_map(|&id| schema.name(id).map(|n| (id, n.to_string())))
|
||||||
|
.collect();
|
||||||
|
return Ok(attrs);
|
||||||
}
|
}
|
||||||
let mut field_ids = Vec::new();
|
let mut field_ids = Vec::with_capacity(facet_attrs.len());
|
||||||
for facet in vals {
|
for facet in vals {
|
||||||
match facet {
|
match facet {
|
||||||
Value::String(facet) => {
|
Value::String(facet) => {
|
||||||
@ -186,7 +190,7 @@ fn prepare_facet_list(facets: &str, schema: &Schema, facet_attrs: &[FieldId]) ->
|
|||||||
if !facet_attrs.contains(&id) {
|
if !facet_attrs.contains(&id) {
|
||||||
return Err(ResponseError::FacetExpression("Only attributes set as facet can be counted".to_string())); // TODO make special error
|
return Err(ResponseError::FacetExpression("Only attributes set as facet can be counted".to_string())); // TODO make special error
|
||||||
}
|
}
|
||||||
field_ids.push(id);
|
field_ids.push((id, facet));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bad_val => return Err(ResponseError::FacetExpression(format!("expected String found {}", bad_val)))
|
bad_val => return Err(ResponseError::FacetExpression(format!("expected String found {}", bad_val)))
|
||||||
|
Loading…
Reference in New Issue
Block a user