【问题标题】:Using ElasticSearch mappings in a strict way严格使用 ElasticSearch 映射
【发布时间】:2020-06-15 01:54:32
【问题描述】:

我们正在使用 ElasticSearch 进行开发。我们创建了两个索引:notificacionespush_anadirdispositivo,用于存储新用户的设备,以及 notificacionespush_crearnotificacion,用于存储与已创建新通知相关的信息。

我们如何配置 ElasticSearch 的服务器,使其只接受与映射定义的结构相同的信息?

notificacionespush_anadirdispositivo 的映射:

{
    "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 映射:

{
    "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
                        }
                    }
                }
            }
        }
    }
}

我们对这个话题很感兴趣和好奇,因为我们发现如果我们向索引 notificacionespush_anadirdispositivo 发送一个带有表示一条信息的 JSON 的 POST,该信息具有 notificacionespush_crearnotificacion 的映射,它就会被接受。

那么当我们得到内容时,我们会看到:

{
    "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\"]}"
                }
            }
        ]
    }
}

【问题讨论】:

    标签: elasticsearch elasticsearch-mapping dynamic-mapping


    【解决方案1】:

    只需将"dynamic": "strict" 添加到您的映射中。

    (请参阅 Elasticsearch 参考:dynamic

    【讨论】:

      猜你喜欢
      • 2016-02-21
      • 2015-12-04
      • 2012-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-25
      • 2019-09-27
      • 2019-05-18
      相关资源
      最近更新 更多