Search code examples
elasticsearchelasticsearch-mappingdynamic-mapping

Using ElasticSearch mappings in a strict way


We are developing using ElasticSearch. We have created two indexes: notificacionespush_anadirdispositivo, which stores new user's devices, and notificacionespush_crearnotificacion, which stores information related to have created a new notification.

How could we configure ElasticSearch's server, to only accept information which have the same structure as the one defined by the mappings?

notificacionespush_anadirdispositivo's mappings:

{
    "notificacionespush_anadirdispositivo": {
        "mappings": {
            "properties": {
                "descripcionresultado": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "fechahora": {
                    "type": "date",
                    "format": "dd/MM/yyyy HH:mm:ss"
                },
                "idapp": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "iddispositivo": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "idioma": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "numexpediente": {
                    "type": "long"
                },
                "resultado": {
                    "type": "long"
                },
                "tiponotificacion": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "usuario": {
                    "type": "long"
                }
            }
        }
    }
}

push_crearnotificacion mappings:

{
    "notificacionespush_crearnotificacion": {
        "mappings": {
            "properties": {
                "descripcionresultado": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "fechahora": {
                    "type": "date",
                    "format": "dd/MM/yyyy HH:mm:ss"
                },
                "idapp": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "idioma": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "numexpediente": {
                    "type": "long"
                },
                "resultado": {
                    "type": "long"
                },
                "tiponotificacion": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        }
    }
}

We are interested and curious about this topic, because of we have discovered that if we send a POST to the index notificacionespush_anadirdispositivo with a JSON representing a piece of information which has notificacionespush_crearnotificacion's mapping, it is being accepted.

So then when we get the contents we would see:

{
    "took": 513,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "notificacionespush_anadirdispositivo",
                "_type": "_doc",
                "_id": "gT1DnHABgf__-U4-DNkw",
                "_score": 1.0,
                "_source": {
                    "usuario": 665365335,
                    "iddispositivo": "SuperID",
                    "idapp": "miHistoria",
                    "fechahora": "02/03/2020 17:20:04",
                    "resultado": -2,
                    "descripcionresultado": "Ya existe ese dispositivo asociado al expediente con el que se pretende registrar"
                }
            },
            {
                "_index": "notificacionespush_anadirdispositivo",
                "_type": "_doc",
                "_id": "RD1EnHABgf__-U4-U9re",
                "_score": 1.0,
                "_source": {
                    "usuario": 11473564,
                    "iddispositivo": "PRE4283599e-4718-4482-8dd5-733c29156cc2",
                    "idapp": "miCitaPrevia",
                    "fechahora": "27/02/2020 16:27:14"
                }
            },
            {
                "_index": "notificacionespush_anadirdispositivo",
                "_type": "_doc",
                "_id": "wj1MnHABgf__-U4-h9ui",
                "_score": 1.0,
                "_source": {
                    "usuario": 11473564,
                    "iddispositivo": "PRE4283599e-4718-4482-8dd5-733c29156cc2",
                    "idapp": "miCitaPrevia",
                    "fechahora": "27/02/2020 16:27:14"
                }
            },
            {
                "_index": "notificacionespush_anadirdispositivo",
                "_type": "_doc",
                "_id": "Uj1lnHABgf__-U4-COAX",
                "_score": 1.0,
                "_source": {
                    "idapp": "5cf57b56-c3b4-4a0d-8938-4ac4466f93af",
                    "numexpediente": 123456789,
                    "idioma": "es",
                    "tiponotificacion": "citafuturaAP",
                    "fechahora": "20/02/2020 10:52:57",
                    "resultado": 0,
                    "descripcionresultado": "{\"id\":\"\",\"recipients\":0,\"errors\":[\"All included players are not subscribed\"]}"
                }
            }
        ]
    }
}

Solution

  • Simply add "dynamic": "strict" to your mapping.

    (See Elasticsearch Reference: dynamic)