From 2d82f1b655559cb80767c0c6614834697c53de99 Mon Sep 17 00:00:00 2001 From: Quentin de Quelen Date: Mon, 16 Mar 2020 12:17:51 +0100 Subject: [PATCH] ranking fields should be stored and indexed by default; fix #521 --- .../src/update/settings_update.rs | 8 ++- meilisearch-http/tests/settings.rs | 1 - .../tests/settings_ranking_rules.rs | 51 +++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/meilisearch-core/src/update/settings_update.rs b/meilisearch-core/src/update/settings_update.rs index 2be2e734e..452d2f3f1 100644 --- a/meilisearch-core/src/update/settings_update.rs +++ b/meilisearch-core/src/update/settings_update.rs @@ -45,7 +45,13 @@ pub fn apply_settings_update( match settings.ranking_rules { UpdateState::Update(v) => { let ranked_field: Vec<&str> = v.iter().filter_map(RankingRule::field).collect(); - schema.update_ranked(ranked_field)?; + schema.update_ranked(&ranked_field)?; + for name in ranked_field { + if schema.accept_new_fields() { + schema.set_indexed(name.as_ref())?; + schema.set_displayed(name.as_ref())?; + } + } index.main.put_ranking_rules(writer, &v)?; must_reindex = true; }, diff --git a/meilisearch-http/tests/settings.rs b/meilisearch-http/tests/settings.rs index 7e2b1275d..e2b57ee31 100644 --- a/meilisearch-http/tests/settings.rs +++ b/meilisearch-http/tests/settings.rs @@ -427,5 +427,4 @@ fn write_setting_and_update_partial() { let (response, _status_code) = server.get_all_settings(); assert_json_eq!(expected, response, ordered: false); - } diff --git a/meilisearch-http/tests/settings_ranking_rules.rs b/meilisearch-http/tests/settings_ranking_rules.rs index a393374ee..f203216ff 100644 --- a/meilisearch-http/tests/settings_ranking_rules.rs +++ b/meilisearch-http/tests/settings_ranking_rules.rs @@ -135,3 +135,54 @@ fn send_malformed_custom_rule() { let (_response, status_code) = server.update_ranking_rules_sync(body); assert_eq!(status_code, 400); } + +// Test issue https://github.com/meilisearch/MeiliSearch/issues/521 +#[test] +fn write_custom_ranking_and_index_documents() { + let mut server = common::Server::with_uid("movies"); + let body = json!({ + "uid": "movies", + "primaryKey": "id", + }); + server.create_index(body); + + // 1 - Add ranking rules with one custom ranking on a string + + let body = json!([ + "asc(title)", + "typo" + ]); + + server.update_ranking_rules(body); + + // 2 - Add documents + + let body = json!([ + { + "id": 1, + "title": "Le Petit Prince", + "author": "Exupéry" + }, + { + "id": 2, + "title": "Pride and Prejudice", + "author": "Jane Austen" + } + ]); + + server.add_or_replace_multiple_documents(body); + + // 3 - Get the first document and compare + + let expected = json!({ + "id": 1, + "title": "Le Petit Prince", + "author": "Exupéry" + }); + + let (response, status_code) = server.get_document(1); + assert_eq!(status_code, 200); + + assert_json_eq!(response, expected, ordered: false); + +}