Exceptions
# Create mapping
curl --request PUT 'localhost:9200/microservice-logs' \
--header "Content-Type: application/json" \
--data-raw '
{
"mappings": {
"properties": {
"timestamp": { "type": "date" },
"service": { "type": "keyword" },
"host_ip": { "type": "ip" },
"port": { "type": "integer" },
"message": { "type": "text" }
}
}
}
'
- Valid document may look like
{
"timestamp": "2020-04-11T12:34:56.789Z",
"service": "ABC",
"host_ip": "10.0.2.15",
"port": 12345,
"message": "Started!"
}
Trying to insert invalid document
# String number instead of Integer (for port) - Passes successfully
curl --request POST 'localhost:9200/microservice-logs/_doc?pretty' \
--header "Content-Type: application/json" \
--data-raw '
{
"timestamp": "2020-04-11T12:34:56.789Z",
"service": "XYZ",
"host_ip": "10.0.2.15",
"port": "15000", // STRING port (instead of INTEGER)
"message": "Hello!"
}
'
# String not-number instead of Integer (for port) - Error (parsing excepting)
curl --request POST 'localhost:9200/microservice-logs/_doc?pretty' \
--header "Content-Type: application/json" \
--data-raw '
{
"timestamp": "2020-04-11T12:34:56.789Z",
"service": "XYZ",
"host_ip": "10.0.2.15",
"port": "none", // STRING port (instead of INTEGER)
"message": "I am not well!"
}
'
# Close index
curl 'localhost:9200/microservice-logs/_close' \
--request POST
# IGNORE malformed documents!
curl 'localhost:9200/microservice-logs/_settings' \
--request PUT \
--header "Content-Type: application/json" \
--data-raw '{
"index.mapping.ignore_malformed": true
}'
# Reopen index
curl 'localhost:9200/microservice-logs/_open' \
--request POST
Inserting invalid documents again
# String not-number instead of Integer - Successful
curl 'localhost:9200/microservice-logs/_doc?pretty' \
--request POST \
--header "Content-Type: application/json" \
--data-raw '
{
"timestamp": "2020-04-11T12:34:56.789Z",
"service": "XYZ",
"host_ip": "10.0.2.15",
"port": "none", // STRING port (instead of INTEGER)
"message": "I am not well!"
}
'
curl 'localhost:9200/microservice-logs/_search?pretty' --request GET # the document is there and the port rule was ignored
# A json object cannot be passed as a value though! It will fail
curl 'localhost:9200/microservice-logs/_doc?pretty' \
--request POST \
--header "Content-Type: application/json" \
--data-raw '{
"timestamp": "2020-04-11T12:34:56.789Z",
"service": "ABC",
"host_ip": "10.0.2.15",
"port": 12345,
"message": {"data": {"received":"here"}} // JSON object (instead of STRING)
}'
# A new field can be added as a JSON
curl 'localhost:9200/microservice-logs/_doc?pretty' \
--request POST \
--header "Content-Type: application/json" \
--data-raw '{
"timestamp": "2020-04-11T12:34:56.789Z",
"service": "ABC",
"host_ip": "10.0.2.15",
"port": 12345,
"message": "Received...",
"payload": {"data": {"received":"here"}} // new JSON field. A mapping will be autogenerated
}'