mirror of
				https://github.com/meilisearch/meilisearch.git
				synced 2025-11-04 01:46:28 +00:00 
			
		
		
		
	replace index-new-field route to accept-new-fields; fix #503
This commit is contained in:
		
				
					committed by
					
						
						Quentin de Quelen
					
				
			
			
				
	
			
			
			
						parent
						
							d56968cb23
						
					
				
				
					commit
					f4ae0844ab
				
			
							
								
								
									
										2
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										2
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							@@ -37,7 +37,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "assert-json-diff"
 | 
			
		||||
version = "1.0.1"
 | 
			
		||||
source = "git+https://github.com/qdequele/assert-json-diff#29bdf99315e510abb2824ccb9353b3a7a330cbc4"
 | 
			
		||||
source = "git+https://github.com/qdequele/assert-json-diff#9012a0c8866d0f2db0ef9a6242e4a19d1e8c67e4"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "serde 1.0.102 (registry+https://github.com/rust-lang/crates.io-index)",
 | 
			
		||||
 "serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)",
 | 
			
		||||
 
 | 
			
		||||
@@ -96,7 +96,7 @@ pub fn load_routes(app: &mut tide::Server<Data>) {
 | 
			
		||||
        .post(|ctx| into_response(setting::update_displayed(ctx)))
 | 
			
		||||
        .delete(|ctx| into_response(setting::delete_displayed(ctx)));
 | 
			
		||||
 | 
			
		||||
    app.at("/indexes/:index/settings/index-new-field")
 | 
			
		||||
    app.at("/indexes/:index/settings/accept-new-fields")
 | 
			
		||||
        .get(|ctx| into_response(setting::get_accept_new_fields(ctx)))
 | 
			
		||||
        .post(|ctx| into_response(setting::update_accept_new_fields(ctx)));
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -58,7 +58,7 @@ impl Server {
 | 
			
		||||
            let response: Value = serde_json::from_slice(&buf).unwrap();
 | 
			
		||||
 | 
			
		||||
            if response["status"] == "processed" {
 | 
			
		||||
                eprintln!("{:?}", response);
 | 
			
		||||
                eprintln!("{:#?}", response);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
            block_on(sleep(Duration::from_secs(1)));
 | 
			
		||||
 
 | 
			
		||||
@@ -117,8 +117,8 @@ fn write_all_and_delete() {
 | 
			
		||||
          "vote_average",
 | 
			
		||||
          "popularity"
 | 
			
		||||
        ],
 | 
			
		||||
        "stopWords": null,
 | 
			
		||||
        "synonyms": null,
 | 
			
		||||
        "stopWords": [],
 | 
			
		||||
        "synonyms": {},
 | 
			
		||||
        "acceptNewFields": true,
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
@@ -242,7 +242,7 @@ fn write_all_and_update() {
 | 
			
		||||
            "rank",
 | 
			
		||||
            "poster",
 | 
			
		||||
        ],
 | 
			
		||||
        "stopWords": null,
 | 
			
		||||
        "stopWords": [],
 | 
			
		||||
        "synonyms": {
 | 
			
		||||
            "wolverine": ["xmen", "logan"],
 | 
			
		||||
            "logan": ["wolverine", "xmen"],
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										290
									
								
								meilisearch-http/tests/settings_accept_new_fields.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										290
									
								
								meilisearch-http/tests/settings_accept_new_fields.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,290 @@
 | 
			
		||||
use assert_json_diff::assert_json_eq;
 | 
			
		||||
use serde_json::json;
 | 
			
		||||
 | 
			
		||||
mod common;
 | 
			
		||||
 | 
			
		||||
#[test]
 | 
			
		||||
fn index_new_fields_default() {
 | 
			
		||||
    let mut server = common::Server::with_uid("movies");
 | 
			
		||||
    let body = json!({
 | 
			
		||||
        "uid": "movies",
 | 
			
		||||
        "identifier": "id",
 | 
			
		||||
    });
 | 
			
		||||
    server.create_index(body);
 | 
			
		||||
 | 
			
		||||
    // 1 - Add a document
 | 
			
		||||
 | 
			
		||||
    let body = json!([{
 | 
			
		||||
        "id": 1,
 | 
			
		||||
        "title": "I'm a legend",
 | 
			
		||||
    }]);
 | 
			
		||||
 | 
			
		||||
    server.add_or_replace_multiple_documents(body);
 | 
			
		||||
 | 
			
		||||
    // 2 - Get the complete document
 | 
			
		||||
 | 
			
		||||
    let expected = json!({
 | 
			
		||||
        "id": 1,
 | 
			
		||||
        "title": "I'm a legend",
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    let (response, status_code) = server.get_document(1);
 | 
			
		||||
    assert_eq!(status_code, 200);
 | 
			
		||||
    assert_json_eq!(response, expected);
 | 
			
		||||
 | 
			
		||||
    // 3 - Add a document with more fields
 | 
			
		||||
 | 
			
		||||
    let body = json!([{
 | 
			
		||||
        "id": 2,
 | 
			
		||||
        "title": "I'm not a legend",
 | 
			
		||||
        "description": "A bad copy of the original movie I'm a lengend"
 | 
			
		||||
    }]);
 | 
			
		||||
 | 
			
		||||
    server.add_or_replace_multiple_documents(body);
 | 
			
		||||
 | 
			
		||||
    // 4 - Get the complete document
 | 
			
		||||
 | 
			
		||||
    let expected = json!({
 | 
			
		||||
        "id": 2,
 | 
			
		||||
        "title": "I'm not a legend",
 | 
			
		||||
        "description": "A bad copy of the original movie I'm a lengend"
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    let (response, status_code) = server.get_document(2);
 | 
			
		||||
    assert_eq!(status_code, 200);
 | 
			
		||||
    assert_json_eq!(response, expected);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[test]
 | 
			
		||||
fn index_new_fields_true() {
 | 
			
		||||
    let mut server = common::Server::with_uid("movies");
 | 
			
		||||
    let body = json!({
 | 
			
		||||
        "uid": "movies",
 | 
			
		||||
        "identifier": "id",
 | 
			
		||||
    });
 | 
			
		||||
    server.create_index(body);
 | 
			
		||||
 | 
			
		||||
    // 1 - Set indexNewFields = true
 | 
			
		||||
 | 
			
		||||
    server.update_accept_new_fields(json!(true));
 | 
			
		||||
 | 
			
		||||
    // 2 - Add a document
 | 
			
		||||
 | 
			
		||||
    let body = json!([{
 | 
			
		||||
        "id": 1,
 | 
			
		||||
        "title": "I'm a legend",
 | 
			
		||||
    }]);
 | 
			
		||||
 | 
			
		||||
    server.add_or_replace_multiple_documents(body);
 | 
			
		||||
 | 
			
		||||
    // 3 - Get the complete document
 | 
			
		||||
 | 
			
		||||
    let expected = json!({
 | 
			
		||||
        "id": 1,
 | 
			
		||||
        "title": "I'm a legend",
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    let (response, status_code) = server.get_document(1);
 | 
			
		||||
    assert_eq!(status_code, 200);
 | 
			
		||||
    assert_json_eq!(response, expected);
 | 
			
		||||
 | 
			
		||||
    // 4 - Add a document with more fields
 | 
			
		||||
 | 
			
		||||
    let body = json!([{
 | 
			
		||||
        "id": 2,
 | 
			
		||||
        "title": "I'm not a legend",
 | 
			
		||||
        "description": "A bad copy of the original movie I'm a lengend"
 | 
			
		||||
    }]);
 | 
			
		||||
 | 
			
		||||
    server.add_or_replace_multiple_documents(body);
 | 
			
		||||
 | 
			
		||||
    // 5 - Get the complete document
 | 
			
		||||
 | 
			
		||||
    let expected = json!({
 | 
			
		||||
        "id": 2,
 | 
			
		||||
        "title": "I'm not a legend",
 | 
			
		||||
        "description": "A bad copy of the original movie I'm a lengend"
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    let (response, status_code) = server.get_document(2);
 | 
			
		||||
    assert_eq!(status_code, 200);
 | 
			
		||||
    assert_json_eq!(response, expected);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[test]
 | 
			
		||||
fn index_new_fields_false() {
 | 
			
		||||
    let mut server = common::Server::with_uid("movies");
 | 
			
		||||
    let body = json!({
 | 
			
		||||
        "uid": "movies",
 | 
			
		||||
        "identifier": "id",
 | 
			
		||||
    });
 | 
			
		||||
    server.create_index(body);
 | 
			
		||||
 | 
			
		||||
    // 1 - Set indexNewFields = false
 | 
			
		||||
 | 
			
		||||
    server.update_accept_new_fields(json!(false));
 | 
			
		||||
 | 
			
		||||
    // 2 - Add a document
 | 
			
		||||
 | 
			
		||||
    let body = json!([{
 | 
			
		||||
        "id": 1,
 | 
			
		||||
        "title": "I'm a legend",
 | 
			
		||||
    }]);
 | 
			
		||||
 | 
			
		||||
    server.add_or_replace_multiple_documents(body);
 | 
			
		||||
 | 
			
		||||
    // 3 - Get the complete document
 | 
			
		||||
 | 
			
		||||
    let expected = json!({
 | 
			
		||||
        "id": 1,
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    let (response, status_code) = server.get_document(1);
 | 
			
		||||
    assert_eq!(status_code, 200);
 | 
			
		||||
    assert_json_eq!(response, expected);
 | 
			
		||||
 | 
			
		||||
    // 4 - Add a document with more fields
 | 
			
		||||
 | 
			
		||||
    let body = json!([{
 | 
			
		||||
        "id": 2,
 | 
			
		||||
        "title": "I'm not a legend",
 | 
			
		||||
        "description": "A bad copy of the original movie I'm a lengend"
 | 
			
		||||
    }]);
 | 
			
		||||
 | 
			
		||||
    server.add_or_replace_multiple_documents(body);
 | 
			
		||||
 | 
			
		||||
    // 5 - Get the complete document
 | 
			
		||||
 | 
			
		||||
    let expected = json!({
 | 
			
		||||
        "id": 2,
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    let (response, status_code) = server.get_document(2);
 | 
			
		||||
    assert_eq!(status_code, 200);
 | 
			
		||||
    assert_json_eq!(response, expected);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[test]
 | 
			
		||||
fn index_new_fields_true_then_false() {
 | 
			
		||||
    let mut server = common::Server::with_uid("movies");
 | 
			
		||||
    let body = json!({
 | 
			
		||||
        "uid": "movies",
 | 
			
		||||
        "identifier": "id",
 | 
			
		||||
    });
 | 
			
		||||
    server.create_index(body);
 | 
			
		||||
 | 
			
		||||
    // 1 - Set indexNewFields = true
 | 
			
		||||
 | 
			
		||||
    server.update_accept_new_fields(json!(true));
 | 
			
		||||
 | 
			
		||||
    // 2 - Add a document
 | 
			
		||||
 | 
			
		||||
    let body = json!([{
 | 
			
		||||
        "id": 1,
 | 
			
		||||
        "title": "I'm a legend",
 | 
			
		||||
    }]);
 | 
			
		||||
 | 
			
		||||
    server.add_or_replace_multiple_documents(body);
 | 
			
		||||
 | 
			
		||||
    // 3 - Get the complete document
 | 
			
		||||
 | 
			
		||||
    let expected = json!({
 | 
			
		||||
        "id": 1,
 | 
			
		||||
        "title": "I'm a legend",
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    let (response, status_code) = server.get_document(1);
 | 
			
		||||
    assert_eq!(status_code, 200);
 | 
			
		||||
    assert_json_eq!(response, expected);
 | 
			
		||||
 | 
			
		||||
    // 4 - Set indexNewFields = false
 | 
			
		||||
 | 
			
		||||
    server.update_accept_new_fields(json!(false));
 | 
			
		||||
 | 
			
		||||
    // 5 - Add a document with more fields
 | 
			
		||||
 | 
			
		||||
    let body = json!([{
 | 
			
		||||
        "id": 2,
 | 
			
		||||
        "title": "I'm not a legend",
 | 
			
		||||
        "description": "A bad copy of the original movie I'm a lengend"
 | 
			
		||||
    }]);
 | 
			
		||||
 | 
			
		||||
    server.add_or_replace_multiple_documents(body);
 | 
			
		||||
 | 
			
		||||
    // 6 - Get the complete document
 | 
			
		||||
 | 
			
		||||
    let expected = json!({
 | 
			
		||||
        "id": 2,
 | 
			
		||||
        "title": "I'm not a legend",
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    let (response, status_code) = server.get_document(2);
 | 
			
		||||
    assert_eq!(status_code, 200);
 | 
			
		||||
    assert_json_eq!(response, expected);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[test]
 | 
			
		||||
fn index_new_fields_false_then_true() {
 | 
			
		||||
    let mut server = common::Server::with_uid("movies");
 | 
			
		||||
    let body = json!({
 | 
			
		||||
        "uid": "movies",
 | 
			
		||||
        "identifier": "id",
 | 
			
		||||
    });
 | 
			
		||||
    server.create_index(body);
 | 
			
		||||
 | 
			
		||||
    // 1 - Set indexNewFields = false
 | 
			
		||||
 | 
			
		||||
    server.update_accept_new_fields(json!(false));
 | 
			
		||||
 | 
			
		||||
    // 2 - Add a document
 | 
			
		||||
 | 
			
		||||
    let body = json!([{
 | 
			
		||||
        "id": 1,
 | 
			
		||||
        "title": "I'm a legend",
 | 
			
		||||
    }]);
 | 
			
		||||
 | 
			
		||||
    server.add_or_replace_multiple_documents(body);
 | 
			
		||||
 | 
			
		||||
    // 3 - Get the complete document
 | 
			
		||||
 | 
			
		||||
    let expected = json!({
 | 
			
		||||
        "id": 1,
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    let (response, status_code) = server.get_document(1);
 | 
			
		||||
    assert_eq!(status_code, 200);
 | 
			
		||||
    assert_json_eq!(response, expected);
 | 
			
		||||
 | 
			
		||||
    // 4 - Set indexNewFields = false
 | 
			
		||||
 | 
			
		||||
    server.update_accept_new_fields(json!(true));
 | 
			
		||||
 | 
			
		||||
    // 5 - Add a document with more fields
 | 
			
		||||
 | 
			
		||||
    let body = json!([{
 | 
			
		||||
        "id": 2,
 | 
			
		||||
        "title": "I'm not a legend",
 | 
			
		||||
        "description": "A bad copy of the original movie I'm a lengend"
 | 
			
		||||
    }]);
 | 
			
		||||
 | 
			
		||||
    server.add_or_replace_multiple_documents(body);
 | 
			
		||||
 | 
			
		||||
    // 6 - Get the complete document
 | 
			
		||||
 | 
			
		||||
    let expected = json!({
 | 
			
		||||
        "id": 1,
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    let (response, status_code) = server.get_document(1);
 | 
			
		||||
    assert_eq!(status_code, 200);
 | 
			
		||||
    assert_json_eq!(response, expected);
 | 
			
		||||
 | 
			
		||||
    let expected = json!({
 | 
			
		||||
        "id": 2,
 | 
			
		||||
        "description": "A bad copy of the original movie I'm a lengend"
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    let (response, status_code) = server.get_document(2);
 | 
			
		||||
    assert_eq!(status_code, 200);
 | 
			
		||||
    assert_json_eq!(response, expected);
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user