reuse an index with already indexed documents instead of reindexing from scratch

This commit is contained in:
Tamo 2024-07-18 18:13:42 +02:00
parent 953d3a44bd
commit 993264227d
4 changed files with 81 additions and 176 deletions

View File

@ -17,7 +17,9 @@ mod search_queue;
use meilisearch::Opt; use meilisearch::Opt;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use tempfile::TempDir; use tempfile::TempDir;
use uuid::Uuid;
use crate::common::index::Index;
use crate::common::{default_settings, Server, Value}; use crate::common::{default_settings, Server, Value};
use crate::json; use crate::json;
@ -51,6 +53,29 @@ static DOCUMENTS: Lazy<Value> = Lazy::new(|| {
]) ])
}); });
pub async fn shared_index_with_documents() -> &'static Index<'static> {
static INDEX: Lazy<Index<'static>> = Lazy::new(|| {
let server = Server::new_shared();
let uuid = Uuid::new_v4();
let index = server.index(uuid.to_string());
index
});
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 documents = DOCUMENTS.clone();
let (response, _code) = INDEX.add_documents(documents, None).await;
INDEX.wait_task(response.uid()).await.succeeded();
let (response, _code) = INDEX
.update_settings(
json!({"filterableAttributes": ["id", "title"], "sortableAttributes": ["id", "title"]}),
)
.await;
INDEX.wait_task(response.uid()).await.succeeded();
}
&INDEX
}
static SCORE_DOCUMENTS: Lazy<Value> = Lazy::new(|| { static SCORE_DOCUMENTS: Lazy<Value> = Lazy::new(|| {
json!([ json!([
{ {
@ -135,6 +160,29 @@ static NESTED_DOCUMENTS: Lazy<Value> = Lazy::new(|| {
]) ])
}); });
pub async fn shared_index_with_nested_documents() -> &'static Index<'static> {
static INDEX: Lazy<Index<'static>> = Lazy::new(|| {
let server = Server::new_shared();
let uuid = Uuid::new_v4();
let index = server.index(uuid.to_string());
index
});
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 documents = NESTED_DOCUMENTS.clone();
let (response, _code) = INDEX.add_documents(documents, None).await;
INDEX.wait_task(response.uid()).await.succeeded();
let (response, _code) = INDEX
.update_settings(
json!({"filterableAttributes": ["father", "doggos"], "sortableAttributes": ["doggos"]}),
)
.await;
INDEX.wait_task(response.uid()).await.succeeded();
}
&INDEX
}
static FRUITS_DOCUMENTS: Lazy<Value> = Lazy::new(|| { static FRUITS_DOCUMENTS: Lazy<Value> = Lazy::new(|| {
json!([ json!([
{ {
@ -210,13 +258,7 @@ static VECTOR_DOCUMENTS: Lazy<Value> = Lazy::new(|| {
#[actix_rt::test] #[actix_rt::test]
async fn simple_placeholder_search() { async fn simple_placeholder_search() {
let server = Server::new().await; let index = shared_index_with_documents().await;
let index = server.index("basic");
let documents = DOCUMENTS.clone();
index.add_documents(documents, None).await;
index.wait_task(0).await;
index index
.search(json!({}), |response, code| { .search(json!({}), |response, code| {
assert_eq!(code, 200, "{}", response); assert_eq!(code, 200, "{}", response);
@ -224,11 +266,7 @@ async fn simple_placeholder_search() {
}) })
.await; .await;
let index = server.index("nested"); let index = shared_index_with_nested_documents().await;
let documents = NESTED_DOCUMENTS.clone();
index.add_documents(documents, None).await;
index.wait_task(1).await;
index index
.search(json!({}), |response, code| { .search(json!({}), |response, code| {
assert_eq!(code, 200, "{}", response); assert_eq!(code, 200, "{}", response);
@ -239,13 +277,7 @@ async fn simple_placeholder_search() {
#[actix_rt::test] #[actix_rt::test]
async fn simple_search() { async fn simple_search() {
let server = Server::new().await; let index = shared_index_with_documents().await;
let index = server.index("test");
let documents = DOCUMENTS.clone();
index.add_documents(documents, None).await;
index.wait_task(0).await;
index index
.search(json!({"q": "glass"}), |response, code| { .search(json!({"q": "glass"}), |response, code| {
assert_eq!(code, 200, "{}", response); assert_eq!(code, 200, "{}", response);
@ -253,11 +285,7 @@ async fn simple_search() {
}) })
.await; .await;
let index = server.index("nested"); let index = shared_index_with_nested_documents().await;
let documents = NESTED_DOCUMENTS.clone();
index.add_documents(documents, None).await;
index.wait_task(1).await;
index index
.search(json!({"q": "pésti"}), |response, code| { .search(json!({"q": "pésti"}), |response, code| {
assert_eq!(code, 200, "{}", response); assert_eq!(code, 200, "{}", response);
@ -289,13 +317,7 @@ async fn phrase_search_with_stop_word() {
#[actix_rt::test] #[actix_rt::test]
async fn negative_phrase_search() { async fn negative_phrase_search() {
let server = Server::new().await; let index = shared_index_with_documents().await;
let index = server.index("test");
let documents = DOCUMENTS.clone();
index.add_documents(documents, None).await;
index.wait_task(0).await;
index index
.search(json!({"q": "-\"train your dragon\"" }), |response, code| { .search(json!({"q": "-\"train your dragon\"" }), |response, code| {
assert_eq!(code, 200, "{}", response); assert_eq!(code, 200, "{}", response);
@ -311,13 +333,7 @@ async fn negative_phrase_search() {
#[actix_rt::test] #[actix_rt::test]
async fn negative_word_search() { async fn negative_word_search() {
let server = Server::new().await; let index = shared_index_with_documents().await;
let index = server.index("test");
let documents = DOCUMENTS.clone();
index.add_documents(documents, None).await;
index.wait_task(0).await;
index index
.search(json!({"q": "-escape" }), |response, code| { .search(json!({"q": "-escape" }), |response, code| {
assert_eq!(code, 200, "{}", response); assert_eq!(code, 200, "{}", response);
@ -342,13 +358,7 @@ async fn negative_word_search() {
#[actix_rt::test] #[actix_rt::test]
async fn non_negative_search() { async fn non_negative_search() {
let server = Server::new().await; let index = shared_index_with_documents().await;
let index = server.index("test");
let documents = DOCUMENTS.clone();
index.add_documents(documents, None).await;
index.wait_task(0).await;
index index
.search(json!({"q": "- escape" }), |response, code| { .search(json!({"q": "- escape" }), |response, code| {
assert_eq!(code, 200, "{}", response); assert_eq!(code, 200, "{}", response);
@ -440,13 +450,7 @@ async fn test_thai_language() {
#[actix_rt::test] #[actix_rt::test]
async fn search_multiple_params() { async fn search_multiple_params() {
let server = Server::new().await; let index = shared_index_with_documents().await;
let index = server.index("test");
let documents = DOCUMENTS.clone();
index.add_documents(documents, None).await;
index.wait_task(0).await;
index index
.search( .search(
json!({ json!({
@ -463,11 +467,7 @@ async fn search_multiple_params() {
) )
.await; .await;
let index = server.index("nested"); let index = shared_index_with_nested_documents().await;
let documents = NESTED_DOCUMENTS.clone();
index.add_documents(documents, None).await;
index.wait_task(1).await;
index index
.search( .search(
json!({ json!({
@ -553,15 +553,7 @@ async fn search_with_filter_string_notation() {
#[actix_rt::test] #[actix_rt::test]
async fn search_with_filter_array_notation() { async fn search_with_filter_array_notation() {
let server = Server::new().await; let index = shared_index_with_documents().await;
let index = server.index("test");
index.update_settings(json!({"filterableAttributes": ["title"]})).await;
let documents = DOCUMENTS.clone();
index.add_documents(documents, None).await;
index.wait_task(1).await;
let (response, code) = index let (response, code) = index
.search_post(json!({ .search_post(json!({
"filter": ["title = Gläss"] "filter": ["title = Gläss"]
@ -607,15 +599,7 @@ async fn search_with_contains_filter() {
#[actix_rt::test] #[actix_rt::test]
async fn search_with_sort_on_numbers() { async fn search_with_sort_on_numbers() {
let server = Server::new().await; let index = shared_index_with_documents().await;
let index = server.index("test");
index.update_settings(json!({"sortableAttributes": ["id"]})).await;
let documents = DOCUMENTS.clone();
index.add_documents(documents, None).await;
index.wait_task(1).await;
index index
.search( .search(
json!({ json!({
@ -628,14 +612,7 @@ async fn search_with_sort_on_numbers() {
) )
.await; .await;
let index = server.index("nested"); let index = shared_index_with_nested_documents().await;
index.update_settings(json!({"sortableAttributes": ["doggos.age"]})).await;
let documents = NESTED_DOCUMENTS.clone();
index.add_documents(documents, None).await;
index.wait_task(3).await;
index index
.search( .search(
json!({ json!({
@ -651,15 +628,7 @@ async fn search_with_sort_on_numbers() {
#[actix_rt::test] #[actix_rt::test]
async fn search_with_sort_on_strings() { async fn search_with_sort_on_strings() {
let server = Server::new().await; let index = shared_index_with_documents().await;
let index = server.index("test");
index.update_settings(json!({"sortableAttributes": ["title"]})).await;
let documents = DOCUMENTS.clone();
index.add_documents(documents, None).await;
index.wait_task(1).await;
index index
.search( .search(
json!({ json!({
@ -672,14 +641,7 @@ async fn search_with_sort_on_strings() {
) )
.await; .await;
let index = server.index("nested"); let index = shared_index_with_nested_documents().await;
index.update_settings(json!({"sortableAttributes": ["doggos.name"]})).await;
let documents = NESTED_DOCUMENTS.clone();
index.add_documents(documents, None).await;
index.wait_task(3).await;
index index
.search( .search(
json!({ json!({
@ -695,15 +657,7 @@ async fn search_with_sort_on_strings() {
#[actix_rt::test] #[actix_rt::test]
async fn search_with_multiple_sort() { async fn search_with_multiple_sort() {
let server = Server::new().await; let index = shared_index_with_documents().await;
let index = server.index("test");
index.update_settings(json!({"sortableAttributes": ["id", "title"]})).await;
let documents = DOCUMENTS.clone();
index.add_documents(documents, None).await;
index.wait_task(1).await;
let (response, code) = index let (response, code) = index
.search_post(json!({ .search_post(json!({
"sort": ["id:asc", "title:desc"] "sort": ["id:asc", "title:desc"]
@ -715,15 +669,7 @@ async fn search_with_multiple_sort() {
#[actix_rt::test] #[actix_rt::test]
async fn search_facet_distribution() { async fn search_facet_distribution() {
let server = Server::new().await; let index = shared_index_with_documents().await;
let index = server.index("test");
index.update_settings(json!({"filterableAttributes": ["title"]})).await;
let documents = DOCUMENTS.clone();
index.add_documents(documents, None).await;
index.wait_task(1).await;
index index
.search( .search(
json!({ json!({
@ -738,13 +684,7 @@ async fn search_facet_distribution() {
) )
.await; .await;
let index = server.index("nested"); let index = shared_index_with_nested_documents().await;
index.update_settings(json!({"filterableAttributes": ["father", "doggos.name"]})).await;
let documents = NESTED_DOCUMENTS.clone();
index.add_documents(documents, None).await;
index.wait_task(3).await;
// TODO: TAMO: fix the test // TODO: TAMO: fix the test
index index
@ -771,9 +711,6 @@ async fn search_facet_distribution() {
) )
.await; .await;
index.update_settings(json!({"filterableAttributes": ["doggos"]})).await;
index.wait_task(4).await;
index index
.search( .search(
json!({ json!({
@ -809,9 +746,6 @@ async fn search_facet_distribution() {
) )
.await; .await;
index.update_settings(json!({"filterableAttributes": ["doggos.name"]})).await;
index.wait_task(5).await;
index index
.search( .search(
json!({ json!({

View File

@ -94,7 +94,7 @@ async fn simple_search_single_index() {
let (response, code) = server let (response, code) = server
.multi_search(json!({"queries": [ .multi_search(json!({"queries": [
{"indexUid" : "test", "q": "glass"}, {"indexUid": "test", "q": "glass"},
{"indexUid": "test", "q": "captain"}, {"indexUid": "test", "q": "captain"},
]})) ]}))
.await; .await;

View File

@ -1,16 +1,11 @@
use crate::common::Server; use crate::common::Server;
use crate::json; use crate::json;
use crate::search::DOCUMENTS;
use super::shared_index_with_documents;
#[actix_rt::test] #[actix_rt::test]
async fn default_search_should_return_estimated_total_hit() { async fn default_search_should_return_estimated_total_hit() {
let server = Server::new().await; let index = shared_index_with_documents().await;
let index = server.index("basic");
let documents = DOCUMENTS.clone();
index.add_documents(documents, None).await;
index.wait_task(0).await;
index index
.search(json!({}), |response, code| { .search(json!({}), |response, code| {
assert_eq!(code, 200, "{}", response); assert_eq!(code, 200, "{}", response);
@ -28,13 +23,7 @@ async fn default_search_should_return_estimated_total_hit() {
#[actix_rt::test] #[actix_rt::test]
async fn simple_search() { async fn simple_search() {
let server = Server::new().await; let index = shared_index_with_documents().await;
let index = server.index("basic");
let documents = DOCUMENTS.clone();
index.add_documents(documents, None).await;
index.wait_task(0).await;
index index
.search(json!({"page": 1}), |response, code| { .search(json!({"page": 1}), |response, code| {
assert_eq!(code, 200, "{}", response); assert_eq!(code, 200, "{}", response);
@ -53,13 +42,7 @@ async fn simple_search() {
#[actix_rt::test] #[actix_rt::test]
async fn page_zero_should_not_return_any_result() { async fn page_zero_should_not_return_any_result() {
let server = Server::new().await; let index = shared_index_with_documents().await;
let index = server.index("basic");
let documents = DOCUMENTS.clone();
index.add_documents(documents, None).await;
index.wait_task(0).await;
index index
.search(json!({"page": 0}), |response, code| { .search(json!({"page": 0}), |response, code| {
assert_eq!(code, 200, "{}", response); assert_eq!(code, 200, "{}", response);
@ -73,13 +56,7 @@ async fn page_zero_should_not_return_any_result() {
#[actix_rt::test] #[actix_rt::test]
async fn hits_per_page_1() { async fn hits_per_page_1() {
let server = Server::new().await; let index = shared_index_with_documents().await;
let index = server.index("basic");
let documents = DOCUMENTS.clone();
index.add_documents(documents, None).await;
index.wait_task(0).await;
index index
.search(json!({"hitsPerPage": 1}), |response, code| { .search(json!({"hitsPerPage": 1}), |response, code| {
assert_eq!(code, 200, "{}", response); assert_eq!(code, 200, "{}", response);
@ -93,13 +70,7 @@ async fn hits_per_page_1() {
#[actix_rt::test] #[actix_rt::test]
async fn hits_per_page_0_should_not_return_any_result() { async fn hits_per_page_0_should_not_return_any_result() {
let server = Server::new().await; let index = shared_index_with_documents().await;
let index = server.index("basic");
let documents = DOCUMENTS.clone();
index.add_documents(documents, None).await;
index.wait_task(0).await;
index index
.search(json!({"hitsPerPage": 0}), |response, code| { .search(json!({"hitsPerPage": 0}), |response, code| {
assert_eq!(code, 200, "{}", response); assert_eq!(code, 200, "{}", response);
@ -113,8 +84,8 @@ async fn hits_per_page_0_should_not_return_any_result() {
#[actix_rt::test] #[actix_rt::test]
async fn ensure_placeholder_search_hit_count_valid() { async fn ensure_placeholder_search_hit_count_valid() {
let server = Server::new().await; let server = Server::new_shared();
let index = server.index("basic"); let index = server.unique_index();
let documents = json!([ let documents = json!([
{ {
@ -143,15 +114,15 @@ async fn ensure_placeholder_search_hit_count_valid() {
"distinct": 3, "distinct": 3,
} }
]); ]);
index.add_documents(documents, None).await; let (task, _code) = index.add_documents(documents, None).await;
index.wait_task(0).await; index.wait_task(task.uid()).await.succeeded();
let (_response, _code) = index let (response, _code) = index
.update_settings( .update_settings(
json!({ "rankingRules": ["distinct:asc"], "distinctAttribute": "distinct"}), json!({ "rankingRules": ["distinct:asc"], "distinctAttribute": "distinct"}),
) )
.await; .await;
index.wait_task(1).await; index.wait_task(response.uid()).await.succeeded();
for page in 0..=4 { for page in 0..=4 {
index index

View File

@ -8,8 +8,8 @@ use crate::json;
async fn index_with_documents<'a>(server: &'a Server, documents: &Value) -> Index<'a> { async fn index_with_documents<'a>(server: &'a Server, documents: &Value) -> Index<'a> {
let index = server.index("test"); let index = server.index("test");
index.add_documents(documents.clone(), None).await; let (task, _code) = index.add_documents(documents.clone(), None).await;
index.wait_task(0).await; index.wait_task(task.uid()).await.succeeded();
index index
} }