Continue integrating Jayson

This commit is contained in:
Loïc Lecrenier 2022-06-21 14:40:01 +02:00
parent ba82584328
commit b84ea036dd
7 changed files with 49 additions and 110 deletions

1
Cargo.lock generated
View File

@ -2099,6 +2099,7 @@ dependencies = [
"meilisearch-auth", "meilisearch-auth",
"meilisearch-lib", "meilisearch-lib",
"meilisearch-types", "meilisearch-types",
"milli 0.29.3",
"mime", "mime",
"num_cpus", "num_cpus",
"obkv", "obkv",

View File

@ -48,6 +48,7 @@ log = "0.4.14"
meilisearch-auth = { path = "../meilisearch-auth" } meilisearch-auth = { path = "../meilisearch-auth" }
meilisearch-types = { path = "../meilisearch-types" } meilisearch-types = { path = "../meilisearch-types" }
meilisearch-lib = { path = "../meilisearch-lib" } meilisearch-lib = { path = "../meilisearch-lib" }
milli = { path = "../../milli/milli" }
mime = "0.3.16" mime = "0.3.16"
num_cpus = "1.13.1" num_cpus = "1.13.1"
obkv = "0.2.0" obkv = "0.2.0"

View File

@ -1,7 +1,6 @@
use actix_web::{dev::Payload, web::Json, FromRequest, HttpRequest}; use actix_web::{dev::Payload, web::Json, FromRequest, HttpRequest};
use futures::ready; use futures::ready;
use jayson::{DeserializeError, DeserializeFromValue, MergeWithError, ValuePointer}; use jayson::{DeserializeError, DeserializeFromValue};
use meilisearch_lib::milli::AscDescError;
use meilisearch_types::error::{Code, ErrorCode, ResponseError}; use meilisearch_types::error::{Code, ErrorCode, ResponseError};
use std::{ use std::{
fmt::Debug, fmt::Debug,
@ -11,66 +10,6 @@ use std::{
task::{Context, Poll}, task::{Context, Poll},
}; };
// pub struct MeilisearchDeserializeError {
// pub Vec<(ValuePointer, Box<dyn Error>)>,
// }
// impl MergeWithError<AscDescError> for MeilisearchDeserializeError {
// fn merge(self_: Option<Self>, other: AscDescError, merge_location: jayson::ValuePointerRef) -> Result<Self, Self> {
// todo!()
// }
// }
// /*
// {
// !
// x: {
// y: {
// z: {
// a: 2
// }
// }
// }
// }
// */
// impl MergeWithError<MeilisearchDeserializeError> for MeilisearchDeserializeError {
// }
// impl DeserializeError for MeilisearchDeserializeError{
// fn location(&self) -> Option<jayson::ValuePointer> {
// todo!()
// }
// fn incorrect_value_kind(
// self_: Option<Self>,
// actual: jayson::ValueKind,
// accepted: &[jayson::ValueKind],
// location: jayson::ValuePointerRef,
// ) -> Result<Self, Self> {
// todo!()
// }
// fn missing_field(
// self_: Option<Self>,
// field: &str,
// location: jayson::ValuePointerRef,
// ) -> Result<Self, Self> {
// todo!()
// }
// fn unknown_key(
// self_: Option<Self>,
// key: &str,
// accepted: &[&str],
// location: jayson::ValuePointerRef,
// ) -> Result<Self, Self> {
// todo!()
// }
// fn unexpected(self_: Option<Self>, msg: &str, location: jayson::ValuePointerRef) -> Result<Self, Self> {
// todo!()
// }
// }
/// Extractor for typed data from Json request payloads /// Extractor for typed data from Json request payloads
/// deserialised by Jayson. /// deserialised by Jayson.
/// ///

View File

@ -8,7 +8,6 @@ use meilisearch_types::error::{MeiliDeserError, ResponseError};
use serde_json::json; use serde_json::json;
use crate::analytics::Analytics; use crate::analytics::Analytics;
use crate::error::MeilisearchHttpError;
use crate::extractors::authentication::{policies::*, GuardedData}; use crate::extractors::authentication::{policies::*, GuardedData};
use crate::extractors::jayson::ValidatedJson; use crate::extractors::jayson::ValidatedJson;
use crate::task::SummarizedTaskView; use crate::task::SummarizedTaskView;
@ -262,27 +261,27 @@ make_setting_route!(
"distinctAttribute" "distinctAttribute"
); );
// make_setting_route!( make_setting_route!(
// "/ranking-rules", "/ranking-rules",
// put, put,
// Vec<String>, Vec<milli::Criterion>,
// ranking_rules, ranking_rules,
// "rankingRules", "rankingRules",
// analytics, analytics,
// |setting: &Option<Vec<milli::AscDesc>>, req: &HttpRequest| { |setting: &Option<Vec<milli::Criterion>>, req: &HttpRequest| {
// use serde_json::json; use serde_json::json;
// analytics.publish( analytics.publish(
// "RankingRules Updated".to_string(), "RankingRules Updated".to_string(),
// json!({ json!({
// "ranking_rules": { "ranking_rules": {
// "sort_position": setting.as_ref().map(|sort| sort.iter().position(|s| s == "sort")), "sort_position": setting.as_ref().map(|sort| sort.iter().position(|s| matches!(s, milli::Criterion::Sort))),
// } }
// }), }),
// Some(req), Some(req),
// ); );
// } }
// ); );
make_setting_route!( make_setting_route!(
"/faceting", "/faceting",
@ -350,7 +349,7 @@ generate_configure!(
distinct_attribute, distinct_attribute,
stop_words, stop_words,
synonyms, synonyms,
// ranking_rules, ranking_rules,
typo_tolerance typo_tolerance
); );
@ -367,7 +366,7 @@ pub async fn update_all(
"Settings Updated".to_string(), "Settings Updated".to_string(),
json!({ json!({
"ranking_rules": { "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": { "searchable_attributes": {
"total": settings.searchable_attributes.as_ref().set().map(|searchable| searchable.len()), "total": settings.searchable_attributes.as_ref().set().map(|searchable| searchable.len()),

View File

@ -8,7 +8,7 @@ use std::sync::Arc;
use fst::IntoStreamer; use fst::IntoStreamer;
use milli::heed::{EnvOpenOptions, RoTxn}; use milli::heed::{EnvOpenOptions, RoTxn};
use milli::update::{IndexerConfig, Setting}; 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::{Deserialize, Serialize};
use serde_json::{Map, Value}; use serde_json::{Map, Value};
use time::OffsetDateTime; use time::OffsetDateTime;
@ -143,11 +143,7 @@ impl Index {
let sortable_attributes = self.sortable_fields(txn)?.into_iter().collect(); let sortable_attributes = self.sortable_fields(txn)?.into_iter().collect();
let criteria = self let criteria = self.criteria(txn)?;
.criteria(txn)?
.into_iter()
.map(|c| AscDesc::Asc(Member::Field("todo".to_string())))
.collect();
let stop_words = self let stop_words = self
.stop_words(txn)? .stop_words(txn)?

View File

@ -5,8 +5,7 @@ use milli::update::{
DocumentAdditionResult, DocumentDeletionResult, IndexDocumentsConfig, IndexDocumentsMethod, DocumentAdditionResult, DocumentDeletionResult, IndexDocumentsConfig, IndexDocumentsMethod,
Setting, Setting,
}; };
use milli::{AscDesc, Criterion}; use milli::Criterion;
use rayon::vec;
use serde::{Deserialize, Serialize, Serializer}; use serde::{Deserialize, Serialize, Serializer};
use std::collections::{BTreeMap, BTreeSet}; use std::collections::{BTreeMap, BTreeSet};
use std::marker::PhantomData; use std::marker::PhantomData;
@ -43,8 +42,8 @@ where
E: jayson::DeserializeError, E: jayson::DeserializeError,
{ {
fn deserialize_from_value<V>( fn deserialize_from_value<V>(
value: jayson::Value<V>, _value: jayson::Value<V>,
location: jayson::ValuePointerRef, _location: jayson::ValuePointerRef,
) -> std::result::Result<Self, E> ) -> std::result::Result<Self, E>
where where
V: jayson::IntoValue, V: jayson::IntoValue,
@ -147,7 +146,7 @@ pub struct Settings<T> {
#[serde(default, skip_serializing_if = "Setting::is_not_set")] #[serde(default, skip_serializing_if = "Setting::is_not_set")]
#[cfg_attr(test, proptest(strategy = "test::setting_strategy()"))] #[cfg_attr(test, proptest(strategy = "test::setting_strategy()"))]
#[jayson(needs_predicate)] #[jayson(needs_predicate)]
pub ranking_rules: Setting<Vec<AscDesc>>, pub ranking_rules: Setting<Vec<Criterion>>,
#[serde(default, skip_serializing_if = "Setting::is_not_set")] #[serde(default, skip_serializing_if = "Setting::is_not_set")]
#[cfg_attr(test, proptest(strategy = "test::setting_strategy()"))] #[cfg_attr(test, proptest(strategy = "test::setting_strategy()"))]
pub stop_words: Setting<BTreeSet<String>>, pub stop_words: Setting<BTreeSet<String>>,

View File

@ -36,10 +36,10 @@ impl ErrorCode for MeiliDeserError {
Code::MalformedPayload Code::MalformedPayload
} }
} }
impl jayson::MergeWithError<milli::AscDescError> for MeiliDeserError { impl jayson::MergeWithError<milli::CriterionError> for MeiliDeserError {
fn merge( fn merge(
self_: Option<Self>, _self_: Option<Self>,
other: milli::AscDescError, other: milli::CriterionError,
merge_location: jayson::ValuePointerRef, merge_location: jayson::ValuePointerRef,
) -> Result<Self, Self> { ) -> Result<Self, Self> {
let pointer = merge_location.to_owned(); let pointer = merge_location.to_owned();
@ -49,9 +49,9 @@ impl jayson::MergeWithError<milli::AscDescError> for MeiliDeserError {
impl jayson::MergeWithError<MeiliDeserError> for MeiliDeserError { impl jayson::MergeWithError<MeiliDeserError> for MeiliDeserError {
fn merge( fn merge(
self_: Option<Self>, _self_: Option<Self>,
other: MeiliDeserError, other: MeiliDeserError,
merge_location: ValuePointerRef, _merge_location: ValuePointerRef,
) -> Result<Self, Self> { ) -> Result<Self, Self> {
Err(other) Err(other)
} }
@ -65,10 +65,10 @@ impl jayson::DeserializeError for MeiliDeserError {
/// ///
/// Return `Ok` to continue deserializing or `Err` to fail early. /// Return `Ok` to continue deserializing or `Err` to fail early.
fn incorrect_value_kind( fn incorrect_value_kind(
self_: Option<Self>, _self_: Option<Self>,
actual: ValueKind, actual: ValueKind,
accepted: &[ValueKind], _accepted: &[ValueKind],
location: ValuePointerRef, _location: ValuePointerRef,
) -> Result<Self, Self> { ) -> Result<Self, Self> {
Err(MeiliDeserError(format!("incorrect value kind {actual}"))) 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. /// Return `Ok` to continue deserializing or `Err` to fail early.
fn missing_field( fn missing_field(
self_: Option<Self>, _self_: Option<Self>,
field: &str, field: &str,
location: ValuePointerRef, _location: ValuePointerRef,
) -> Result<Self, Self> { ) -> Result<Self, Self> {
Err(MeiliDeserError(format!("missing field {field}"))) 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. /// Return `Ok` to continue deserializing or `Err` to fail early.
fn unknown_key( fn unknown_key(
self_: Option<Self>, _self_: Option<Self>,
key: &str, key: &str,
accepted: &[&str], _accepted: &[&str],
location: ValuePointerRef, _location: ValuePointerRef,
) -> Result<Self, Self> { ) -> Result<Self, Self> {
Err(MeiliDeserError(format!("unknown key {key}"))) Err(MeiliDeserError(format!("unknown key {key}")))
} }
/// Create a new error with the custom message. /// Create a new error with the custom message.
/// ///
/// Return `Ok` to continue deserializing or `Err` to fail early. /// Return `Ok` to continue deserializing or `Err` to fail early.
fn unexpected(self_: Option<Self>, msg: &str, location: ValuePointerRef) -> Result<Self, Self> { fn unexpected(
_self_: Option<Self>,
msg: &str,
_location: ValuePointerRef,
) -> Result<Self, Self> {
Err(MeiliDeserError(format!("unexpected {msg}"))) Err(MeiliDeserError(format!("unexpected {msg}")))
} }
} }