Fix PR comments

This commit is contained in:
ManyTheFish 2024-07-25 10:16:23 +02:00 committed by Louis Dureuil
parent 70d71581ee
commit a918561ac1
No known key found for this signature in database
7 changed files with 689 additions and 81 deletions

View File

@ -40,19 +40,7 @@ macro_rules! make_locale {
impl std::fmt::Display for LocaleFormatError { impl std::fmt::Display for LocaleFormatError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let valid_locales = [$(Locale::$language),+].iter().map(|l| format!("`{}`", json!(l).as_str().unwrap())).collect::<Vec<_>>().join(", "); let valid_locales = [$(Locale::$language),+].iter().map(|l| format!("`{}`", json!(l).as_str().unwrap())).collect::<Vec<_>>().join(", ");
write!(f, "Unknown value `{}`, expected one of {}", self.invalid_locale, valid_locales) write!(f, "Unsupported locale `{}`, expected one of {}", self.invalid_locale, valid_locales)
}
}
impl std::error::Error for LocaleFormatError {}
impl std::str::FromStr for Locale {
type Err = LocaleFormatError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
milli::tokenizer::Language::from_code(s).map(Self::from).ok_or(LocaleFormatError {
invalid_locale: s.to_string(),
})
} }
} }
}; };
@ -130,6 +118,18 @@ make_locale! {
Hye Hye
} }
impl std::error::Error for LocaleFormatError {}
impl std::str::FromStr for Locale {
type Err = LocaleFormatError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
milli::tokenizer::Language::from_code(s)
.map(Self::from)
.ok_or(LocaleFormatError { invalid_locale: s.to_string() })
}
}
#[derive(Debug, Clone, PartialEq, Eq, Deserr, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Deserr, Serialize, Deserialize)]
#[deserr(rename_all = camelCase)] #[deserr(rename_all = camelCase)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]

View File

@ -78,7 +78,8 @@ async fn import_dump_v1_movie_raw() {
"pagination": { "pagination": {
"maxTotalHits": 1000 "maxTotalHits": 1000
}, },
"searchCutoffMs": null "searchCutoffMs": null,
"localizedAttributes": null
} }
"### "###
); );
@ -240,7 +241,8 @@ async fn import_dump_v1_movie_with_settings() {
"pagination": { "pagination": {
"maxTotalHits": 1000 "maxTotalHits": 1000
}, },
"searchCutoffMs": null "searchCutoffMs": null,
"localizedAttributes": null
} }
"### "###
); );
@ -388,7 +390,8 @@ async fn import_dump_v1_rubygems_with_settings() {
"pagination": { "pagination": {
"maxTotalHits": 1000 "maxTotalHits": 1000
}, },
"searchCutoffMs": null "searchCutoffMs": null,
"localizedAttributes": null
} }
"### "###
); );
@ -522,7 +525,8 @@ async fn import_dump_v2_movie_raw() {
"pagination": { "pagination": {
"maxTotalHits": 1000 "maxTotalHits": 1000
}, },
"searchCutoffMs": null "searchCutoffMs": null,
"localizedAttributes": null
} }
"### "###
); );
@ -668,7 +672,8 @@ async fn import_dump_v2_movie_with_settings() {
"pagination": { "pagination": {
"maxTotalHits": 1000 "maxTotalHits": 1000
}, },
"searchCutoffMs": null "searchCutoffMs": null,
"localizedAttributes": null
} }
"### "###
); );
@ -813,7 +818,8 @@ async fn import_dump_v2_rubygems_with_settings() {
"pagination": { "pagination": {
"maxTotalHits": 1000 "maxTotalHits": 1000
}, },
"searchCutoffMs": null "searchCutoffMs": null,
"localizedAttributes": null
} }
"### "###
); );
@ -947,7 +953,8 @@ async fn import_dump_v3_movie_raw() {
"pagination": { "pagination": {
"maxTotalHits": 1000 "maxTotalHits": 1000
}, },
"searchCutoffMs": null "searchCutoffMs": null,
"localizedAttributes": null
} }
"### "###
); );
@ -1093,7 +1100,8 @@ async fn import_dump_v3_movie_with_settings() {
"pagination": { "pagination": {
"maxTotalHits": 1000 "maxTotalHits": 1000
}, },
"searchCutoffMs": null "searchCutoffMs": null,
"localizedAttributes": null
} }
"### "###
); );
@ -1238,7 +1246,8 @@ async fn import_dump_v3_rubygems_with_settings() {
"pagination": { "pagination": {
"maxTotalHits": 1000 "maxTotalHits": 1000
}, },
"searchCutoffMs": null "searchCutoffMs": null,
"localizedAttributes": null
} }
"### "###
); );
@ -1372,7 +1381,8 @@ async fn import_dump_v4_movie_raw() {
"pagination": { "pagination": {
"maxTotalHits": 1000 "maxTotalHits": 1000
}, },
"searchCutoffMs": null "searchCutoffMs": null,
"localizedAttributes": null
} }
"### "###
); );
@ -1518,7 +1528,8 @@ async fn import_dump_v4_movie_with_settings() {
"pagination": { "pagination": {
"maxTotalHits": 1000 "maxTotalHits": 1000
}, },
"searchCutoffMs": null "searchCutoffMs": null,
"localizedAttributes": null
} }
"### "###
); );
@ -1663,7 +1674,8 @@ async fn import_dump_v4_rubygems_with_settings() {
"pagination": { "pagination": {
"maxTotalHits": 1000 "maxTotalHits": 1000
}, },
"searchCutoffMs": null "searchCutoffMs": null,
"localizedAttributes": null
} }
"### "###
); );
@ -1909,7 +1921,8 @@ async fn import_dump_v6_containing_experimental_features() {
"pagination": { "pagination": {
"maxTotalHits": 1000 "maxTotalHits": 1000
}, },
"searchCutoffMs": null "searchCutoffMs": null,
"localizedAttributes": null
} }
"###); "###);
@ -2087,7 +2100,8 @@ async fn generate_and_import_dump_containing_vectors() {
"documentTemplate": "{{doc.doggo}}" "documentTemplate": "{{doc.doggo}}"
} }
}, },
"searchCutoffMs": null "searchCutoffMs": null,
"localizedAttributes": null
} }
"###); "###);

View File

@ -103,12 +103,41 @@ async fn simple_search() {
// english // english
index index
.search(json!({"q": "Atta", "attributesToRetrieve": ["id"]}), |response, code| { .search(json!({"q": "Atta", "attributesToHighlight": ["*"]}), |response, code| {
snapshot!(response, @r###" snapshot!(response, @r###"
{ {
"hits": [ "hits": [
{ {
"id": 852 "name_en": "Attack on Titan",
"name_ja": "進撃の巨人",
"author_en": "Hajime Isayama",
"author_ja": "諫山 創",
"description_en": "Attack on Titan is a Japanese manga series written and illustrated by Hajime Isayama",
"description_ja": "進撃の巨人は、日本の漫画シリーズであり、諫山 創によって作画されている。",
"id": 852,
"_vectors": {
"manual": [
1.0,
2.0,
3.0
]
},
"_formatted": {
"name_en": "<em>Atta</em>ck on Titan",
"name_ja": "進撃の巨人",
"author_en": "Hajime Isayama",
"author_ja": "諫山 創",
"description_en": "<em>Atta</em>ck on Titan is a Japanese manga series written and illustrated by Hajime Isayama",
"description_ja": "進撃の巨人は、日本の漫画シリーズであり、諫山 創によって作画されている。",
"id": "852",
"_vectors": {
"manual": [
"1.0",
"2.0",
"3.0"
]
}
}
} }
], ],
"query": "Atta", "query": "Atta",
@ -124,12 +153,35 @@ async fn simple_search() {
// japanese // japanese
index index
.search(json!({"q": "進撃", "attributesToRetrieve": ["id"]}), |response, code| { .search(json!({"q": "進撃", "attributesToHighlight": ["*"]}), |response, code| {
snapshot!(response, @r###" snapshot!(response, @r###"
{ {
"hits": [ "hits": [
{ {
"id": 853 "name_zh": "进击的巨人",
"author_zh": "諫山創",
"description_zh": "进击的巨人是日本的漫画系列,由諫山 創作画。",
"id": 853,
"_vectors": {
"manual": [
1.0,
2.0,
3.0
]
},
"_formatted": {
"name_zh": "<em>进击</em>的巨人",
"author_zh": "諫山創",
"description_zh": "<em>进击</em>的巨人是日本的漫画系列,由諫山 創作画。",
"id": "853",
"_vectors": {
"manual": [
"1.0",
"2.0",
"3.0"
]
}
}
} }
], ],
"query": "進撃", "query": "進撃",
@ -145,25 +197,77 @@ async fn simple_search() {
index index
.search( .search(
json!({"q": "進撃", "attributesToRetrieve": ["id"], "locales": ["jpn"]}), json!({"q": "進撃", "locales": ["jpn"], "attributesToHighlight": ["*"]}),
|response, code| { |response, code| {
snapshot!(response, @r###" snapshot!(response, @r###"
{
"hits": [
{ {
"id": 852 "hits": [
}, {
{ "name_en": "Attack on Titan",
"id": 853 "name_ja": "進撃の巨人",
"author_en": "Hajime Isayama",
"author_ja": "諫山 創",
"description_en": "Attack on Titan is a Japanese manga series written and illustrated by Hajime Isayama",
"description_ja": "進撃の巨人は、日本の漫画シリーズであり、諫山 創によって作画されている。",
"id": 852,
"_vectors": {
"manual": [
1.0,
2.0,
3.0
]
},
"_formatted": {
"name_en": "Attack on Titan",
"name_ja": "<em>進撃</em>の巨人",
"author_en": "Hajime Isayama",
"author_ja": "諫山 創",
"description_en": "Attack on Titan is a Japanese manga series written and illustrated by Hajime Isayama",
"description_ja": "<em>進撃</em>の巨人は、日本の漫画シリーズであり、諫山 創によって作画されている。",
"id": "852",
"_vectors": {
"manual": [
"1.0",
"2.0",
"3.0"
]
}
}
},
{
"name_zh": "进击的巨人",
"author_zh": "諫山創",
"description_zh": "进击的巨人是日本的漫画系列,由諫山 創作画。",
"id": 853,
"_vectors": {
"manual": [
1.0,
2.0,
3.0
]
},
"_formatted": {
"name_zh": "进击的巨人",
"author_zh": "諫山創",
"description_zh": "进击的巨人是日本的漫画系列,由諫山 創作画。",
"id": "853",
"_vectors": {
"manual": [
"1.0",
"2.0",
"3.0"
]
}
}
}
],
"query": "進撃",
"processingTimeMs": "[duration]",
"limit": 20,
"offset": 0,
"estimatedTotalHits": 2
} }
], "###);
"query": "進撃",
"processingTimeMs": "[duration]",
"limit": 20,
"offset": 0,
"estimatedTotalHits": 2
}
"###);
snapshot!(code, @"200 OK"); snapshot!(code, @"200 OK");
}, },
) )
@ -171,15 +275,67 @@ async fn simple_search() {
// chinese // chinese
index index
.search(json!({"q": "进击", "attributesToRetrieve": ["id"]}), |response, code| { .search(json!({"q": "进击", "attributesToHighlight": ["*"]}), |response, code| {
snapshot!(response, @r###" snapshot!(response, @r###"
{ {
"hits": [ "hits": [
{ {
"id": 853 "name_zh": "进击的巨人",
"author_zh": "諫山創",
"description_zh": "进击的巨人是日本的漫画系列,由諫山 創作画。",
"id": 853,
"_vectors": {
"manual": [
1.0,
2.0,
3.0
]
},
"_formatted": {
"name_zh": "<em>进击</em>的巨人",
"author_zh": "諫山創",
"description_zh": "<em>进击</em>的巨人是日本的漫画系列,由諫山 創作画。",
"id": "853",
"_vectors": {
"manual": [
"1.0",
"2.0",
"3.0"
]
}
}
}, },
{ {
"id": 852 "name_en": "Attack on Titan",
"name_ja": "進撃の巨人",
"author_en": "Hajime Isayama",
"author_ja": "諫山 創",
"description_en": "Attack on Titan is a Japanese manga series written and illustrated by Hajime Isayama",
"description_ja": "進撃の巨人は、日本の漫画シリーズであり、諫山 創によって作画されている。",
"id": 852,
"_vectors": {
"manual": [
1.0,
2.0,
3.0
]
},
"_formatted": {
"name_en": "Attack on Titan",
"name_ja": "<em>進撃</em>の巨人",
"author_en": "Hajime Isayama",
"author_ja": "諫山 創",
"description_en": "Attack on Titan is a Japanese manga series written and illustrated by Hajime Isayama",
"description_ja": "<em>進撃</em>の巨人は、日本の漫画シリーズであり、諫山 創によって作画されている。",
"id": "852",
"_vectors": {
"manual": [
"1.0",
"2.0",
"3.0"
]
}
}
} }
], ],
"query": "进击", "query": "进击",
@ -226,7 +382,7 @@ async fn force_locales() {
// chinese detection // chinese detection
index index
.search( .search(
json!({"q": "\"进击的巨人\"", "attributesToRetrieve": ["id"]}), json!({"q": "\"进击的巨人\"", "attributesToHighlight": ["*"]}),
|response, code| { |response, code| {
snapshot!(response, @r###" snapshot!(response, @r###"
{ {
@ -246,13 +402,36 @@ async fn force_locales() {
// force japanese // force japanese
index index
.search( .search(
json!({"q": "\"进击的巨人\"", "attributesToRetrieve": ["id"], "locales": ["jpn"]}), json!({"q": "\"进击的巨人\"", "locales": ["jpn"], "attributesToHighlight": ["*"]}),
|response, code| { |response, code| {
snapshot!(response, @r###" snapshot!(response, @r###"
{ {
"hits": [ "hits": [
{ {
"id": 853 "name_zh": "进击的巨人",
"author_zh": "諫山創",
"description_zh": "进击的巨人是日本的漫画系列,由諫山 創作画。",
"id": 853,
"_vectors": {
"manual": [
1.0,
2.0,
3.0
]
},
"_formatted": {
"name_zh": "<em>进</em><em>击</em><em>的</em><em>巨人</em>",
"author_zh": "諫山創",
"description_zh": "<em>进</em><em>击</em><em>的</em><em>巨人</em>是日本的漫画系列,由諫山 創作画。",
"id": "853",
"_vectors": {
"manual": [
"1.0",
"2.0",
"3.0"
]
}
}
} }
], ],
"query": "\"进击的巨人\"", "query": "\"进击的巨人\"",
@ -300,7 +479,7 @@ async fn force_locales_with_pattern() {
// chinese detection // chinese detection
index index
.search( .search(
json!({"q": "\"进击的巨人\"", "attributesToRetrieve": ["id"]}), json!({"q": "\"进击的巨人\"", "attributesToHighlight": ["*"]}),
|response, code| { |response, code| {
snapshot!(response, @r###" snapshot!(response, @r###"
{ {
@ -320,13 +499,36 @@ async fn force_locales_with_pattern() {
// force japanese // force japanese
index index
.search( .search(
json!({"q": "\"进击的巨人\"", "attributesToRetrieve": ["id"], "locales": ["jpn"]}), json!({"q": "\"进击的巨人\"", "locales": ["jpn"], "attributesToHighlight": ["*"]}),
|response, code| { |response, code| {
snapshot!(response, @r###" snapshot!(response, @r###"
{ {
"hits": [ "hits": [
{ {
"id": 853 "name_zh": "进击的巨人",
"author_zh": "諫山創",
"description_zh": "进击的巨人是日本的漫画系列,由諫山 創作画。",
"id": 853,
"_vectors": {
"manual": [
1.0,
2.0,
3.0
]
},
"_formatted": {
"name_zh": "<em>进</em><em>击</em><em>的</em><em>巨人</em>",
"author_zh": "諫山創",
"description_zh": "<em>进</em><em>击</em><em>的</em><em>巨人</em>是日本的漫画系列,由諫山 創作画。",
"id": "853",
"_vectors": {
"manual": [
"1.0",
"2.0",
"3.0"
]
}
}
} }
], ],
"query": "\"进击的巨人\"", "query": "\"进击的巨人\"",
@ -372,7 +574,7 @@ async fn force_locales_with_pattern_nested() {
// chinese // chinese
index index
.search( .search(
json!({"q": "\"进击的巨人\"", "attributesToRetrieve": ["id"], "locales": ["cmn"]}), json!({"q": "\"进击的巨人\"", "locales": ["cmn"], "attributesToHighlight": ["*"]}),
|response, code| { |response, code| {
snapshot!(response, @r###" snapshot!(response, @r###"
{ {
@ -392,13 +594,60 @@ async fn force_locales_with_pattern_nested() {
// force japanese // force japanese
index index
.search( .search(
json!({"q": "\"进击的巨人\"", "attributesToRetrieve": ["id"], "locales": ["jpn"]}), json!({"q": "\"进击的巨人\"", "locales": ["jpn"], "attributesToHighlight": ["*"]}),
|response, code| { |response, code| {
snapshot!(response, @r###" snapshot!(response, @r###"
{ {
"hits": [ "hits": [
{ {
"id": 852 "document_en": {
"name": "Attack on Titan",
"description": "Attack on Titan is a Japanese manga series written and illustrated by Hajime Isayama",
"author": "Hajime Isayama"
},
"document_ja": {
"name": "進撃の巨人",
"description": "進撃の巨人は、日本の漫画シリーズであり、諫山 創によって作画されている。",
"author": "諫山 創"
},
"document_zh": {
"name": "进击的巨人",
"description": "进击的巨人是日本的漫画系列,由諫山 創作画。",
"author": "諫山創"
},
"id": 852,
"_vectors": {
"manual": [
1.0,
2.0,
3.0
]
},
"_formatted": {
"document_en": {
"name": "Attack on Titan",
"description": "Attack on Titan is a Japanese manga series written and illustrated by Hajime Isayama",
"author": "Hajime Isayama"
},
"document_ja": {
"name": "進撃の巨人",
"description": "進撃の巨人は、日本の漫画シリーズであり、諫山 創によって作画されている。",
"author": "諫山 創"
},
"document_zh": {
"name": "<em>进</em><em>击</em><em>的</em><em>巨人</em>",
"description": "<em>进</em><em>击</em><em>的</em><em>巨人</em>是日本的漫画系列,由諫山 創作画。",
"author": "諫山創"
},
"id": "852",
"_vectors": {
"manual": [
"1.0",
"2.0",
"3.0"
]
}
}
} }
], ],
"query": "\"进击的巨人\"", "query": "\"进击的巨人\"",
@ -413,6 +662,357 @@ async fn force_locales_with_pattern_nested() {
) )
.await; .await;
} }
#[actix_rt::test]
async fn force_different_locales_with_pattern() {
let server = Server::new().await;
let index = server.index("test");
let documents = DOCUMENTS.clone();
let (response, _) = index
.update_settings(
json!({
"searchableAttributes": ["name_en", "name_ja", "name_zh", "author_en", "author_ja", "author_zh", "description_en", "description_ja", "description_zh"],
"localizedAttributes": [
// force japanese
{"attributePatterns": ["*_zh"], "locales": ["jpn"]},
// force chinese
{"attributePatterns": ["*_ja"], "locales": ["cmn"]}
]
}),
)
.await;
snapshot!(response, @r###"
{
"taskUid": 0,
"indexUid": "test",
"status": "enqueued",
"type": "settingsUpdate",
"enqueuedAt": "[date]"
}
"###);
index.add_documents(documents, None).await;
index.wait_task(1).await;
// force chinese
index
.search(
json!({"q": "\"进击的巨人\"", "locales": ["cmn"], "attributesToHighlight": ["*"]}),
|response, code| {
snapshot!(response, @r###"
{
"hits": [],
"query": "\"进击的巨人\"",
"processingTimeMs": "[duration]",
"limit": 20,
"offset": 0,
"estimatedTotalHits": 0
}
"###);
snapshot!(code, @"200 OK");
},
)
.await;
// force japanese
index
.search(
json!({"q": "\"进击的巨人\"", "locales": ["jpn"], "attributesToHighlight": ["*"]}),
|response, code| {
snapshot!(response, @r###"
{
"hits": [
{
"name_zh": "进击的巨人",
"author_zh": "諫山創",
"description_zh": "进击的巨人是日本的漫画系列,由諫山 創作画。",
"id": 853,
"_vectors": {
"manual": [
1.0,
2.0,
3.0
]
},
"_formatted": {
"name_zh": "<em>进</em><em>击</em><em>的</em><em>巨人</em>",
"author_zh": "諫山創",
"description_zh": "<em>进</em><em>击</em><em>的</em><em>巨人</em>是日本的漫画系列,由諫山 創作画。",
"id": "853",
"_vectors": {
"manual": [
"1.0",
"2.0",
"3.0"
]
}
}
}
],
"query": "\"进击的巨人\"",
"processingTimeMs": "[duration]",
"limit": 20,
"offset": 0,
"estimatedTotalHits": 1
}
"###);
snapshot!(code, @"200 OK");
},
)
.await;
}
#[actix_rt::test]
async fn force_different_locales_with_pattern_nested() {
let server = Server::new().await;
let index = server.index("test");
let documents = NESTED_DOCUMENTS.clone();
let (response, _) = index
.update_settings(json!({
"searchableAttributes": ["document_en", "document_ja", "document_zh"],
"localizedAttributes": [
// force japanese
{"attributePatterns": ["*_zh.*"], "locales": ["jpn"]},
// force chinese
{"attributePatterns": ["document_ja.*", "document_zh.*"], "locales": ["cmn"]}
]
}))
.await;
snapshot!(response, @r###"
{
"taskUid": 0,
"indexUid": "test",
"status": "enqueued",
"type": "settingsUpdate",
"enqueuedAt": "[date]"
}
"###);
index.add_documents(documents, None).await;
index.wait_task(1).await;
// chinese
index
.search(
json!({"q": "\"进击的巨人\"", "locales": ["cmn"], "attributesToHighlight": ["*"]}),
|response, code| {
snapshot!(response, @r###"
{
"hits": [],
"query": "\"进击的巨人\"",
"processingTimeMs": "[duration]",
"limit": 20,
"offset": 0,
"estimatedTotalHits": 0
}
"###);
snapshot!(code, @"200 OK");
},
)
.await;
// force japanese
index
.search(
json!({"q": "\"进击的巨人\"", "locales": ["jpn"], "attributesToHighlight": ["*"]}),
|response, code| {
snapshot!(response, @r###"
{
"hits": [
{
"document_en": {
"name": "Attack on Titan",
"description": "Attack on Titan is a Japanese manga series written and illustrated by Hajime Isayama",
"author": "Hajime Isayama"
},
"document_ja": {
"name": "進撃の巨人",
"description": "進撃の巨人は、日本の漫画シリーズであり、諫山 創によって作画されている。",
"author": "諫山 創"
},
"document_zh": {
"name": "进击的巨人",
"description": "进击的巨人是日本的漫画系列,由諫山 創作画。",
"author": "諫山創"
},
"id": 852,
"_vectors": {
"manual": [
1.0,
2.0,
3.0
]
},
"_formatted": {
"document_en": {
"name": "Attack on Titan",
"description": "Attack on Titan is a Japanese manga series written and illustrated by Hajime Isayama",
"author": "Hajime Isayama"
},
"document_ja": {
"name": "進撃の巨人",
"description": "進撃の巨人は、日本の漫画シリーズであり、諫山 創によって作画されている。",
"author": "諫山 創"
},
"document_zh": {
"name": "<em>进</em><em>击</em><em>的</em><em>巨人</em>",
"description": "<em>进</em><em>击</em><em>的</em><em>巨人</em>是日本的漫画系列,由諫山 創作画。",
"author": "諫山創"
},
"id": "852",
"_vectors": {
"manual": [
"1.0",
"2.0",
"3.0"
]
}
}
}
],
"query": "\"进击的巨人\"",
"processingTimeMs": "[duration]",
"limit": 20,
"offset": 0,
"estimatedTotalHits": 1
}
"###);
snapshot!(code, @"200 OK");
},
)
.await;
}
#[actix_rt::test]
async fn settings_change() {
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;
let (response, _) = index
.update_settings(json!({
"searchableAttributes": ["document_en", "document_ja", "document_zh"],
"localizedAttributes": [
// force japanese
{"attributePatterns": ["document_ja.*", "*_zh.*"], "locales": ["jpn"]}
]
}))
.await;
snapshot!(response, @r###"
{
"taskUid": 1,
"indexUid": "test",
"status": "enqueued",
"type": "settingsUpdate",
"enqueuedAt": "[date]"
}
"###);
index.wait_task(1).await;
// chinese
index
.search(
json!({"q": "\"进击的巨人\"", "locales": ["cmn"], "attributesToHighlight": ["*"]}),
|response, code| {
snapshot!(response, @r###"
{
"hits": [],
"query": "\"进击的巨人\"",
"processingTimeMs": "[duration]",
"limit": 20,
"offset": 0,
"estimatedTotalHits": 0
}
"###);
snapshot!(code, @"200 OK");
},
)
.await;
// force japanese
index
.search(
json!({"q": "\"进击的巨人\"", "locales": ["jpn"], "attributesToHighlight": ["*"]}),
|response, code| {
snapshot!(response, @r###"
{
"hits": [],
"query": "\"进击的巨人\"",
"processingTimeMs": "[duration]",
"limit": 20,
"offset": 0,
"estimatedTotalHits": 0
}
"###);
snapshot!(code, @"200 OK");
},
)
.await;
// change settings
let (response, _) = index
.update_settings(json!({
"searchableAttributes": ["document_en", "document_ja", "document_zh"],
"localizedAttributes": [
// force japanese
{"attributePatterns": ["*_zh.*"], "locales": ["jpn"]},
// force chinese
{"attributePatterns": ["document_ja.*"], "locales": ["cmn"]}
]
}))
.await;
snapshot!(response, @r###"
{
"taskUid": 2,
"indexUid": "test",
"status": "enqueued",
"type": "settingsUpdate",
"enqueuedAt": "[date]"
}
"###);
index.wait_task(2).await;
// chinese
index
.search(
json!({"q": "\"进击的巨人\"", "locales": ["cmn"], "attributesToHighlight": ["*"]}),
|response, code| {
snapshot!(response, @r###"
{
"hits": [],
"query": "\"进击的巨人\"",
"processingTimeMs": "[duration]",
"limit": 20,
"offset": 0,
"estimatedTotalHits": 0
}
"###);
snapshot!(code, @"200 OK");
},
)
.await;
// force japanese
index
.search(
json!({"q": "\"进击的巨人\"", "locales": ["jpn"], "attributesToHighlight": ["*"]}),
|response, code| {
snapshot!(response, @r###"
{
"hits": [],
"query": "\"进击的巨人\"",
"processingTimeMs": "[duration]",
"limit": 20,
"offset": 0,
"estimatedTotalHits": 0
}
"###);
snapshot!(code, @"200 OK");
},
)
.await;
}
#[actix_rt::test] #[actix_rt::test]
async fn invalid_locales() { async fn invalid_locales() {
@ -428,9 +1028,7 @@ async fn invalid_locales() {
index.add_documents(documents, None).await; index.add_documents(documents, None).await;
index.wait_task(1).await; index.wait_task(1).await;
let (response, code) = index let (response, code) = index.search_post(json!({"q": "Atta", "locales": ["invalid"]})).await;
.search_post(json!({"q": "Atta", "attributesToRetrieve": ["id"], "locales": ["invalid"]}))
.await;
snapshot!(code, @"400 Bad Request"); snapshot!(code, @"400 Bad Request");
snapshot!(json_string!(response), @r###" snapshot!(json_string!(response), @r###"
{ {
@ -442,17 +1040,12 @@ async fn invalid_locales() {
"###); "###);
let (response, code) = index let (response, code) = index
.search_get( .search_get(&yaup::to_string(&json!({"q": "Atta", "locales": ["invalid"]})).unwrap())
&yaup::to_string(
&json!({"q": "Atta", "attributesToRetrieve": ["id"], "locales": ["invalid"]}),
)
.unwrap(),
)
.await; .await;
snapshot!(code, @"400 Bad Request"); snapshot!(code, @"400 Bad Request");
snapshot!(json_string!(response), @r###" snapshot!(json_string!(response), @r###"
{ {
"message": "Invalid value in parameter `locales`: Unknown value `invalid`, expected one of `epo`, `eng`, `rus`, `cmn`, `spa`, `por`, `ita`, `ben`, `fra`, `deu`, `ukr`, `kat`, `ara`, `hin`, `jpn`, `heb`, `yid`, `pol`, `amh`, `jav`, `kor`, `nob`, `dan`, `swe`, `fin`, `tur`, `nld`, `hun`, `ces`, `ell`, `bul`, `bel`, `mar`, `kan`, `ron`, `slv`, `hrv`, `srp`, `mkd`, `lit`, `lav`, `est`, `tam`, `vie`, `urd`, `tha`, `guj`, `uzb`, `pan`, `aze`, `ind`, `tel`, `pes`, `mal`, `ori`, `mya`, `nep`, `sin`, `khm`, `tuk`, `aka`, `zul`, `sna`, `afr`, `lat`, `slk`, `cat`, `tgl`, `hye`", "message": "Invalid value in parameter `locales`: Unsupported locale `invalid`, expected one of `epo`, `eng`, `rus`, `cmn`, `spa`, `por`, `ita`, `ben`, `fra`, `deu`, `ukr`, `kat`, `ara`, `hin`, `jpn`, `heb`, `yid`, `pol`, `amh`, `jav`, `kor`, `nob`, `dan`, `swe`, `fin`, `tur`, `nld`, `hun`, `ces`, `ell`, `bul`, `bel`, `mar`, `kan`, `ron`, `slv`, `hrv`, `srp`, `mkd`, `lit`, `lav`, `est`, `tam`, `vie`, `urd`, `tha`, `guj`, `uzb`, `pan`, `aze`, `ind`, `tel`, `pes`, `mal`, `ori`, `mya`, `nep`, `sin`, `khm`, `tuk`, `aka`, `zul`, `sna`, `afr`, `lat`, `slk`, `cat`, `tgl`, `hye`",
"code": "invalid_search_locales", "code": "invalid_search_locales",
"type": "invalid_request", "type": "invalid_request",
"link": "https://docs.meilisearch.com/errors#invalid_search_locales" "link": "https://docs.meilisearch.com/errors#invalid_search_locales"

View File

@ -1558,7 +1558,7 @@ impl Index {
rtxn: &RoTxn<'_>, rtxn: &RoTxn<'_>,
) -> heed::Result<Option<Vec<LocalizedAttributesRule>>> { ) -> heed::Result<Option<Vec<LocalizedAttributesRule>>> {
self.main self.main
.remap_types::<Str, SerdeBincode<Vec<LocalizedAttributesRule>>>() .remap_types::<Str, SerdeJson<Vec<LocalizedAttributesRule>>>()
.get(rtxn, main_key::LOCALIZED_ATTRIBUTES_RULES) .get(rtxn, main_key::LOCALIZED_ATTRIBUTES_RULES)
} }
@ -1567,7 +1567,7 @@ impl Index {
txn: &mut RwTxn<'_>, txn: &mut RwTxn<'_>,
val: Vec<LocalizedAttributesRule>, val: Vec<LocalizedAttributesRule>,
) -> heed::Result<()> { ) -> heed::Result<()> {
self.main.remap_types::<Str, SerdeBincode<Vec<LocalizedAttributesRule>>>().put( self.main.remap_types::<Str, SerdeJson<Vec<LocalizedAttributesRule>>>().put(
txn, txn,
main_key::LOCALIZED_ATTRIBUTES_RULES, main_key::LOCALIZED_ATTRIBUTES_RULES,
&val, &val,

View File

@ -71,6 +71,8 @@ impl LocalizedFieldIds {
for rule in rules { for rule in rules {
if rule.match_str(field_name) { if rule.match_str(field_name) {
locales.extend(rule.locales.iter()); locales.extend(rule.locales.iter());
// Take the first rule that matches
break;
} }
} }

View File

@ -346,5 +346,5 @@ fn normalize_facet_string(facet_string: &str, locales: Option<&[Language]>) -> S
..Default::default() ..Default::default()
}; };
token.normalize(&options).lemma.to_string() token.normalize(&options).lemma.into_owned()
} }

View File

@ -1128,22 +1128,21 @@ impl<'a, 't, 'i> Settings<'a, 't, 'i> {
Ok(changed) Ok(changed)
} }
fn update_localized_attributes_rules(&mut self) -> Result<bool> { fn update_localized_attributes_rules(&mut self) -> Result<()> {
let changed = match &self.localized_attributes_rules { match &self.localized_attributes_rules {
Setting::Set(new) => { Setting::Set(new) => {
let old = self.index.localized_attributes_rules(self.wtxn)?; let old = self.index.localized_attributes_rules(self.wtxn)?;
if old.as_ref() == Some(new) { if old.as_ref() != Some(new) {
false
} else {
self.index.put_localized_attributes_rules(self.wtxn, new.clone())?; self.index.put_localized_attributes_rules(self.wtxn, new.clone())?;
true
} }
} }
Setting::Reset => self.index.delete_localized_attributes_rules(self.wtxn)?, Setting::Reset => {
Setting::NotSet => false, self.index.delete_localized_attributes_rules(self.wtxn)?;
}; }
Setting::NotSet => (),
}
Ok(changed) Ok(())
} }
pub fn execute<FP, FA>(mut self, progress_callback: FP, should_abort: FA) -> Result<()> pub fn execute<FP, FA>(mut self, progress_callback: FP, should_abort: FA) -> Result<()>