479 lines
14 KiB
Rust
Raw Normal View History

2022-10-20 18:00:07 +02:00
use serde_json::json;
2022-04-19 16:49:38 +02:00
use super::*;
use crate::common::Server;
#[actix_rt::test]
async fn formatted_contain_wildcard() {
let server = Server::new().await;
let index = server.index("test");
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();
index.add_documents(documents, None).await;
index.wait_task(1).await;
index.search(json!({ "q": "pesti", "attributesToRetrieve": ["father", "mother"], "attributesToHighlight": ["father", "mother", "*"], "attributesToCrop": ["doggos"], "showMatchesPosition": true }),
|response, code|
{
assert_eq!(code, 200, "{}", response);
assert_eq!(
response["hits"][0],
json!({
"_formatted": {
"id": "852",
"cattos": "<em>pesti</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
2022-10-20 18:00:07 +02:00
.search(json!({ "q": "pesti", "attributesToRetrieve": ["*"] }), |response, code| {
assert_eq!(code, 200, "{}", response);
assert_eq!(
response["hits"][0],
json!({
"id": 852,
"cattos": "pesti",
})
);
})
2022-04-19 16:49:38 +02:00
.await;
2022-06-28 13:01:18 +02:00
index
.search(
json!({ "q": "pesti", "attributesToRetrieve": ["*"], "attributesToHighlight": ["id"], "showMatchesPosition": true }),
2022-06-28 13:01:18 +02:00
|response, code| {
assert_eq!(code, 200, "{}", response);
assert_eq!(
response["hits"][0],
json!({
"id": 852,
"cattos": "pesti",
"_formatted": {
"id": "852",
"cattos": "pesti",
},
"_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(
2022-04-19 16:49:38 +02:00
json!({ "q": "pesti", "attributesToRetrieve": ["*"], "attributesToCrop": ["*"] }),
2022-06-28 13:01:18 +02:00
|response, code| {
assert_eq!(code, 200, "{}", response);
assert_eq!(
response["hits"][0],
json!({
"id": 852,
"cattos": "pesti",
"_formatted": {
"id": "852",
"cattos": "pesti",
}
})
);
},
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!({ "q": "pesti", "attributesToCrop": ["*"] }), |response, code| {
assert_eq!(code, 200, "{}", response);
assert_eq!(
response["hits"][0],
json!({
"id": 852,
"cattos": "pesti",
"_formatted": {
"id": "852",
2022-06-28 13:01:18 +02:00
"cattos": "pesti",
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 server = Server::new().await;
let index = server.index("test");
let documents = NESTED_DOCUMENTS.clone();
index.add_documents(documents, None).await;
index.wait_task(0).await;
2022-06-28 13:01:18 +02:00
index
2022-10-20 18:00:07 +02:00
.search(json!({ "q": "pesti", "attributesToRetrieve": ["doggos"] }), |response, code| {
assert_eq!(code, 200, "{}", response);
assert_eq!(
response["hits"][0],
json!({
"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
.search(
json!({ "q": "pesti", "attributesToRetrieve": ["doggos.name"] }),
|response, code| {
assert_eq!(code, 200, "{}", response);
assert_eq!(
response["hits"][0],
json!({
"doggos": [
{
"name": "bobby",
},
{
"name": "buddy",
},
],
})
);
},
)
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);
assert_eq!(
response["hits"][0],
json!({
"doggos": [
{
"name": "bobby",
},
{
"name": "buddy",
},
],
"_matchesPosition": {"doggos.name": [{"start": 0, "length": 5}]},
})
);
}
2022-05-18 10:26:52 +02:00
)
.await;
2022-06-28 13:01:18 +02:00
index
.search(json!({ "q": "pesti", "attributesToRetrieve": [], "attributesToHighlight": ["doggos.name"] }),
|response, code| {
assert_eq!(code, 200, "{}", response);
assert_eq!(
response["hits"][0],
json!({
"_formatted": {
"doggos": [
{
"name": "bobby",
},
{
"name": "buddy",
},
],
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!({ "q": "pesti", "attributesToRetrieve": [], "attributesToCrop": ["doggos.name"] }),
|response, code| {
assert_eq!(code, 200, "{}", response);
assert_eq!(
response["hits"][0],
json!({
"_formatted": {
"doggos": [
{
"name": "bobby",
},
{
"name": "buddy",
},
],
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!({ "q": "pesti", "attributesToRetrieve": ["doggos.name"], "attributesToHighlight": ["doggos.age"] }),
|response, code| {
2022-04-19 16:49:38 +02:00
assert_eq!(code, 200, "{}", response);
assert_eq!(
response["hits"][0],
json!({
"doggos": [
{
"name": "bobby",
},
{
"name": "buddy",
},
],
"_formatted": {
"doggos": [
{
"name": "bobby",
"age": "2",
},
{
"name": "buddy",
"age": "4",
},
],
},
})
);
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": "pesti", "attributesToRetrieve": [], "attributesToHighlight": ["doggos.age"], "attributesToCrop": ["doggos.name"] }),
|response, code| {
assert_eq!(code, 200, "{}", response);
2022-04-19 16:49:38 +02:00
assert_eq!(
response["hits"][0],
json!({
"_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().await;
let index = server.index("test");
// 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();
index.add_documents(documents, None).await;
index.wait_task(1).await;
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);
assert_eq!(
response["hits"][0],
json!({
"id": 852,
})
);
})
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);
assert_eq!(
response["hits"][0],
json!({
"id": 852,
})
);
})
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);
assert_eq!(
response["hits"][0],
json!({
"id": 852,
"_formatted": {
"id": "852",
}
})
);
})
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);
assert_eq!(
response["hits"][0],
json!({
"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);
assert_eq!(
response["hits"][0],
json!({
"id": 852,
"_formatted": {
"id": "852",
}
})
);
},
)
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);
assert_eq!(
response["hits"][0],
json!({
"id": 852,
})
);
})
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);
assert_eq!(
response["hits"][0],
json!({
"id": 852,
})
);
})
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);
assert_eq!(response["hits"][0], json!({}));
})
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);
assert_eq!(response["hits"][0], json!({}));
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);
assert_eq!(
response["hits"][0],
json!({
"_formatted": {
"id": "852",
}
})
);
},
)
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);
assert_eq!(
response["hits"][0],
json!({
"_formatted": {
"id": "852",
}
})
);
},
)
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().await;
let index = server.index("test");
let documents = json!([
{ "id": 0, "title": "この度、クーポンで無料で頂きました。" },
{ "id": 1, "title": "大卫到了扫罗那里" },
]);
index.add_documents(documents, None).await;
index.wait_task(0).await;
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;
}