ElasticSearch / Kibana字段更新文档

时间:2016-11-09 16:41:46

标签: elasticsearch kibana

我对ES很新。我使用https://github.com/dariusk/corpora/blob/master/data/humans/us_presidents.json作为学习集。

刚开始我在Kibana的Dev Tools选项卡中输入了这个:

POST /presidents/president/1
{ "bo" : 
        {
         "website":"",
         "startdate":"2009-01-20",
         "role_type_label":"President",
         "enddate":"2013-01-20",
         "description":"President",
         "district":null,
         "phone":null,
         "title":"President",
         "congress_numbers":[
            111,
            112,
            113
         ],
         "title_long":"President",
         "current":false,
         "person":{
            "name":"President Barack Obama [D]",
            "firstname":"Barack",
            "twitterid":null,
            "middlename":"",
            "gender":"male",
            "bioguideid":"O000167",
            "namemod":"",
            "birthday":"1961-08-04",
            "link":"https://www.govtrack.us/congress/members/barack_obama/400629",
            "youtubeid":null,
            "sortname":"Obama, Barack (President) [D]",
            "lastname":"Obama",
            "gender_label":"Male",
            "osid":"N00009638",
            "pvsid":"9490",
            "nickname":"",
            "id":400629,
            "cspanid":null
         }
         ...
        }
}

然后我意识到,如果我想添加更多关于个别总统的数据,我宁愿这样做:

POST /presidents/president/1
{
         "website":"",
         "startdate":"2009-01-20",
         "role_type_label":"President",
         "enddate":"2013-01-20",
         "description":"President",
         "district":null,
         "phone":null,
         "title":"President",
         "congress_numbers":[
            111,
            112,
            113
         ],
         "title_long":"President",
         "current":false,
         "person":{
            "name":"President Barack Obama [D]",
            "firstname":"Barack",
            "twitterid":null,
            "middlename":"",
            "gender":"male",
            "bioguideid":"O000167",
            "namemod":"",
            "birthday":"1961-08-04",
            "link":"https://www.govtrack.us/congress/members/barack_obama/400629",
            "youtubeid":null,
            "sortname":"Obama, Barack (President) [D]",
            "lastname":"Obama",
            "gender_label":"Male",
            "osid":"N00009638",
            "pvsid":"9490",
            "nickname":"",
            "id":400629,
            "cspanid":null
         }
}

好的,所以ES接受了更新。

但现在,当我在Kibana中进入管理/索引模式时,我会将person.lastnamebo.person.lastname视为字段。

为什么以前的字段仍然存在? ES是否正常保留不再在更新文档中的字段?

显然,除了非常有趣的,请不要打趣今天的选举结果。

1 个答案:

答案 0 :(得分:1)

这是Elasticsearch的正常预期行为。

默认情况下,ES会动态映射您要插入的数据。在索引中索引相同类型的多个对象时,所有这些对象共享相同的映射。目的是允许插入不必携带其类型的所有潜在字段的对象,并将其插入到索引中。

您可以自己定义映射,无论是在索引创建时,还是通过为索引定义新类型。映射也可以更新,但有一些警告。

要查看索引中类型的映射,请执行以下操作:

GET /tk_file.2016/TK_FILE/_mapping

您的回复将如下所示:

{
   "presidents": {
      "mappings": {
         "president": {
            "properties": {
               "bo": {
                  "properties": {
                     "congress_numbers": {
                        "type": "long"
                     },
                     "current": {
                        "type": "boolean"
                     },
                     "description": {
                        "type": "text",
                        "fields": {
                           "keyword": {
                              "type": "keyword",
                              "ignore_above": 256
                           }
                        }
                     },
                     "enddate": {
                        "type": "date"
                     },
                     "person": {
                        "properties": {
                           "bioguideid": {
                              "type": "text",
                              "fields": {
                                 "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                 }
                              }
                           },
                           "birthday": {
                              "type": "date"
                           },
                           "firstname": {
                              "type": "text",
                              "fields": {
                                 "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                 }
                              }
                           },
                           "gender": {
                              "type": "text",
                              "fields": {
                                 "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                 }
                              }
                           },
                           "gender_label": {
                              "type": "text",
                              "fields": {
                                 "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                 }
                              }
                           },
                           "id": {
                              "type": "long"
                           },
                           "lastname": {
                              "type": "text",
                              "fields": {
                                 "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                 }
                              }
                           },
                           "link": {
                              "type": "text",
                              "fields": {
                                 "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                 }
                              }
                           },
                           "middlename": {
                              "type": "text",
                              "fields": {
                                 "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                 }
                              }
                           },
                           "name": {
                              "type": "text",
                              "fields": {
                                 "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                 }
                              }
                           },
                           "namemod": {
                              "type": "text",
                              "fields": {
                                 "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                 }
                              }
                           },
                           "nickname": {
                              "type": "text",
                              "fields": {
                                 "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                 }
                              }
                           },
                           "osid": {
                              "type": "text",
                              "fields": {
                                 "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                 }
                              }
                           },
                           "pvsid": {
                              "type": "text",
                              "fields": {
                                 "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                 }
                              }
                           },
                           "sortname": {
                              "type": "text",
                              "fields": {
                                 "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                 }
                              }
                           }
                        }
                     },
                     "role_type_label": {
                        "type": "text",
                        "fields": {
                           "keyword": {
                              "type": "keyword",
                              "ignore_above": 256
                           }
                        }
                     },
                     "startdate": {
                        "type": "date"
                     },
                     "title": {
                        "type": "text",
                        "fields": {
                           "keyword": {
                              "type": "keyword",
                              "ignore_above": 256
                           }
                        }
                     },
                     "title_long": {
                        "type": "text",
                        "fields": {
                           "keyword": {
                              "type": "keyword",
                              "ignore_above": 256
                           }
                        }
                     },
                     "website": {
                        "type": "text",
                        "fields": {
                           "keyword": {
                              "type": "keyword",
                              "ignore_above": 256
                           }
                        }
                     }
                  }
               },
               "congress_numbers": {
                  "type": "long"
               },
               "current": {
                  "type": "boolean"
               },
               "description": {
                  "type": "text",
                  "fields": {
                     "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                     }
                  }
               },
               "enddate": {
                  "type": "date"
               },
               "person": {
                  "properties": {
                     "bioguideid": {
                        "type": "text",
                        "fields": {
                           "keyword": {
                              "type": "keyword",
                              "ignore_above": 256
                           }
                        }
                     },
                     "birthday": {
                        "type": "date"
                     },
                     "firstname": {
                        "type": "text",
                        "fields": {
                           "keyword": {
                              "type": "keyword",
                              "ignore_above": 256
                           }
                        }
                     },
                     "gender": {
                        "type": "text",
                        "fields": {
                           "keyword": {
                              "type": "keyword",
                              "ignore_above": 256
                           }
                        }
                     },
                     "gender_label": {
                        "type": "text",
                        "fields": {
                           "keyword": {
                              "type": "keyword",
                              "ignore_above": 256
                           }
                        }
                     },
                     "id": {
                        "type": "long"
                     },
                     "lastname": {
                        "type": "text",
                        "fields": {
                           "keyword": {
                              "type": "keyword",
                              "ignore_above": 256
                           }
                        }
                     },
                     "link": {
                        "type": "text",
                        "fields": {
                           "keyword": {
                              "type": "keyword",
                              "ignore_above": 256
                           }
                        }
                     },
                     "middlename": {
                        "type": "text",
                        "fields": {
                           "keyword": {
                              "type": "keyword",
                              "ignore_above": 256
                           }
                        }
                     },
                     "name": {
                        "type": "text",
                        "fields": {
                           "keyword": {
                              "type": "keyword",
                              "ignore_above": 256
                           }
                        }
                     },
                     "namemod": {
                        "type": "text",
                        "fields": {
                           "keyword": {
                              "type": "keyword",
                              "ignore_above": 256
                           }
                        }
                     },
                     "nickname": {
                        "type": "text",
                        "fields": {
                           "keyword": {
                              "type": "keyword",
                              "ignore_above": 256
                           }
                        }
                     },
                     "osid": {
                        "type": "text",
                        "fields": {
                           "keyword": {
                              "type": "keyword",
                              "ignore_above": 256
                           }
                        }
                     },
                     "pvsid": {
                        "type": "text",
                        "fields": {
                           "keyword": {
                              "type": "keyword",
                              "ignore_above": 256
                           }
                        }
                     },
                     "sortname": {
                        "type": "text",
                        "fields": {
                           "keyword": {
                              "type": "keyword",
                              "ignore_above": 256
                           }
                        }
                     }
                  }
               },
               "role_type_label": {
                  "type": "text",
                  "fields": {
                     "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                     }
                  }
               },
               "startdate": {
                  "type": "date"
               },
               "title": {
                  "type": "text",
                  "fields": {
                     "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                     }
                  }
               },
               "title_long": {
                  "type": "text",
                  "fields": {
                     "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                     }
                  }
               },
               "website": {
                  "type": "text",
                  "fields": {
                     "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                     }
                  }
               }
            }
         }
      }
   }
}

请注意,您有一组bo对象及其相关子字段的映射,以及后续文档的每个字段的映射。这是动态映射的效果。

要禁用此映射灵活性,可以显式禁用动态映射,并自行定义文档的结构,包括其数据类型。也许您希望bioguideid是整数而不是文本,因为它是在当前映射中定义的?我引导你去Mappings API