Introduce the sort search parameter

This commit is contained in:
Kerollmops 2021-08-24 12:31:35 +02:00
parent ea4c831de0
commit e0f73fe742
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
2 changed files with 23 additions and 7 deletions

View File

@ -1,15 +1,17 @@
use std::collections::{BTreeMap, BTreeSet, HashSet}; use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::str::FromStr;
use std::time::Instant; use std::time::Instant;
use either::Either; use either::Either;
use heed::RoTxn; use heed::RoTxn;
use indexmap::IndexMap; use indexmap::IndexMap;
use meilisearch_tokenizer::{Analyzer, AnalyzerConfig, Token}; use meilisearch_tokenizer::{Analyzer, AnalyzerConfig, Token};
use milli::{FieldId, FieldsIdsMap, FilterCondition, MatchingWords}; use milli::{AscDesc, FieldId, FieldsIdsMap, FilterCondition, MatchingWords};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value; use serde_json::Value;
use crate::index::error::FacetError; use crate::index::error::FacetError;
use crate::index::IndexError;
use super::error::Result; use super::error::Result;
use super::Index; use super::Index;
@ -49,6 +51,7 @@ pub struct SearchQuery {
#[serde(default = "Default::default")] #[serde(default = "Default::default")]
pub matches: bool, pub matches: bool,
pub filter: Option<Value>, pub filter: Option<Value>,
pub sort: Option<Vec<String>>,
pub facets_distribution: Option<Vec<String>>, pub facets_distribution: Option<Vec<String>>,
} }
@ -104,6 +107,15 @@ impl Index {
} }
} }
if let Some(ref sort) = query.sort {
let sort = match sort.iter().map(|s| AscDesc::from_str(s)).collect() {
Ok(sorts) => sorts,
Err(err) => return Err(IndexError::Milli(err.into())),
};
search.sort_criteria(sort);
}
let milli::SearchResult { let milli::SearchResult {
documents_ids, documents_ids,
matching_words, matching_words,

View File

@ -1,5 +1,3 @@
use std::collections::{BTreeSet, HashSet};
use actix_web::{web, HttpResponse}; use actix_web::{web, HttpResponse};
use log::debug; use log::debug;
use serde::Deserialize; use serde::Deserialize;
@ -31,6 +29,7 @@ pub struct SearchQueryGet {
crop_length: usize, crop_length: usize,
attributes_to_highlight: Option<String>, attributes_to_highlight: Option<String>,
filter: Option<String>, filter: Option<String>,
sort: Option<String>,
#[serde(default = "Default::default")] #[serde(default = "Default::default")]
matches: bool, matches: bool,
facets_distribution: Option<String>, facets_distribution: Option<String>,
@ -40,19 +39,19 @@ impl From<SearchQueryGet> for SearchQuery {
fn from(other: SearchQueryGet) -> Self { fn from(other: SearchQueryGet) -> Self {
let attributes_to_retrieve = other let attributes_to_retrieve = other
.attributes_to_retrieve .attributes_to_retrieve
.map(|attrs| attrs.split(',').map(String::from).collect::<BTreeSet<_>>()); .map(|attrs| attrs.split(',').map(String::from).collect());
let attributes_to_crop = other let attributes_to_crop = other
.attributes_to_crop .attributes_to_crop
.map(|attrs| attrs.split(',').map(String::from).collect::<Vec<_>>()); .map(|attrs| attrs.split(',').map(String::from).collect());
let attributes_to_highlight = other let attributes_to_highlight = other
.attributes_to_highlight .attributes_to_highlight
.map(|attrs| attrs.split(',').map(String::from).collect::<HashSet<_>>()); .map(|attrs| attrs.split(',').map(String::from).collect());
let facets_distribution = other let facets_distribution = other
.facets_distribution .facets_distribution
.map(|attrs| attrs.split(',').map(String::from).collect::<Vec<_>>()); .map(|attrs| attrs.split(',').map(String::from).collect());
let filter = match other.filter { let filter = match other.filter {
Some(f) => match serde_json::from_str(&f) { Some(f) => match serde_json::from_str(&f) {
@ -62,6 +61,10 @@ impl From<SearchQueryGet> for SearchQuery {
None => None, None => None,
}; };
let sort = other
.sort
.map(|attrs| attrs.split(',').map(String::from).collect());
Self { Self {
q: other.q, q: other.q,
offset: other.offset, offset: other.offset,
@ -71,6 +74,7 @@ impl From<SearchQueryGet> for SearchQuery {
crop_length: other.crop_length, crop_length: other.crop_length,
attributes_to_highlight, attributes_to_highlight,
filter, filter,
sort,
matches: other.matches, matches: other.matches,
facets_distribution, facets_distribution,
} }