mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-27 07:14:26 +01:00
move search logic out of search route
This commit is contained in:
parent
35605c9f57
commit
8cd224899c
@ -8,7 +8,7 @@ use serde::Deserialize;
|
|||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
use crate::error::{Error, FacetCountError, ResponseError};
|
use crate::error::{Error, FacetCountError, ResponseError};
|
||||||
use crate::helpers::meilisearch::IndexSearchExt;
|
use crate::helpers::meilisearch::{IndexSearchExt, SearchResult};
|
||||||
use crate::helpers::Authentication;
|
use crate::helpers::Authentication;
|
||||||
use crate::routes::IndexParam;
|
use crate::routes::IndexParam;
|
||||||
use crate::Data;
|
use crate::Data;
|
||||||
@ -36,16 +36,12 @@ struct SearchQuery {
|
|||||||
facets_distribution: Option<String>,
|
facets_distribution: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/indexes/{index_uid}/search", wrap = "Authentication::Public")]
|
impl SearchQuery {
|
||||||
async fn search_with_url_query(
|
fn search(&self, index_uid: &str, data: web::Data<Data>) -> Result<SearchResult, ResponseError> {
|
||||||
data: web::Data<Data>,
|
|
||||||
path: web::Path<IndexParam>,
|
|
||||||
params: web::Query<SearchQuery>,
|
|
||||||
) -> Result<HttpResponse, ResponseError> {
|
|
||||||
let index = data
|
let index = data
|
||||||
.db
|
.db
|
||||||
.open_index(&path.index_uid)
|
.open_index(index_uid)
|
||||||
.ok_or(Error::index_not_found(&path.index_uid))?;
|
.ok_or(Error::index_not_found(index_uid))?;
|
||||||
|
|
||||||
let reader = data.db.main_read_txn()?;
|
let reader = data.db.main_read_txn()?;
|
||||||
let schema = index
|
let schema = index
|
||||||
@ -53,18 +49,18 @@ async fn search_with_url_query(
|
|||||||
.schema(&reader)?
|
.schema(&reader)?
|
||||||
.ok_or(Error::internal("Impossible to retrieve the schema"))?;
|
.ok_or(Error::internal("Impossible to retrieve the schema"))?;
|
||||||
|
|
||||||
let mut search_builder = index.new_search(params.q.clone());
|
let mut search_builder = index.new_search(self.q.clone());
|
||||||
|
|
||||||
if let Some(offset) = params.offset {
|
if let Some(offset) = self.offset {
|
||||||
search_builder.offset(offset);
|
search_builder.offset(offset);
|
||||||
}
|
}
|
||||||
if let Some(limit) = params.limit {
|
if let Some(limit) = self.limit {
|
||||||
search_builder.limit(limit);
|
search_builder.limit(limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
let available_attributes = schema.displayed_name();
|
let available_attributes = schema.displayed_name();
|
||||||
let mut restricted_attributes: HashSet<&str>;
|
let mut restricted_attributes: HashSet<&str>;
|
||||||
match ¶ms.attributes_to_retrieve {
|
match &self.attributes_to_retrieve {
|
||||||
Some(attributes_to_retrieve) => {
|
Some(attributes_to_retrieve) => {
|
||||||
let attributes_to_retrieve: HashSet<&str> = attributes_to_retrieve.split(',').collect();
|
let attributes_to_retrieve: HashSet<&str> = attributes_to_retrieve.split(',').collect();
|
||||||
if attributes_to_retrieve.contains("*") {
|
if attributes_to_retrieve.contains("*") {
|
||||||
@ -86,12 +82,12 @@ async fn search_with_url_query(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ref facet_filters) = params.facet_filters {
|
if let Some(ref facet_filters) = self.facet_filters {
|
||||||
let attrs = index.main.attributes_for_faceting(&reader)?.unwrap_or_default();
|
let attrs = index.main.attributes_for_faceting(&reader)?.unwrap_or_default();
|
||||||
search_builder.add_facet_filters(FacetFilter::from_str(facet_filters, &schema, &attrs)?);
|
search_builder.add_facet_filters(FacetFilter::from_str(facet_filters, &schema, &attrs)?);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(facets) = ¶ms.facets_distribution {
|
if let Some(facets) = &self.facets_distribution {
|
||||||
match index.main.attributes_for_faceting(&reader)? {
|
match index.main.attributes_for_faceting(&reader)? {
|
||||||
Some(ref attrs) => {
|
Some(ref attrs) => {
|
||||||
let field_ids = prepare_facet_list(&facets, &schema, attrs)?;
|
let field_ids = prepare_facet_list(&facets, &schema, attrs)?;
|
||||||
@ -101,8 +97,8 @@ async fn search_with_url_query(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(attributes_to_crop) = ¶ms.attributes_to_crop {
|
if let Some(attributes_to_crop) = &self.attributes_to_crop {
|
||||||
let default_length = params.crop_length.unwrap_or(200);
|
let default_length = self.crop_length.unwrap_or(200);
|
||||||
let mut final_attributes: HashMap<String, usize> = HashMap::new();
|
let mut final_attributes: HashMap<String, usize> = HashMap::new();
|
||||||
|
|
||||||
for attribute in attributes_to_crop.split(',') {
|
for attribute in attributes_to_crop.split(',') {
|
||||||
@ -129,7 +125,7 @@ async fn search_with_url_query(
|
|||||||
search_builder.attributes_to_crop(final_attributes);
|
search_builder.attributes_to_crop(final_attributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(attributes_to_highlight) = ¶ms.attributes_to_highlight {
|
if let Some(attributes_to_highlight) = &self.attributes_to_highlight {
|
||||||
let mut final_attributes: HashSet<String> = HashSet::new();
|
let mut final_attributes: HashSet<String> = HashSet::new();
|
||||||
for attribute in attributes_to_highlight.split(',') {
|
for attribute in attributes_to_highlight.split(',') {
|
||||||
if attribute == "*" {
|
if attribute == "*" {
|
||||||
@ -148,17 +144,26 @@ async fn search_with_url_query(
|
|||||||
search_builder.attributes_to_highlight(final_attributes);
|
search_builder.attributes_to_highlight(final_attributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(filters) = ¶ms.filters {
|
if let Some(filters) = &self.filters {
|
||||||
search_builder.filters(filters.to_string());
|
search_builder.filters(filters.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(matches) = params.matches {
|
if let Some(matches) = self.matches {
|
||||||
if matches {
|
if matches {
|
||||||
search_builder.get_matches();
|
search_builder.get_matches();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let search_result = search_builder.search(&reader)?;
|
search_builder.search(&reader)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/indexes/{index_uid}/search", wrap = "Authentication::Public")]
|
||||||
|
async fn search_with_url_query(
|
||||||
|
data: web::Data<Data>,
|
||||||
|
path: web::Path<IndexParam>,
|
||||||
|
params: web::Query<SearchQuery>,
|
||||||
|
) -> Result<HttpResponse, ResponseError> {
|
||||||
|
let search_result = params.search(&path.index_uid, data)?;
|
||||||
Ok(HttpResponse::Ok().json(search_result))
|
Ok(HttpResponse::Ok().json(search_result))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user