【问题标题】:ElasticSearch/Kibana fields on update of document文档更新时的 ElasticSearch/Kibana 字段
【发布时间】:2016-11-09 16:41:45
【问题描述】:

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

起初我在 Kibana 的开发工具选项卡中输入了这个:

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 保留更新文档中不再存在的字段是否正常?

显然,除了特别有趣的内容之外,请不要对今天的选举结果进行调侃。

【问题讨论】:

    标签: elasticsearch kibana


    【解决方案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

    【讨论】:

      猜你喜欢
      • 2020-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-27
      相关资源
      最近更新 更多