[elasticserch] 淺談 dynamic mapping

動態的新增欄位與 type ,在elasticsearch 是個非常直覺的行為。

上文有先說明過,關於在 elasticsearch put 欄位的資料時,這個欄位的mapping 會依據他第一次 index 資料時欄位的 type 來當預設值。

所以,預設的 dynamic mapping 是開啟的。但是可能會發生,我們非預期的情況。date 資訊就是常見的一種,有時我們欄位的值,並不是 date 欄位,但是 elasticsearch會幫我們判定成 date。

舉個例子來說, 在索引時note欄位第一次出現的值,被判定為 date。
{“note":"2014-06-25″}

再來的資料是,

{“note":"this is note"}

會發生 note的 type已經被定為 date ,但是,但是你要index 的 value 的情況。(會出現 malformed date)

此時,就可以關掉預設 “date_detection" ,讓 elasticsearch 不會自動幫我們把 string 裡面的值判定成 date type。

PUT /my_index{
    "mappings": {
        "my_type": {
            "date_detection": false
        }
    }
}