MeiliSearch/crates/meilisearch/tests/search/formatted.rs

569 lines
17 KiB
Rust
Raw Normal View History

2023-06-07 10:45:25 +02:00
use insta::{allow_duplicates, assert_json_snapshot};
2022-10-20 18:00:07 +02:00
2022-04-19 16:49:38 +02:00
use super::*;
use crate::common::Server;
2023-09-11 16:50:53 +02:00
use crate::json;
2022-04-19 16:49:38 +02:00
#[actix_rt::test]
async fn formatted_contain_wildcard() {
let server = Server::new_shared();
let index = server.unique_index();
2022-04-19 16:49:38 +02:00
2022-10-20 18:00:07 +02:00
index.update_settings(json!({ "displayedAttributes": ["id", "cattos"] })).await;
2022-04-19 16:49:38 +02:00
let documents = NESTED_DOCUMENTS.clone();
let (response, _) = index.add_documents(documents, None).await;
index.wait_task(response.uid()).await;
2022-04-19 16:49:38 +02:00
2023-03-29 14:36:17 +02:00
index.search(json!({ "q": "pésti", "attributesToRetrieve": ["father", "mother"], "attributesToHighlight": ["father", "mother", "*"], "attributesToCrop": ["doggos"], "showMatchesPosition": true }),
|response, code|
{
assert_eq!(code, 200, "{}", response);
2023-06-07 10:45:25 +02:00
allow_duplicates! {
assert_json_snapshot!(response["hits"][0],
{ "._rankingScore" => "[score]" },
@r###"
{
"_formatted": {
"id": "852",
"cattos": "<em>pésti</em>"
},
"_matchesPosition": {
"cattos": [
{
"start": 0,
"length": 5
}
]
}
}
"###);
}
}
)
.await;
2022-04-19 16:49:38 +02:00
2022-06-28 13:01:18 +02:00
index
2023-03-29 14:36:17 +02:00
.search(json!({ "q": "pésti", "attributesToRetrieve": ["*"] }), |response, code| {
2022-10-20 18:00:07 +02:00
assert_eq!(code, 200, "{}", response);
2023-06-07 10:45:25 +02:00
allow_duplicates! {
assert_json_snapshot!(response["hits"][0],
{ "._rankingScore" => "[score]" },
@r###"
{
"id": 852,
"cattos": "pésti"
}
"###)
}
2022-10-20 18:00:07 +02:00
})
2022-04-19 16:49:38 +02:00
.await;
2022-06-28 13:01:18 +02:00
index
.search(
2023-03-29 14:36:17 +02:00
json!({ "q": "pésti", "attributesToRetrieve": ["*"], "attributesToHighlight": ["id"], "showMatchesPosition": true }),
2022-06-28 13:01:18 +02:00
|response, code| {
assert_eq!(code, 200, "{}", response);
2023-06-07 10:45:25 +02:00
allow_duplicates! {
assert_json_snapshot!(response["hits"][0],
{ "._rankingScore" => "[score]" },
@r###"
{
"id": 852,
"cattos": "pésti",
"_formatted": {
"id": "852",
"cattos": "pésti"
},
"_matchesPosition": {
"cattos": [
{
"start": 0,
"length": 5
}
]
}
}
"###)
}
})
2022-04-19 16:49:38 +02:00
.await;
2022-06-28 13:01:18 +02:00
index
.search(
2023-03-29 14:36:17 +02:00
json!({ "q": "pésti", "attributesToRetrieve": ["*"], "attributesToCrop": ["*"] }),
2022-06-28 13:01:18 +02:00
|response, code| {
assert_eq!(code, 200, "{}", response);
2023-06-07 10:45:25 +02:00
allow_duplicates! {
assert_json_snapshot!(response["hits"][0],
{ "._rankingScore" => "[score]" },
@r###"
{
"id": 852,
"cattos": "pésti",
"_formatted": {
"id": "852",
"cattos": "pésti"
}
}
"###);
}
2022-06-28 13:01:18 +02:00
},
2022-04-19 16:49:38 +02:00
)
.await;
2022-06-28 13:01:18 +02:00
index
2023-03-29 14:36:17 +02:00
.search(json!({ "q": "pésti", "attributesToCrop": ["*"] }), |response, code| {
2022-10-20 18:00:07 +02:00
assert_eq!(code, 200, "{}", response);
2023-06-07 10:45:25 +02:00
allow_duplicates! {
assert_json_snapshot!(response["hits"][0],
{ "._rankingScore" => "[score]" },
@r###"
{
"id": 852,
"cattos": "pésti",
"_formatted": {
"id": "852",
"cattos": "pésti"
}
}
"###)
}
2022-10-20 18:00:07 +02:00
})
2022-04-19 16:49:38 +02:00
.await;
}
#[actix_rt::test]
async fn format_nested() {
let index = shared_index_with_nested_documents().await;
2022-04-19 16:49:38 +02:00
2022-06-28 13:01:18 +02:00
index
2023-03-29 14:36:17 +02:00
.search(json!({ "q": "pésti", "attributesToRetrieve": ["doggos"] }), |response, code| {
2022-10-20 18:00:07 +02:00
assert_eq!(code, 200, "{}", response);
2023-06-07 10:45:25 +02:00
allow_duplicates! {
assert_json_snapshot!(response["hits"][0],
{ "._rankingScore" => "[score]" },
@r###"
{
"doggos": [
{
"name": "bobby",
"age": 2
},
{
"name": "buddy",
"age": 4
}
]
}
"###)
}
2022-10-20 18:00:07 +02:00
})
2022-04-19 16:49:38 +02:00
.await;
2022-06-28 13:01:18 +02:00
index
.search(
2023-03-29 14:36:17 +02:00
json!({ "q": "pésti", "attributesToRetrieve": ["doggos.name"] }),
2022-06-28 13:01:18 +02:00
|response, code| {
assert_eq!(code, 200, "{}", response);
2023-06-07 10:45:25 +02:00
allow_duplicates! {
assert_json_snapshot!(response["hits"][0],
{ "._rankingScore" => "[score]" },
@r###"
{
"doggos": [
{
"name": "bobby"
},
{
"name": "buddy"
}
]
}
"###)
}
2022-06-28 13:01:18 +02:00
},
)
2022-04-19 16:49:38 +02:00
.await;
2022-06-28 13:01:18 +02:00
index
.search(
json!({ "q": "bobby", "attributesToRetrieve": ["doggos.name"], "showMatchesPosition": true }),
2022-06-28 13:01:18 +02:00
|response, code| {
assert_eq!(code, 200, "{}", response);
2023-06-07 10:45:25 +02:00
allow_duplicates! {
assert_json_snapshot!(response["hits"][0],
{ "._rankingScore" => "[score]" },
@r###"
{
"doggos": [
{
"name": "bobby"
},
{
"name": "buddy"
}
],
"_matchesPosition": {
"doggos.name": [
{
"start": 0,
"length": 5,
"indices": [
0
]
2023-06-07 10:45:25 +02:00
}
]
}
}
"###)
}
2022-06-28 13:01:18 +02:00
}
2022-05-18 10:26:52 +02:00
)
.await;
2022-06-28 13:01:18 +02:00
index
2023-03-29 14:36:17 +02:00
.search(json!({ "q": "pésti", "attributesToRetrieve": [], "attributesToHighlight": ["doggos.name"] }),
2022-06-28 13:01:18 +02:00
|response, code| {
assert_eq!(code, 200, "{}", response);
2023-06-07 10:45:25 +02:00
allow_duplicates! {
assert_json_snapshot!(response["hits"][0],
{ "._rankingScore" => "[score]" },
@r###"
{
"_formatted": {
"doggos": [
{
"name": "bobby"
},
{
"name": "buddy"
}
]
}
}
"###)
}
2022-04-19 16:49:38 +02:00
})
.await;
2022-06-28 13:01:18 +02:00
index
2023-03-29 14:36:17 +02:00
.search(json!({ "q": "pésti", "attributesToRetrieve": [], "attributesToCrop": ["doggos.name"] }),
2022-06-28 13:01:18 +02:00
|response, code| {
assert_eq!(code, 200, "{}", response);
2023-06-07 10:45:25 +02:00
allow_duplicates! {
assert_json_snapshot!(response["hits"][0],
{ "._rankingScore" => "[score]" },
@r###"
{
"_formatted": {
"doggos": [
{
"name": "bobby"
},
{
"name": "buddy"
}
]
}
}
"###)
}
2022-04-19 16:49:38 +02:00
})
.await;
2022-06-28 13:01:18 +02:00
index
2023-03-29 14:36:17 +02:00
.search(json!({ "q": "pésti", "attributesToRetrieve": ["doggos.name"], "attributesToHighlight": ["doggos.age"] }),
2022-06-28 13:01:18 +02:00
|response, code| {
2022-04-19 16:49:38 +02:00
assert_eq!(code, 200, "{}", response);
2023-06-07 10:45:25 +02:00
allow_duplicates! {
assert_json_snapshot!(response["hits"][0],
{ "._rankingScore" => "[score]" },
@r###"
{
"doggos": [
{
"name": "bobby"
2022-04-19 16:49:38 +02:00
},
2023-06-07 10:45:25 +02:00
{
"name": "buddy"
}
],
"_formatted": {
"doggos": [
{
"name": "bobby",
"age": "2"
},
{
"name": "buddy",
"age": "4"
}
]
}
}
"###)
}
})
2022-04-19 16:49:38 +02:00
.await;
2022-06-28 13:01:18 +02:00
index
2023-03-29 14:36:17 +02:00
.search(json!({ "q": "pésti", "attributesToRetrieve": [], "attributesToHighlight": ["doggos.age"], "attributesToCrop": ["doggos.name"] }),
2022-06-28 13:01:18 +02:00
|response, code| {
assert_eq!(code, 200, "{}", response);
2023-06-07 10:45:25 +02:00
allow_duplicates! {
assert_json_snapshot!(response["hits"][0],
{ "._rankingScore" => "[score]" },
@r###"
2022-04-19 16:49:38 +02:00
{
2023-06-07 10:45:25 +02:00
"_formatted": {
"doggos": [
{
"name": "bobby",
"age": "2"
},
{
"name": "buddy",
"age": "4"
}
]
}
}
"###)
}
2022-06-28 13:01:18 +02:00
}
)
.await;
2022-04-19 16:49:38 +02:00
}
#[actix_rt::test]
async fn displayedattr_2_smol() {
let server = Server::new_shared();
let index = server.unique_index();
2022-04-19 16:49:38 +02:00
// not enough displayed for the other settings
2022-10-20 18:00:07 +02:00
index.update_settings(json!({ "displayedAttributes": ["id"] })).await;
2022-04-19 16:49:38 +02:00
let documents = NESTED_DOCUMENTS.clone();
let (response, _) = index.add_documents(documents, None).await;
index.wait_task(response.uid()).await;
2022-04-19 16:49:38 +02:00
2022-06-28 13:01:18 +02:00
index
.search(json!({ "attributesToRetrieve": ["father", "id"], "attributesToHighlight": ["mother"], "attributesToCrop": ["cattos"] }),
|response, code| {
2022-04-19 16:49:38 +02:00
assert_eq!(code, 200, "{}", response);
2023-06-07 10:45:25 +02:00
allow_duplicates! {
assert_json_snapshot!(response["hits"][0],
{ "._rankingScore" => "[score]" },
@r###"
{
"id": 852
}
"###)
}
2022-04-19 16:49:38 +02:00
})
2022-06-28 13:01:18 +02:00
.await;
2022-04-19 16:49:38 +02:00
2022-06-28 13:01:18 +02:00
index
2022-10-20 18:00:07 +02:00
.search(json!({ "attributesToRetrieve": ["id"] }), |response, code| {
assert_eq!(code, 200, "{}", response);
2023-06-07 10:45:25 +02:00
allow_duplicates! {
assert_json_snapshot!(response["hits"][0],
{ "._rankingScore" => "[score]" },
@r###"
{
"id": 852
}
"###)
}
2022-10-20 18:00:07 +02:00
})
2022-04-19 16:49:38 +02:00
.await;
2022-06-28 13:01:18 +02:00
index
2022-10-20 18:00:07 +02:00
.search(json!({ "attributesToHighlight": ["id"] }), |response, code| {
assert_eq!(code, 200, "{}", response);
2023-06-07 10:45:25 +02:00
allow_duplicates! {
assert_json_snapshot!(response["hits"][0],
{ "._rankingScore" => "[score]" },
@r###"
{
"id": 852,
"_formatted": {
"id": "852"
}
}
"###)
}
2022-10-20 18:00:07 +02:00
})
2022-04-19 16:49:38 +02:00
.await;
2022-06-28 13:01:18 +02:00
index
.search(json!({ "attributesToCrop": ["id"] }), |response, code| {
assert_eq!(code, 200, "{}", response);
2023-06-07 10:45:25 +02:00
allow_duplicates! {
assert_json_snapshot!(response["hits"][0],
{ "._rankingScore" => "[score]" },
@r###"
{
"id": 852,
"_formatted": {
"id": "852"
}
}
"###)
}
2022-04-19 16:49:38 +02:00
})
2022-06-28 13:01:18 +02:00
.await;
2022-04-19 16:49:38 +02:00
2022-06-28 13:01:18 +02:00
index
.search(
json!({ "attributesToHighlight": ["id"], "attributesToCrop": ["id"] }),
|response, code| {
assert_eq!(code, 200, "{}", response);
2023-06-07 10:45:25 +02:00
allow_duplicates! {
assert_json_snapshot!(response["hits"][0],
{ "._rankingScore" => "[score]" },
@r###"
{
"id": 852,
"_formatted": {
"id": "852"
}
}
"###)
}
2022-06-28 13:01:18 +02:00
},
)
2022-04-19 16:49:38 +02:00
.await;
2022-06-28 13:01:18 +02:00
index
2022-10-20 18:00:07 +02:00
.search(json!({ "attributesToHighlight": ["cattos"] }), |response, code| {
assert_eq!(code, 200, "{}", response);
2023-06-07 10:45:25 +02:00
allow_duplicates! {
assert_json_snapshot!(response["hits"][0],
{ "._rankingScore" => "[score]" },
@r###"
{
"id": 852
}
"###)
}
2022-10-20 18:00:07 +02:00
})
2022-04-19 16:49:38 +02:00
.await;
2022-06-28 13:01:18 +02:00
index
2022-10-20 18:00:07 +02:00
.search(json!({ "attributesToCrop": ["cattos"] }), |response, code| {
assert_eq!(code, 200, "{}", response);
2023-06-07 10:45:25 +02:00
allow_duplicates! {
assert_json_snapshot!(response["hits"][0],
{ "._rankingScore" => "[score]" },
@r###"
{
"id": 852
}
"###)
}
2022-10-20 18:00:07 +02:00
})
2022-04-19 16:49:38 +02:00
.await;
2022-06-28 13:01:18 +02:00
index
2022-10-20 18:00:07 +02:00
.search(json!({ "attributesToRetrieve": ["cattos"] }), |response, code| {
assert_eq!(code, 200, "{}", response);
2023-06-07 10:45:25 +02:00
allow_duplicates! {
assert_json_snapshot!(response["hits"][0],
{ "._rankingScore" => "[score]" },
@"{}")
}
2022-10-20 18:00:07 +02:00
})
2022-04-19 16:49:38 +02:00
.await;
2022-06-28 13:01:18 +02:00
index
.search(
json!({ "attributesToRetrieve": ["cattos"], "attributesToHighlight": ["cattos"], "attributesToCrop": ["cattos"] }),
|response, code| {
2022-04-19 16:49:38 +02:00
assert_eq!(code, 200, "{}", response);
2023-06-07 10:45:25 +02:00
allow_duplicates! {
assert_json_snapshot!(response["hits"][0],
{ "._rankingScore" => "[score]" },
@"{}")
}
2022-04-19 16:49:38 +02:00
2022-06-28 13:01:18 +02:00
}
2022-04-19 16:49:38 +02:00
)
.await;
2022-06-28 13:01:18 +02:00
index
.search(
json!({ "attributesToRetrieve": ["cattos"], "attributesToHighlight": ["id"] }),
|response, code| {
assert_eq!(code, 200, "{}", response);
2023-06-07 10:45:25 +02:00
allow_duplicates! {
assert_json_snapshot!(response["hits"][0],
{ "._rankingScore" => "[score]" },
@r###"
{
"_formatted": {
"id": "852"
}
}
"###)
}
2022-06-28 13:01:18 +02:00
},
)
2022-04-19 16:49:38 +02:00
.await;
2022-06-28 13:01:18 +02:00
index
.search(
json!({ "attributesToRetrieve": ["cattos"], "attributesToCrop": ["id"] }),
|response, code| {
assert_eq!(code, 200, "{}", response);
2023-06-07 10:45:25 +02:00
allow_duplicates! {
assert_json_snapshot!(response["hits"][0],
{ "._rankingScore" => "[score]" },
@r###"
{
"_formatted": {
"id": "852"
}
}
"###)
}
2022-06-28 13:01:18 +02:00
},
)
2022-04-19 16:49:38 +02:00
.await;
}
2023-02-22 15:47:15 +01:00
#[cfg(feature = "default")]
#[actix_rt::test]
async fn test_cjk_highlight() {
let server = Server::new_shared();
let index = server.unique_index();
2023-02-22 15:47:15 +01:00
let documents = json!([
{ "id": 0, "title": "この度、クーポンで無料で頂きました。" },
{ "id": 1, "title": "大卫到了扫罗那里" },
]);
let (response, _) = index.add_documents(documents, None).await;
index.wait_task(response.uid()).await;
2023-02-22 15:47:15 +01:00
index
.search(json!({"q": "", "attributesToHighlight": ["title"]}), |response, code| {
assert_eq!(code, 200, "{}", response);
assert_eq!(
response["hits"][0]["_formatted"]["title"],
json!("この度、クーポン<em>で</em>無料<em>で</em>頂きました。")
);
})
.await;
index
.search(json!({"q": "大卫", "attributesToHighlight": ["title"]}), |response, code| {
assert_eq!(code, 200, "{}", response);
assert_eq!(
response["hits"][0]["_formatted"]["title"],
json!("<em>大卫</em>到了扫罗那里")
);
})
.await;
}