From c25641ff2d5cda4f8b015a1033e6e92275f7765b Mon Sep 17 00:00:00 2001 From: qdequele Date: Wed, 11 Mar 2020 12:00:40 +0100 Subject: [PATCH] fix that AcceptNewFields does not take into account the primary-key; fix #518 --- .../tests/settings_accept_new_fields.rs | 59 +++++++++++++++++++ meilisearch-schema/src/schema.rs | 6 +- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/meilisearch-http/tests/settings_accept_new_fields.rs b/meilisearch-http/tests/settings_accept_new_fields.rs index 5d11b3fe9..807aa1cd2 100644 --- a/meilisearch-http/tests/settings_accept_new_fields.rs +++ b/meilisearch-http/tests/settings_accept_new_fields.rs @@ -288,3 +288,62 @@ fn index_new_fields_false_then_true() { assert_eq!(status_code, 200); assert_json_eq!(response, expected); } + + +// Fix issue https://github.com/meilisearch/MeiliSearch/issues/518 +#[test] +fn accept_new_fields_does_not_take_into_account_the_primary_key () { + let mut server = common::Server::with_uid("movies"); + + // 1 - Create an index with no primary-key + + let body = json!({ + "uid": "movies", + }); + let (response, status_code) = server.create_index(body); + assert_eq!(status_code, 201); + assert_eq!(response["primaryKey"], json!(null)); + + // 2 - Add searchable and displayed attributes as: ["title"] & Set acceptNewFields to false + + let body = json!({ + "searchableAttributes": ["title"], + "displayedAttributes": ["title"], + "acceptNewFields": false, + }); + + server.update_all_settings(body); + + // 4 - Add a document + + let body = json!([{ + "id": 1, + "title": "Test", + "comment": "comment test" + }]); + + server.add_or_replace_multiple_documents(body); + + // 5 - Get settings, they should not changed + + let (response, _status_code) = server.get_all_settings(); + + let expected = json!({ + "rankingRules": [ + "typo", + "words", + "proximity", + "attribute", + "wordsPosition", + "exactness", + ], + "distinctAttribute": null, + "searchableAttributes": ["title"], + "displayedAttributes": ["title"], + "stopWords": [], + "synonyms": {}, + "acceptNewFields": false, + }); + + assert_json_eq!(response, expected, ordered: false); +} diff --git a/meilisearch-schema/src/schema.rs b/meilisearch-schema/src/schema.rs index b1b8bc66b..64f622501 100644 --- a/meilisearch-schema/src/schema.rs +++ b/meilisearch-schema/src/schema.rs @@ -63,8 +63,10 @@ impl Schema { let id = self.insert(name)?; self.primary_key = Some(id); - self.set_indexed(name)?; - self.set_displayed(name)?; + if self.accept_new_fields { + self.set_indexed(name)?; + self.set_displayed(name)?; + } Ok(id) }