mirror of
https://github.com/meilisearch/MeiliSearch
synced 2025-07-04 12:27:13 +02:00
add sort result struct
This commit is contained in:
parent
a88f6c3241
commit
effbb7f7f1
7 changed files with 194 additions and 69 deletions
|
@ -36,6 +36,7 @@ impl IndexSearchExt for Index {
|
|||
filters: None,
|
||||
matches: false,
|
||||
facet_filters: None,
|
||||
facets: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -51,6 +52,7 @@ pub struct SearchBuilder<'a> {
|
|||
filters: Option<String>,
|
||||
matches: bool,
|
||||
facet_filters: Option<FacetFilter>,
|
||||
facets: Option<Vec<FieldId>>
|
||||
}
|
||||
|
||||
impl<'a> SearchBuilder<'a> {
|
||||
|
@ -100,6 +102,11 @@ impl<'a> SearchBuilder<'a> {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn add_facets(&mut self, facets: Vec<FieldId>) -> &SearchBuilder {
|
||||
self.facets = Some(facets);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn search(&self, reader: &heed::RoTxn<MainT>) -> Result<SearchResult, ResponseError> {
|
||||
let schema = self
|
||||
.index
|
||||
|
@ -146,11 +153,12 @@ impl<'a> SearchBuilder<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
query_builder.set_facets(self.facet_filters.as_ref());
|
||||
query_builder.set_facet_filters(self.facet_filters.as_ref());
|
||||
query_builder.set_facets(self.facets.as_deref());
|
||||
|
||||
let start = Instant::now();
|
||||
let result = query_builder.query(reader, &self.query, self.offset..(self.offset + self.limit));
|
||||
let (docs, nb_hits) = 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 mut all_attributes: HashSet<&str> = HashSet::new();
|
||||
|
@ -181,7 +189,7 @@ impl<'a> SearchBuilder<'a> {
|
|||
}
|
||||
|
||||
let mut hits = Vec::with_capacity(self.limit);
|
||||
for doc in docs {
|
||||
for doc in search_result.documents {
|
||||
let mut document: IndexMap<String, Value> = self
|
||||
.index
|
||||
.document(reader, Some(&all_attributes), doc.id)
|
||||
|
@ -235,10 +243,11 @@ impl<'a> SearchBuilder<'a> {
|
|||
hits,
|
||||
offset: self.offset,
|
||||
limit: self.limit,
|
||||
nb_hits,
|
||||
exhaustive_nb_hits: false,
|
||||
nb_hits: search_result.nb_hits,
|
||||
exhaustive_nb_hits: search_result.is_exhaustive,
|
||||
processing_time_ms: time_ms,
|
||||
query: self.query.to_string(),
|
||||
facets: search_result.facets
|
||||
};
|
||||
|
||||
Ok(results)
|
||||
|
|
|
@ -5,6 +5,7 @@ use actix_web::web;
|
|||
use actix_web::HttpResponse;
|
||||
use actix_web_macros::get;
|
||||
use serde::Deserialize;
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::error::ResponseError;
|
||||
use crate::helpers::meilisearch::IndexSearchExt;
|
||||
|
@ -13,6 +14,7 @@ use crate::routes::IndexParam;
|
|||
use crate::Data;
|
||||
|
||||
use meilisearch_core::facets::FacetFilter;
|
||||
use meilisearch_schema::{Schema, FieldId};
|
||||
|
||||
pub fn services(cfg: &mut web::ServiceConfig) {
|
||||
cfg.service(search_with_url_query);
|
||||
|
@ -31,6 +33,7 @@ struct SearchQuery {
|
|||
filters: Option<String>,
|
||||
matches: Option<bool>,
|
||||
facet_filters: Option<String>,
|
||||
facets: Option<String>
|
||||
}
|
||||
|
||||
#[get("/indexes/{index_uid}/search", wrap = "Authentication::Public")]
|
||||
|
@ -91,6 +94,14 @@ async fn search_with_url_query(
|
|||
}
|
||||
}
|
||||
|
||||
if let Some(ref facets) = params.facets {
|
||||
match index.main.attributes_for_faceting(&reader)? {
|
||||
Some(ref attrs) => { search_builder.add_facets(prepare_facet_list(facets, &schema, attrs)?); },
|
||||
None => return Err(ResponseError::FacetExpression("can't return facets count, as no facet is set".to_string()))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if let Some(attributes_to_crop) = ¶ms.attributes_to_crop {
|
||||
let default_length = params.crop_length.unwrap_or(200);
|
||||
let mut final_attributes: HashMap<String, usize> = HashMap::new();
|
||||
|
@ -150,3 +161,27 @@ async fn search_with_url_query(
|
|||
|
||||
Ok(HttpResponse::Ok().json(search_builder.search(&reader)?))
|
||||
}
|
||||
|
||||
fn prepare_facet_list<'fa>(facets: &str, schema: &Schema, facet_attrs: &'fa [FieldId]) -> Result<Vec<FieldId>, ResponseError> {
|
||||
let facet_array = serde_json::from_str(facets).expect("do error handling"); // TODO
|
||||
match facet_array {
|
||||
Value::Array(facet_array) => {
|
||||
let wild_card = Value::String("*".to_string());
|
||||
if facet_array.iter().any(|it| it == &wild_card) {
|
||||
return Ok(Vec::from(facet_attrs)); // TODO can make cow?
|
||||
}
|
||||
let mut fields = Vec::with_capacity(facet_attrs.len());
|
||||
for v in facet_array {
|
||||
match v {
|
||||
Value::String(name) => {
|
||||
let id = schema.id(&name).expect("not found error"); // TODO
|
||||
fields.push(id);
|
||||
}
|
||||
_ => todo!("expected string, found {}", v),
|
||||
}
|
||||
}
|
||||
return Ok(fields);
|
||||
}
|
||||
_ => todo!("error, bad syntax, expected array")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue