【问题标题】:What happens when I insert 2 documents with same id in ElasticSearch?当我在 ElasticSearch 中插入 2 个具有相同 id 的文档时会发生什么?
【发布时间】:2021-11-16 18:42:20
【问题描述】:

我想知道当我在同一索引中添加具有现有 id 的文档时会发生什么。

我知道它不会重复,我不知道 Elasticsearch 是否会忽略后者或是否会更新第一个。

非常感谢。

【问题讨论】:

    标签: python elasticsearch


    【解决方案1】:

    最新的文档将简单地覆盖具有相同 ID 的前一个文档,并且版本计数将增加 1。

    很容易测试:

    # 1. create the document
    PUT test/_doc/1
    {
      "test": "me"
    }
    
    # 2. response from the creation
    {
      "_index" : "test",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 1,               <---- check this
      "result" : "created",         <---- check this
      "_shards" : {
        "total" : 2,
        "successful" : 2,
        "failed" : 0
      },
      "_seq_no" : 0,
      "_primary_term" : 1
    }
    
    # 3. update the document
    PUT test/_doc/1
    {
      "test": "me2"
    }
    
    # 4. response from the update
    {
      "_index" : "test",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 2,               <---- check this
      "result" : "updated",         <---- check this
      "_shards" : {
        "total" : 2,
        "successful" : 2,
        "failed" : 0
      },
      "_seq_no" : 1,
      "_primary_term" : 1
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-09
      • 1970-01-01
      • 2018-04-19
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 2018-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多