From b84ea036ddc063c0ba5be5aca75ab46c2a407ba3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Lecrenier?= Date: Tue, 21 Jun 2022 14:40:01 +0200 Subject: [PATCH] Continue integrating Jayson --- Cargo.lock | 1 + meilisearch-http/Cargo.toml | 1 + meilisearch-http/src/extractors/jayson.rs | 63 +------------------ .../src/routes/indexes/settings.rs | 45 +++++++------ meilisearch-lib/src/index/index.rs | 8 +-- meilisearch-lib/src/index/updates.rs | 9 ++- meilisearch-types/src/error.rs | 32 +++++----- 7 files changed, 49 insertions(+), 110 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f5bcb4eff..e922d5e6d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2099,6 +2099,7 @@ dependencies = [ "meilisearch-auth", "meilisearch-lib", "meilisearch-types", + "milli 0.29.3", "mime", "num_cpus", "obkv", diff --git a/meilisearch-http/Cargo.toml b/meilisearch-http/Cargo.toml index 7f8b9931f..4ed999118 100644 --- a/meilisearch-http/Cargo.toml +++ b/meilisearch-http/Cargo.toml @@ -48,6 +48,7 @@ log = "0.4.14" meilisearch-auth = { path = "../meilisearch-auth" } meilisearch-types = { path = "../meilisearch-types" } meilisearch-lib = { path = "../meilisearch-lib" } +milli = { path = "../../milli/milli" } mime = "0.3.16" num_cpus = "1.13.1" obkv = "0.2.0" diff --git a/meilisearch-http/src/extractors/jayson.rs b/meilisearch-http/src/extractors/jayson.rs index 4cfb45138..3af7e4876 100644 --- a/meilisearch-http/src/extractors/jayson.rs +++ b/meilisearch-http/src/extractors/jayson.rs @@ -1,7 +1,6 @@ use actix_web::{dev::Payload, web::Json, FromRequest, HttpRequest}; use futures::ready; -use jayson::{DeserializeError, DeserializeFromValue, MergeWithError, ValuePointer}; -use meilisearch_lib::milli::AscDescError; +use jayson::{DeserializeError, DeserializeFromValue}; use meilisearch_types::error::{Code, ErrorCode, ResponseError}; use std::{ fmt::Debug, @@ -11,66 +10,6 @@ use std::{ task::{Context, Poll}, }; -// pub struct MeilisearchDeserializeError { -// pub Vec<(ValuePointer, Box)>, -// } -// impl MergeWithError for MeilisearchDeserializeError { -// fn merge(self_: Option, other: AscDescError, merge_location: jayson::ValuePointerRef) -> Result { -// todo!() -// } -// } -// /* -// { -// ! -// x: { -// y: { -// z: { -// a: 2 -// } -// } -// } -// } - -// */ -// impl MergeWithError for MeilisearchDeserializeError { - -// } -// impl DeserializeError for MeilisearchDeserializeError{ -// fn location(&self) -> Option { -// todo!() -// } - -// fn incorrect_value_kind( -// self_: Option, -// actual: jayson::ValueKind, -// accepted: &[jayson::ValueKind], -// location: jayson::ValuePointerRef, -// ) -> Result { -// todo!() -// } - -// fn missing_field( -// self_: Option, -// field: &str, -// location: jayson::ValuePointerRef, -// ) -> Result { -// todo!() -// } - -// fn unknown_key( -// self_: Option, -// key: &str, -// accepted: &[&str], -// location: jayson::ValuePointerRef, -// ) -> Result { -// todo!() -// } - -// fn unexpected(self_: Option, msg: &str, location: jayson::ValuePointerRef) -> Result { -// todo!() -// } -// } - /// Extractor for typed data from Json request payloads /// deserialised by Jayson. /// diff --git a/meilisearch-http/src/routes/indexes/settings.rs b/meilisearch-http/src/routes/indexes/settings.rs index 083a6bd01..5b7e9d609 100644 --- a/meilisearch-http/src/routes/indexes/settings.rs +++ b/meilisearch-http/src/routes/indexes/settings.rs @@ -8,7 +8,6 @@ use meilisearch_types::error::{MeiliDeserError, ResponseError}; use serde_json::json; use crate::analytics::Analytics; -use crate::error::MeilisearchHttpError; use crate::extractors::authentication::{policies::*, GuardedData}; use crate::extractors::jayson::ValidatedJson; use crate::task::SummarizedTaskView; @@ -262,27 +261,27 @@ make_setting_route!( "distinctAttribute" ); -// make_setting_route!( -// "/ranking-rules", -// put, -// Vec, -// ranking_rules, -// "rankingRules", -// analytics, -// |setting: &Option>, req: &HttpRequest| { -// use serde_json::json; +make_setting_route!( + "/ranking-rules", + put, + Vec, + ranking_rules, + "rankingRules", + analytics, + |setting: &Option>, req: &HttpRequest| { + use serde_json::json; -// analytics.publish( -// "RankingRules Updated".to_string(), -// json!({ -// "ranking_rules": { -// "sort_position": setting.as_ref().map(|sort| sort.iter().position(|s| s == "sort")), -// } -// }), -// Some(req), -// ); -// } -// ); + analytics.publish( + "RankingRules Updated".to_string(), + json!({ + "ranking_rules": { + "sort_position": setting.as_ref().map(|sort| sort.iter().position(|s| matches!(s, milli::Criterion::Sort))), + } + }), + Some(req), + ); + } +); make_setting_route!( "/faceting", @@ -350,7 +349,7 @@ generate_configure!( distinct_attribute, stop_words, synonyms, - // ranking_rules, + ranking_rules, typo_tolerance ); @@ -367,7 +366,7 @@ pub async fn update_all( "Settings Updated".to_string(), json!({ "ranking_rules": { - "sort_position": settings.ranking_rules.as_ref().set().map(|sort| sort.iter().position(|s| true /*TODO*/)), + "sort_position": settings.ranking_rules.as_ref().set().map(|sort| sort.iter().position(|s| matches!(s, milli::Criterion::Sort))), }, "searchable_attributes": { "total": settings.searchable_attributes.as_ref().set().map(|searchable| searchable.len()), diff --git a/meilisearch-lib/src/index/index.rs b/meilisearch-lib/src/index/index.rs index 6ceebe5e6..c79c7e8f1 100644 --- a/meilisearch-lib/src/index/index.rs +++ b/meilisearch-lib/src/index/index.rs @@ -8,7 +8,7 @@ use std::sync::Arc; use fst::IntoStreamer; use milli::heed::{EnvOpenOptions, RoTxn}; use milli::update::{IndexerConfig, Setting}; -use milli::{obkv_to_json, AscDesc, FieldDistribution, Member, DEFAULT_VALUES_PER_FACET}; +use milli::{obkv_to_json, FieldDistribution, DEFAULT_VALUES_PER_FACET}; use serde::{Deserialize, Serialize}; use serde_json::{Map, Value}; use time::OffsetDateTime; @@ -143,11 +143,7 @@ impl Index { let sortable_attributes = self.sortable_fields(txn)?.into_iter().collect(); - let criteria = self - .criteria(txn)? - .into_iter() - .map(|c| AscDesc::Asc(Member::Field("todo".to_string()))) - .collect(); + let criteria = self.criteria(txn)?; let stop_words = self .stop_words(txn)? diff --git a/meilisearch-lib/src/index/updates.rs b/meilisearch-lib/src/index/updates.rs index 2300f5c97..011e59e11 100644 --- a/meilisearch-lib/src/index/updates.rs +++ b/meilisearch-lib/src/index/updates.rs @@ -5,8 +5,7 @@ use milli::update::{ DocumentAdditionResult, DocumentDeletionResult, IndexDocumentsConfig, IndexDocumentsMethod, Setting, }; -use milli::{AscDesc, Criterion}; -use rayon::vec; +use milli::Criterion; use serde::{Deserialize, Serialize, Serializer}; use std::collections::{BTreeMap, BTreeSet}; use std::marker::PhantomData; @@ -43,8 +42,8 @@ where E: jayson::DeserializeError, { fn deserialize_from_value( - value: jayson::Value, - location: jayson::ValuePointerRef, + _value: jayson::Value, + _location: jayson::ValuePointerRef, ) -> std::result::Result where V: jayson::IntoValue, @@ -147,7 +146,7 @@ pub struct Settings { #[serde(default, skip_serializing_if = "Setting::is_not_set")] #[cfg_attr(test, proptest(strategy = "test::setting_strategy()"))] #[jayson(needs_predicate)] - pub ranking_rules: Setting>, + pub ranking_rules: Setting>, #[serde(default, skip_serializing_if = "Setting::is_not_set")] #[cfg_attr(test, proptest(strategy = "test::setting_strategy()"))] pub stop_words: Setting>, diff --git a/meilisearch-types/src/error.rs b/meilisearch-types/src/error.rs index c1f37edba..17c23b889 100644 --- a/meilisearch-types/src/error.rs +++ b/meilisearch-types/src/error.rs @@ -36,10 +36,10 @@ impl ErrorCode for MeiliDeserError { Code::MalformedPayload } } -impl jayson::MergeWithError for MeiliDeserError { +impl jayson::MergeWithError for MeiliDeserError { fn merge( - self_: Option, - other: milli::AscDescError, + _self_: Option, + other: milli::CriterionError, merge_location: jayson::ValuePointerRef, ) -> Result { let pointer = merge_location.to_owned(); @@ -49,9 +49,9 @@ impl jayson::MergeWithError for MeiliDeserError { impl jayson::MergeWithError for MeiliDeserError { fn merge( - self_: Option, + _self_: Option, other: MeiliDeserError, - merge_location: ValuePointerRef, + _merge_location: ValuePointerRef, ) -> Result { Err(other) } @@ -65,10 +65,10 @@ impl jayson::DeserializeError for MeiliDeserError { /// /// Return `Ok` to continue deserializing or `Err` to fail early. fn incorrect_value_kind( - self_: Option, + _self_: Option, actual: ValueKind, - accepted: &[ValueKind], - location: ValuePointerRef, + _accepted: &[ValueKind], + _location: ValuePointerRef, ) -> Result { Err(MeiliDeserError(format!("incorrect value kind {actual}"))) } @@ -76,9 +76,9 @@ impl jayson::DeserializeError for MeiliDeserError { /// /// Return `Ok` to continue deserializing or `Err` to fail early. fn missing_field( - self_: Option, + _self_: Option, field: &str, - location: ValuePointerRef, + _location: ValuePointerRef, ) -> Result { Err(MeiliDeserError(format!("missing field {field}"))) } @@ -86,17 +86,21 @@ impl jayson::DeserializeError for MeiliDeserError { /// /// Return `Ok` to continue deserializing or `Err` to fail early. fn unknown_key( - self_: Option, + _self_: Option, key: &str, - accepted: &[&str], - location: ValuePointerRef, + _accepted: &[&str], + _location: ValuePointerRef, ) -> Result { Err(MeiliDeserError(format!("unknown key {key}"))) } /// Create a new error with the custom message. /// /// Return `Ok` to continue deserializing or `Err` to fail early. - fn unexpected(self_: Option, msg: &str, location: ValuePointerRef) -> Result { + fn unexpected( + _self_: Option, + msg: &str, + _location: ValuePointerRef, + ) -> Result { Err(MeiliDeserError(format!("unexpected {msg}"))) } }