2022-10-09 19:43:51 +02:00
|
|
|
pub mod encoder;
|
2021-03-24 11:03:01 +01:00
|
|
|
pub mod index;
|
|
|
|
pub mod server;
|
|
|
|
pub mod service;
|
2021-02-18 19:50:52 +01:00
|
|
|
|
2023-09-11 16:50:53 +02:00
|
|
|
use std::fmt::{self, Display};
|
|
|
|
|
2023-10-30 11:00:46 +01:00
|
|
|
#[allow(unused)]
|
2024-06-05 23:40:29 +02:00
|
|
|
pub use index::GetAllDocumentsOptions;
|
2023-09-11 16:50:53 +02:00
|
|
|
use meili_snap::json_string;
|
2024-07-30 10:27:57 +02:00
|
|
|
use once_cell::sync::Lazy;
|
2023-09-11 16:50:53 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-10-30 11:00:46 +01:00
|
|
|
#[allow(unused)]
|
2022-04-28 10:48:57 +02:00
|
|
|
pub use server::{default_settings, Server};
|
2024-07-30 10:47:58 +02:00
|
|
|
use tokio::sync::OnceCell;
|
2021-02-18 19:50:52 +01:00
|
|
|
|
2024-07-30 10:27:57 +02:00
|
|
|
use crate::common::index::Index;
|
|
|
|
|
|
|
|
pub enum Shared {}
|
|
|
|
pub enum Owned {}
|
|
|
|
|
2023-09-11 16:50:53 +02:00
|
|
|
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
|
|
|
|
pub struct Value(pub serde_json::Value);
|
|
|
|
|
|
|
|
impl Value {
|
2024-03-18 12:06:00 +01:00
|
|
|
#[track_caller]
|
2023-09-11 16:50:53 +02:00
|
|
|
pub fn uid(&self) -> u64 {
|
|
|
|
if let Some(uid) = self["uid"].as_u64() {
|
|
|
|
uid
|
|
|
|
} else if let Some(uid) = self["taskUid"].as_u64() {
|
|
|
|
uid
|
|
|
|
} else {
|
|
|
|
panic!("Didn't find any task id in: {self}");
|
|
|
|
}
|
|
|
|
}
|
2024-07-17 11:13:37 +02:00
|
|
|
|
2024-07-30 10:27:57 +02:00
|
|
|
/// Return `true` if the `status` field is set to `succeeded`.
|
|
|
|
/// Panic if the `status` field doesn't exists.
|
|
|
|
#[track_caller]
|
|
|
|
pub fn is_success(&self) -> bool {
|
|
|
|
if !self["status"].is_string() {
|
|
|
|
panic!("Called `is_success` on {}", serde_json::to_string_pretty(&self.0).unwrap());
|
|
|
|
}
|
|
|
|
self["status"] == serde_json::Value::String(String::from("succeeded"))
|
|
|
|
}
|
|
|
|
|
2024-07-17 11:13:37 +02:00
|
|
|
// Panic if the json doesn't contain the `status` field set to "succeeded"
|
|
|
|
#[track_caller]
|
|
|
|
pub fn succeeded(&self) -> &Self {
|
2024-07-30 10:27:57 +02:00
|
|
|
if !self.is_success() {
|
2024-07-17 11:13:37 +02:00
|
|
|
panic!("Called succeeded on {}", serde_json::to_string_pretty(&self.0).unwrap());
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
2023-09-11 16:50:53 +02:00
|
|
|
}
|
|
|
|
|
2023-09-11 17:02:01 +02:00
|
|
|
impl From<serde_json::Value> for Value {
|
|
|
|
fn from(value: serde_json::Value) -> Self {
|
|
|
|
Value(value)
|
2023-09-11 16:50:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::ops::Deref for Value {
|
|
|
|
type Target = serde_json::Value;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-24 19:00:44 +02:00
|
|
|
impl std::ops::DerefMut for Value {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-11 16:50:53 +02:00
|
|
|
impl PartialEq<serde_json::Value> for Value {
|
|
|
|
fn eq(&self, other: &serde_json::Value) -> bool {
|
|
|
|
&self.0 == other
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq<Value> for serde_json::Value {
|
|
|
|
fn eq(&self, other: &Value) -> bool {
|
|
|
|
self == &other.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq<&str> for Value {
|
|
|
|
fn eq(&self, other: &&str) -> bool {
|
|
|
|
self.0.eq(other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Value {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"{}",
|
2024-06-12 18:27:03 +02:00
|
|
|
json_string!(self, {
|
2024-07-18 16:32:50 +02:00
|
|
|
".uid" => "[uid]",
|
2024-06-12 18:27:03 +02:00
|
|
|
".enqueuedAt" => "[date]",
|
|
|
|
".startedAt" => "[date]",
|
|
|
|
".finishedAt" => "[date]",
|
|
|
|
".duration" => "[duration]",
|
|
|
|
".processingTimeMs" => "[duration]",
|
|
|
|
".details.embedders.*.url" => "[url]"
|
|
|
|
})
|
2023-09-11 16:50:53 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Vec<Value>> for Value {
|
|
|
|
fn from(value: Vec<Value>) -> Self {
|
|
|
|
Self(value.into_iter().map(|value| value.0).collect::<serde_json::Value>())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! json {
|
|
|
|
($($json:tt)+) => {
|
|
|
|
$crate::common::Value(serde_json::json!($($json)+))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-18 19:50:52 +01:00
|
|
|
/// Performs a search test on both post and get routes
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! test_post_get_search {
|
|
|
|
($server:expr, $query:expr, |$response:ident, $status_code:ident | $block:expr) => {
|
2022-12-07 16:20:47 +01:00
|
|
|
let post_query: meilisearch::routes::search::SearchQueryPost =
|
2021-02-18 19:50:52 +01:00
|
|
|
serde_json::from_str(&$query.clone().to_string()).unwrap();
|
2022-12-07 16:20:47 +01:00
|
|
|
let get_query: meilisearch::routes::search::SearchQuery = post_query.into();
|
2021-02-18 19:50:52 +01:00
|
|
|
let get_query = ::serde_url_params::to_string(&get_query).unwrap();
|
|
|
|
let ($response, $status_code) = $server.search_get(&get_query).await;
|
2022-10-20 18:00:07 +02:00
|
|
|
let _ = ::std::panic::catch_unwind(|| $block)
|
|
|
|
.map_err(|e| panic!("panic in get route: {:?}", e.downcast_ref::<&str>().unwrap()));
|
2021-02-18 19:50:52 +01:00
|
|
|
let ($response, $status_code) = $server.search_post($query).await;
|
2022-10-20 18:00:07 +02:00
|
|
|
let _ = ::std::panic::catch_unwind(|| $block)
|
|
|
|
.map_err(|e| panic!("panic in post route: {:?}", e.downcast_ref::<&str>().unwrap()));
|
2021-02-18 19:50:52 +01:00
|
|
|
};
|
|
|
|
}
|
2024-07-30 10:27:57 +02:00
|
|
|
|
|
|
|
pub async fn shared_does_not_exists_index() -> &'static Index<'static, Shared> {
|
|
|
|
static INDEX: Lazy<Index<'static, Shared>> = Lazy::new(|| {
|
|
|
|
let server = Server::new_shared();
|
|
|
|
server._index("DOES_NOT_EXISTS").to_shared()
|
|
|
|
});
|
|
|
|
&INDEX
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn shared_empty_index() -> &'static Index<'static, Shared> {
|
|
|
|
static INDEX: Lazy<Index<'static, Shared>> = Lazy::new(|| {
|
|
|
|
let server = Server::new_shared();
|
|
|
|
server._index("EMPTY_INDEX").to_shared()
|
|
|
|
});
|
|
|
|
let index = Lazy::get(&INDEX);
|
|
|
|
// That means the lazy has never been initialized, we need to create the index and index the documents
|
|
|
|
if index.is_none() {
|
|
|
|
let (response, _code) = INDEX._create(None).await;
|
|
|
|
INDEX.wait_task(response.uid()).await.succeeded();
|
|
|
|
}
|
|
|
|
&INDEX
|
|
|
|
}
|
|
|
|
|
|
|
|
pub static DOCUMENTS: Lazy<Value> = Lazy::new(|| {
|
|
|
|
json!([
|
|
|
|
{
|
|
|
|
"title": "Shazam!",
|
|
|
|
"id": "287947",
|
|
|
|
"_vectors": { "manual": [1, 2, 3]},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"title": "Captain Marvel",
|
|
|
|
"id": "299537",
|
|
|
|
"_vectors": { "manual": [1, 2, 54] },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"title": "Escape Room",
|
|
|
|
"id": "522681",
|
|
|
|
"_vectors": { "manual": [10, -23, 32] },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"title": "How to Train Your Dragon: The Hidden World",
|
|
|
|
"id": "166428",
|
|
|
|
"_vectors": { "manual": [-100, 231, 32] },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"title": "Gläss",
|
|
|
|
"id": "450465",
|
|
|
|
"_vectors": { "manual": [-100, 340, 90] },
|
|
|
|
}
|
|
|
|
])
|
|
|
|
});
|
|
|
|
|
|
|
|
pub async fn shared_index_with_documents() -> &'static Index<'static, Shared> {
|
2024-07-30 10:47:58 +02:00
|
|
|
static INDEX: OnceCell<Index<'static, Shared>> = OnceCell::const_new();
|
|
|
|
INDEX.get_or_init(|| async {
|
2024-07-30 10:27:57 +02:00
|
|
|
let server = Server::new_shared();
|
2024-07-30 10:47:58 +02:00
|
|
|
let index = server._index("SHARED_DOCUMENTS").to_shared();
|
2024-07-30 10:27:57 +02:00
|
|
|
let documents = DOCUMENTS.clone();
|
2024-07-30 10:47:58 +02:00
|
|
|
let (response, _code) = index._add_documents(documents, None).await;
|
|
|
|
index.wait_task(response.uid()).await.succeeded();
|
|
|
|
let (response, _code) = index
|
2024-07-30 10:27:57 +02:00
|
|
|
._update_settings(
|
|
|
|
json!({"filterableAttributes": ["id", "title"], "sortableAttributes": ["id", "title"]}),
|
|
|
|
)
|
|
|
|
.await;
|
2024-07-30 10:47:58 +02:00
|
|
|
index.wait_task(response.uid()).await.succeeded();
|
|
|
|
index
|
|
|
|
}).await
|
2024-07-30 10:27:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub static SCORE_DOCUMENTS: Lazy<Value> = Lazy::new(|| {
|
|
|
|
json!([
|
|
|
|
{
|
|
|
|
"title": "Batman the dark knight returns: Part 1",
|
|
|
|
"id": "A",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"title": "Batman the dark knight returns: Part 2",
|
|
|
|
"id": "B",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"title": "Batman Returns",
|
|
|
|
"id": "C",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"title": "Batman",
|
|
|
|
"id": "D",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"title": "Badman",
|
|
|
|
"id": "E",
|
|
|
|
}
|
|
|
|
])
|
|
|
|
});
|
|
|
|
|
|
|
|
pub static NESTED_DOCUMENTS: Lazy<Value> = Lazy::new(|| {
|
|
|
|
json!([
|
|
|
|
{
|
|
|
|
"id": 852,
|
|
|
|
"father": "jean",
|
|
|
|
"mother": "michelle",
|
|
|
|
"doggos": [
|
|
|
|
{
|
|
|
|
"name": "bobby",
|
|
|
|
"age": 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "buddy",
|
|
|
|
"age": 4,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
"cattos": "pésti",
|
|
|
|
"_vectors": { "manual": [1, 2, 3]},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 654,
|
|
|
|
"father": "pierre",
|
|
|
|
"mother": "sabine",
|
|
|
|
"doggos": [
|
|
|
|
{
|
|
|
|
"name": "gros bill",
|
|
|
|
"age": 8,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
"cattos": ["simba", "pestiféré"],
|
|
|
|
"_vectors": { "manual": [1, 2, 54] },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 750,
|
|
|
|
"father": "romain",
|
|
|
|
"mother": "michelle",
|
|
|
|
"cattos": ["enigma"],
|
|
|
|
"_vectors": { "manual": [10, 23, 32] },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 951,
|
|
|
|
"father": "jean-baptiste",
|
|
|
|
"mother": "sophie",
|
|
|
|
"doggos": [
|
|
|
|
{
|
|
|
|
"name": "turbo",
|
|
|
|
"age": 5,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "fast",
|
|
|
|
"age": 6,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
"cattos": ["moumoute", "gomez"],
|
|
|
|
"_vectors": { "manual": [10, 23, 32] },
|
|
|
|
},
|
|
|
|
])
|
|
|
|
});
|
|
|
|
|
|
|
|
pub async fn shared_index_with_nested_documents() -> &'static Index<'static, Shared> {
|
2024-07-30 10:47:58 +02:00
|
|
|
static INDEX: OnceCell<Index<'static, Shared>> = OnceCell::const_new();
|
|
|
|
INDEX.get_or_init(|| async {
|
2024-07-30 10:27:57 +02:00
|
|
|
let server = Server::new_shared();
|
2024-07-30 10:47:58 +02:00
|
|
|
let index = server._index("SHARED_NESTED_DOCUMENTS").to_shared();
|
2024-07-30 10:27:57 +02:00
|
|
|
let documents = NESTED_DOCUMENTS.clone();
|
2024-07-30 10:47:58 +02:00
|
|
|
let (response, _code) = index._add_documents(documents, None).await;
|
|
|
|
index.wait_task(response.uid()).await.succeeded();
|
|
|
|
let (response, _code) = index
|
2024-07-30 10:27:57 +02:00
|
|
|
._update_settings(
|
|
|
|
json!({"filterableAttributes": ["father", "doggos"], "sortableAttributes": ["doggos"]}),
|
|
|
|
)
|
|
|
|
.await;
|
2024-07-30 10:47:58 +02:00
|
|
|
index.wait_task(response.uid()).await.succeeded();
|
|
|
|
index
|
|
|
|
}).await
|
2024-07-30 10:27:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub static FRUITS_DOCUMENTS: Lazy<Value> = Lazy::new(|| {
|
|
|
|
json!([
|
|
|
|
{
|
|
|
|
"name": "Exclusive sale: green apple",
|
|
|
|
"id": "green-apple-boosted",
|
|
|
|
"BOOST": true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "Pear",
|
|
|
|
"id": "pear",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "Red apple gala",
|
|
|
|
"id": "red-apple-gala",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "Exclusive sale: Red Tomato",
|
|
|
|
"id": "red-tomatoes-boosted",
|
|
|
|
"BOOST": true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "Exclusive sale: Red delicious apple",
|
|
|
|
"id": "red-delicious-boosted",
|
|
|
|
"BOOST": true,
|
|
|
|
}
|
|
|
|
])
|
|
|
|
});
|
|
|
|
|
|
|
|
pub static VECTOR_DOCUMENTS: Lazy<Value> = Lazy::new(|| {
|
|
|
|
json!([
|
|
|
|
{
|
|
|
|
"id": "A",
|
|
|
|
"description": "the dog barks at the cat",
|
|
|
|
"_vectors": {
|
|
|
|
// dimensions [canine, feline, young]
|
|
|
|
"animal": [0.9, 0.8, 0.05],
|
|
|
|
// dimensions [negative/positive, energy]
|
|
|
|
"sentiment": [-0.1, 0.55]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": "B",
|
|
|
|
"description": "the kitten scratched the beagle",
|
|
|
|
"_vectors": {
|
|
|
|
// dimensions [canine, feline, young]
|
|
|
|
"animal": [0.8, 0.9, 0.5],
|
|
|
|
// dimensions [negative/positive, energy]
|
|
|
|
"sentiment": [-0.2, 0.65]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": "C",
|
|
|
|
"description": "the dog had to stay alone today",
|
|
|
|
"_vectors": {
|
|
|
|
// dimensions [canine, feline, young]
|
|
|
|
"animal": [0.85, 0.02, 0.1],
|
|
|
|
// dimensions [negative/positive, energy]
|
|
|
|
"sentiment": [-1.0, 0.1]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": "D",
|
|
|
|
"description": "the little boy pets the puppy",
|
|
|
|
"_vectors": {
|
|
|
|
// dimensions [canine, feline, young]
|
|
|
|
"animal": [0.8, 0.09, 0.8],
|
|
|
|
// dimensions [negative/positive, energy]
|
|
|
|
"sentiment": [0.8, 0.3]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
])
|
|
|
|
});
|