diff --git a/07-data-models-new-field/README.md b/07-data-models-new-field/README.md index ee28418..dd99da2 100644 --- a/07-data-models-new-field/README.md +++ b/07-data-models-new-field/README.md @@ -4,7 +4,7 @@ The goal of this tutorial is to learn how to update your data model. We will sho update your [`JSONSchema`](https://json-schema.org/) to store a new field in the DB and your ES mapping so you can search for it. Moreover, we will learn how [`Marshmallow`](https://marshmallow.readthedocs.io) schema can be used to validate your data. -### Table of Contents +## Table of Contents - [Step 1: Bootstrap exercise](#step-1-bootstrap-exercise) - [Step 2: Update the JSONSchema](#step-2-update-the-JSONSchema) @@ -77,6 +77,7 @@ So, in order to update the mapping we edit the `my_site/records/mappings/v7/reco ``` ## Step 4: Update the Marshmallow schema + Next thing is to update our marshmallow schema in order to allow our new field to be validated by our loader. To achieve that we edit the `my_site/records/marshmallow/json.py`: @@ -107,22 +108,31 @@ We have created and started a new DB and ES along with the updated schemas and m **Note**: Make sure you have up and running our development server by running: ```bash -./scripts/server +$ ./scripts/server ``` Run the below command to create our new record: ```bash $ curl -k --header "Content-Type: application/json" \ - --request POST \ - --data '{"title":"Some title", "contributors": [{"name": "Doe, John"}], "owner": "owner"}' \ - https://localhost:5000/api/records/?prettyprint=1 + --request POST \ + --data '{"title":"Some title", "contributors": [{"name": "Doe, John"}], "owner": "owner"}' \ + https://localhost:5000/api/records/?prettyprint=1 ``` After executing the command you should see in your console the following output: ```json -{"status": 400, "message": "Validation error.", "errors": [{"field": "owner", "message": "Not a valid integer."}]} +{ + "status": 400, + "message": "Validation error.", + "errors": [ + { + "field": "owner", + "message": "Not a valid integer." + } + ] +} ``` It seems that our request wasn't successful. By checking again the error message we can see that in our request @@ -136,17 +146,16 @@ So now let's fix the data we sent before and create our record! ```bash $ curl -k --header "Content-Type: application/json" \ - --request POST \ - --data '{"title":"Some title", "contributors": [{"name": "Doe, John"}], "owner": 1}' \ - https://localhost:5000/api/records/?prettyprint=1 + --request POST \ + --data '{"title":"Some title", "contributors": [{"name": "Doe, John"}], "owner": 1}' \ + https://localhost:5000/api/records/?prettyprint=1 ``` Now you should see an output similar to the below: ```json { - "_bucket": "60f2b083-8f7b-4aba-a00e-09e3bb3e12af", - "created": "2019-11-25T07:41:44.620275+00:00", + "created": "2020-05-12T14:41:44.801477+00:00", "id": "1", "links": { "files": "https://localhost:5000/api/records/1/files", @@ -164,9 +173,10 @@ Now you should see an output similar to the below: "title": "Some title" }, "revision": 0, - "updated": "2019-11-25T07:41:44.620282+00:00" + "updated": "2020-05-12T14:41:44.801484+00:00" } ``` + **Tip**: Save somewhere the `id` value of this response! Our new record was successfully created! @@ -178,7 +188,7 @@ Our new record was successfully created! **Note**: Make sure you have up and running our development server by running: ```bash -./scripts/server +$ ./scripts/server ``` Let's search now for our newly created record. Replace the `` with the actual `id` of the @@ -245,7 +255,7 @@ class RecordSchemaV1(StrictKeysMixin): Then now if search again we will take the following result: ```bash -curl -k "https://localhost:5000/api/records/?q=owner:" +$ curl -k "https://localhost:5000/api/records/?q=owner:" { "aggregations": {...}, "hits": { diff --git a/07-data-models-new-field/solution/my-site/my_site/records/mappings/v7/records/record-v1.0.0.json b/07-data-models-new-field/solution/my-site/my_site/records/mappings/v7/records/record-v1.0.0.json index 33f4040..1e8df8f 100644 --- a/07-data-models-new-field/solution/my-site/my_site/records/mappings/v7/records/record-v1.0.0.json +++ b/07-data-models-new-field/solution/my-site/my_site/records/mappings/v7/records/record-v1.0.0.json @@ -1,67 +1,65 @@ { "mappings": { - "record-v1.0.0": { - "date_detection": false, - "numeric_detection": false, - "properties": { - "$schema": { - "type": "text", - "index": false - }, - "title": { - "type": "text", - "copy_to": "suggest_title" - }, - "suggest_title": { - "type": "completion" - }, - "id": { - "type": "keyword" - }, - "owner": { - "type": "integer" - }, - "keywords": { - "type": "keyword" - }, - "publication_date": { - "type": "date", - "format": "date" - }, - "contributors": { - "type": "object", - "properties": { - "ids": { - "type": "object", - "properties": { - "source": { - "type": "text" - }, - "value": { - "type": "keyword" - } + "date_detection": false, + "numeric_detection": false, + "properties": { + "$schema": { + "type": "text", + "index": false + }, + "title": { + "type": "text", + "copy_to": "suggest_title" + }, + "suggest_title": { + "type": "completion" + }, + "id": { + "type": "keyword" + }, + "owner": { + "type": "integer" + }, + "keywords": { + "type": "keyword" + }, + "publication_date": { + "type": "date", + "format": "date" + }, + "contributors": { + "type": "object", + "properties": { + "ids": { + "type": "object", + "properties": { + "source": { + "type": "text" + }, + "value": { + "type": "keyword" } - }, - "affiliations": { - "type": "text" - }, - "role": { - "type": "keyword" - }, - "email": { - "type": "text" - }, - "name": { - "type": "text" } + }, + "affiliations": { + "type": "text" + }, + "role": { + "type": "keyword" + }, + "email": { + "type": "text" + }, + "name": { + "type": "text" } - }, - "_created": { - "type": "date" - }, - "_updated": { - "type": "date" } + }, + "_created": { + "type": "date" + }, + "_updated": { + "type": "date" } } }