meilisearch/meilisearch-http/tests/search_settings.rs

539 lines
13 KiB
Rust
Raw Normal View History

2020-04-03 21:00:41 +08:00
use assert_json_diff::assert_json_eq;
use serde_json::json;
use std::convert::Into;
mod common;
2020-04-16 17:09:47 +08:00
#[actix_rt::test]
async fn search_with_settings_basic() {
let mut server = common::Server::test_server().await;
2020-04-03 21:00:41 +08:00
let config = json!({
"rankingRules": [
"typo",
"words",
"proximity",
"attribute",
"wordsPosition",
"desc(age)",
2020-04-03 21:00:41 +08:00
"exactness",
"desc(balance)"
2020-04-03 21:00:41 +08:00
],
"distinctAttribute": null,
"searchableAttributes": [
"name",
"age",
"color",
"gender",
"email",
"address",
"about"
2020-04-03 21:00:41 +08:00
],
"displayedAttributes": [
"name",
"age",
"gender",
"color",
"email",
"phone",
"address",
"balance"
2020-04-03 21:00:41 +08:00
],
"stopWords": null,
"synonyms": null,
});
2020-04-16 17:09:47 +08:00
server.update_all_settings(config).await;
2020-04-03 21:00:41 +08:00
let query = "q=ea%20exercitation&limit=3";
2020-04-03 21:00:41 +08:00
let expect = json!([
{
"balance": "$2,467.47",
"age": 34,
"color": "blue",
"name": "Patricia Goff",
"gender": "female",
"email": "patriciagoff@chorizon.com",
"phone": "+1 (864) 463-2277",
"address": "866 Hornell Loop, Cresaptown, Ohio, 1700"
2020-04-03 21:00:41 +08:00
},
{
"balance": "$3,344.40",
"age": 35,
"color": "blue",
"name": "Adeline Flynn",
"gender": "female",
"email": "adelineflynn@chorizon.com",
"phone": "+1 (994) 600-2840",
"address": "428 Paerdegat Avenue, Hollymead, Pennsylvania, 948"
2020-04-03 21:00:41 +08:00
},
{
"balance": "$3,394.96",
"age": 25,
"color": "blue",
"name": "Aida Kirby",
"gender": "female",
"email": "aidakirby@chorizon.com",
"phone": "+1 (942) 532-2325",
"address": "797 Engert Avenue, Wilsonia, Idaho, 6532"
2020-04-03 21:00:41 +08:00
}
]);
2020-06-01 20:54:12 +08:00
let (response, _status_code) = server.search_get(query).await;
2020-04-03 21:00:41 +08:00
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
}
2020-04-16 17:09:47 +08:00
#[actix_rt::test]
async fn search_with_settings_stop_words() {
let mut server = common::Server::test_server().await;
2020-04-03 21:00:41 +08:00
let config = json!({
"rankingRules": [
"typo",
"words",
"proximity",
"attribute",
"wordsPosition",
"desc(age)",
2020-04-03 21:00:41 +08:00
"exactness",
"desc(balance)"
2020-04-03 21:00:41 +08:00
],
"distinctAttribute": null,
"searchableAttributes": [
"name",
"age",
"color",
"gender",
"email",
"address",
"about"
2020-04-03 21:00:41 +08:00
],
"displayedAttributes": [
"name",
"age",
"gender",
"color",
"email",
"phone",
"address",
"balance"
2020-04-03 21:00:41 +08:00
],
"stopWords": ["ea"],
2020-04-03 21:00:41 +08:00
"synonyms": null,
});
2020-04-16 17:09:47 +08:00
server.update_all_settings(config).await;
2020-04-03 21:00:41 +08:00
let query = "q=ea%20exercitation&limit=3";
2020-04-03 21:00:41 +08:00
let expect = json!([
{
"balance": "$1,921.58",
"age": 31,
"color": "Green",
"name": "Harper Carson",
"gender": "male",
"email": "harpercarson@chorizon.com",
"phone": "+1 (912) 430-3243",
"address": "883 Dennett Place, Knowlton, New Mexico, 9219"
2020-04-03 21:00:41 +08:00
},
{
"balance": "$1,706.13",
"age": 27,
"color": "Green",
"name": "Cherry Orr",
"gender": "female",
"email": "cherryorr@chorizon.com",
"phone": "+1 (995) 479-3174",
"address": "442 Beverly Road, Ventress, New Mexico, 3361"
2020-04-03 21:00:41 +08:00
},
{
"balance": "$1,476.39",
"age": 28,
"color": "brown",
"name": "Maureen Dale",
"gender": "female",
"email": "maureendale@chorizon.com",
"phone": "+1 (984) 538-3684",
"address": "817 Newton Street, Bannock, Wyoming, 1468"
2020-04-03 21:00:41 +08:00
}
]);
2020-06-01 20:54:12 +08:00
let (response, _status_code) = server.search_get(query).await;
2020-04-03 21:00:41 +08:00
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
}
2020-04-16 17:09:47 +08:00
#[actix_rt::test]
async fn search_with_settings_synonyms() {
let mut server = common::Server::test_server().await;
2020-04-03 21:00:41 +08:00
let config = json!({
"rankingRules": [
"typo",
"words",
"proximity",
"attribute",
"wordsPosition",
"desc(age)",
2020-04-03 21:00:41 +08:00
"exactness",
"desc(balance)"
2020-04-03 21:00:41 +08:00
],
"distinctAttribute": null,
"searchableAttributes": [
"name",
"age",
"color",
"gender",
"email",
"address",
"about"
2020-04-03 21:00:41 +08:00
],
"displayedAttributes": [
"name",
"age",
"gender",
"color",
"email",
"phone",
"address",
"balance"
2020-04-03 21:00:41 +08:00
],
"stopWords": null,
"synonyms": {
"application": [
"exercitation"
]
2020-04-03 21:00:41 +08:00
},
});
2020-04-16 17:09:47 +08:00
server.update_all_settings(config).await;
2020-04-03 21:00:41 +08:00
let query = "q=application&limit=3";
2020-04-03 21:00:41 +08:00
let expect = json!([
{
"balance": "$1,921.58",
"age": 31,
"color": "Green",
"name": "Harper Carson",
"gender": "male",
"email": "harpercarson@chorizon.com",
"phone": "+1 (912) 430-3243",
"address": "883 Dennett Place, Knowlton, New Mexico, 9219"
2020-04-03 21:00:41 +08:00
},
{
"balance": "$1,706.13",
"age": 27,
"color": "Green",
"name": "Cherry Orr",
"gender": "female",
"email": "cherryorr@chorizon.com",
"phone": "+1 (995) 479-3174",
"address": "442 Beverly Road, Ventress, New Mexico, 3361"
2020-04-03 21:00:41 +08:00
},
{
"balance": "$1,476.39",
"age": 28,
"color": "brown",
"name": "Maureen Dale",
"gender": "female",
"email": "maureendale@chorizon.com",
"phone": "+1 (984) 538-3684",
"address": "817 Newton Street, Bannock, Wyoming, 1468"
2020-04-03 21:00:41 +08:00
}
]);
2020-06-01 20:54:12 +08:00
let (response, _status_code) = server.search_get(query).await;
2020-04-03 21:00:41 +08:00
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
}
2020-04-16 17:09:47 +08:00
#[actix_rt::test]
async fn search_with_settings_ranking_rules() {
let mut server = common::Server::test_server().await;
2020-04-03 21:00:41 +08:00
let config = json!({
"rankingRules": [
"typo",
"words",
"proximity",
"attribute",
"wordsPosition",
"desc(age)",
2020-04-03 21:00:41 +08:00
"exactness",
"desc(balance)"
2020-04-03 21:00:41 +08:00
],
"distinctAttribute": null,
"searchableAttributes": [
"name",
"age",
"color",
"gender",
"email",
"address",
"about"
2020-04-03 21:00:41 +08:00
],
"displayedAttributes": [
"name",
"age",
"gender",
"color",
"email",
"phone",
"address",
"balance"
2020-04-03 21:00:41 +08:00
],
"stopWords": null,
"synonyms": null,
});
2020-04-16 17:09:47 +08:00
server.update_all_settings(config).await;
2020-04-03 21:00:41 +08:00
let query = "q=exarcitation&limit=3";
2020-04-03 21:00:41 +08:00
let expect = json!([
{
"balance": "$1,921.58",
"age": 31,
"color": "Green",
"name": "Harper Carson",
"gender": "male",
"email": "harpercarson@chorizon.com",
"phone": "+1 (912) 430-3243",
"address": "883 Dennett Place, Knowlton, New Mexico, 9219"
2020-04-03 21:00:41 +08:00
},
{
"balance": "$1,706.13",
"age": 27,
"color": "Green",
"name": "Cherry Orr",
"gender": "female",
"email": "cherryorr@chorizon.com",
"phone": "+1 (995) 479-3174",
"address": "442 Beverly Road, Ventress, New Mexico, 3361"
2020-04-03 21:00:41 +08:00
},
{
"balance": "$1,476.39",
"age": 28,
"color": "brown",
"name": "Maureen Dale",
"gender": "female",
"email": "maureendale@chorizon.com",
"phone": "+1 (984) 538-3684",
"address": "817 Newton Street, Bannock, Wyoming, 1468"
2020-04-03 21:00:41 +08:00
}
]);
2020-06-01 20:54:12 +08:00
let (response, _status_code) = server.search_get(query).await;
println!("{}", response["hits"].clone());
2020-04-03 21:00:41 +08:00
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
}
2020-04-16 17:09:47 +08:00
#[actix_rt::test]
async fn search_with_settings_searchable_attributes() {
let mut server = common::Server::test_server().await;
2020-04-03 21:00:41 +08:00
let config = json!({
"rankingRules": [
"typo",
"words",
"proximity",
"attribute",
"wordsPosition",
"desc(age)",
2020-04-03 21:00:41 +08:00
"exactness",
"desc(balance)"
2020-04-03 21:00:41 +08:00
],
"distinctAttribute": null,
"searchableAttributes": [
"age",
"color",
"gender",
"address",
"about"
2020-04-03 21:00:41 +08:00
],
"displayedAttributes": [
"name",
"age",
"gender",
"color",
"email",
"phone",
"address",
"balance"
2020-04-03 21:00:41 +08:00
],
"stopWords": null,
"synonyms": {
"exarcitation": [
"exercitation"
]
},
2020-04-03 21:00:41 +08:00
});
2020-04-16 17:09:47 +08:00
server.update_all_settings(config).await;
2020-04-03 21:00:41 +08:00
let query = "q=Carol&limit=3";
2020-04-03 21:00:41 +08:00
let expect = json!([
{
"balance": "$1,440.09",
"age": 40,
"color": "blue",
"name": "Levy Whitley",
"gender": "male",
"email": "levywhitley@chorizon.com",
"phone": "+1 (911) 458-2411",
"address": "187 Thomas Street, Hachita, North Carolina, 2989"
2020-04-03 21:00:41 +08:00
},
{
"balance": "$1,977.66",
"age": 36,
"color": "brown",
"name": "Combs Stanley",
"gender": "male",
"email": "combsstanley@chorizon.com",
"phone": "+1 (827) 419-2053",
"address": "153 Beverley Road, Siglerville, South Carolina, 3666"
2020-04-03 21:00:41 +08:00
}
]);
2020-06-01 20:54:12 +08:00
let (response, _status_code) = server.search_get(query).await;
2020-04-03 21:00:41 +08:00
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
}
2020-04-16 17:09:47 +08:00
#[actix_rt::test]
async fn search_with_settings_displayed_attributes() {
let mut server = common::Server::test_server().await;
2020-04-03 21:00:41 +08:00
let config = json!({
"rankingRules": [
"typo",
"words",
"proximity",
"attribute",
"wordsPosition",
"desc(age)",
2020-04-03 21:00:41 +08:00
"exactness",
"desc(balance)"
2020-04-03 21:00:41 +08:00
],
"distinctAttribute": null,
"searchableAttributes": [
"age",
"color",
"gender",
"address",
"about"
2020-04-03 21:00:41 +08:00
],
"displayedAttributes": [
"name",
"age",
"gender",
"color",
"email",
"phone"
2020-04-03 21:00:41 +08:00
],
"stopWords": null,
"synonyms": null,
});
2020-04-16 17:09:47 +08:00
server.update_all_settings(config).await;
2020-04-03 21:00:41 +08:00
let query = "q=exercitation&limit=3";
2020-04-03 21:00:41 +08:00
let expect = json!([
{
"age": 31,
"color": "Green",
"name": "Harper Carson",
"gender": "male",
"email": "harpercarson@chorizon.com",
"phone": "+1 (912) 430-3243"
2020-04-03 21:00:41 +08:00
},
{
"age": 27,
"color": "Green",
"name": "Cherry Orr",
"gender": "female",
"email": "cherryorr@chorizon.com",
"phone": "+1 (995) 479-3174"
2020-04-03 21:00:41 +08:00
},
{
"age": 28,
"color": "brown",
"name": "Maureen Dale",
"gender": "female",
"email": "maureendale@chorizon.com",
"phone": "+1 (984) 538-3684"
2020-04-03 21:00:41 +08:00
}
]);
2020-06-01 20:54:12 +08:00
let (response, _status_code) = server.search_get(query).await;
2020-04-03 21:00:41 +08:00
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
}
2020-04-16 17:09:47 +08:00
#[actix_rt::test]
async fn search_with_settings_searchable_attributes_2() {
let mut server = common::Server::test_server().await;
2020-04-03 21:00:41 +08:00
let config = json!({
"rankingRules": [
"typo",
"words",
"proximity",
"attribute",
"wordsPosition",
"desc(age)",
2020-04-03 21:00:41 +08:00
"exactness",
"desc(balance)"
2020-04-03 21:00:41 +08:00
],
"distinctAttribute": null,
"searchableAttributes": [
"age",
"color",
"gender",
"address",
"about"
2020-04-03 21:00:41 +08:00
],
"displayedAttributes": [
"name",
"age",
"gender"
2020-04-03 21:00:41 +08:00
],
"stopWords": null,
"synonyms": null,
});
2020-04-16 17:09:47 +08:00
server.update_all_settings(config).await;
2020-04-03 21:00:41 +08:00
let query = "q=exercitation&limit=3";
2020-04-03 21:00:41 +08:00
let expect = json!([
{
"age": 31,
"name": "Harper Carson",
"gender": "male"
2020-04-03 21:00:41 +08:00
},
{
"age": 27,
"name": "Cherry Orr",
"gender": "female"
2020-04-03 21:00:41 +08:00
},
{
"age": 28,
"name": "Maureen Dale",
"gender": "female"
2020-04-03 21:00:41 +08:00
}
]);
2020-06-01 20:54:12 +08:00
let (response, _status_code) = server.search_get(query).await;
2020-04-03 21:00:41 +08:00
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
}
2020-06-27 16:38:08 +08:00
// issue #798
#[actix_rt::test]
async fn distinct_attributes_returns_name_not_id() {
let mut server = common::Server::test_server().await;
let settings = json!({
"distinctAttribute": "color",
});
server.update_all_settings(settings).await;
let (response, _) = server.get_all_settings().await;
assert_eq!(response["distinctAttribute"], "color");
let (response, _) = server.get_distinct_attribute().await;
assert_eq!(response, "color");
}