【问题标题】:Can we do a bulk index without specifying a document ID for Elasticsearch?我们可以在不为 Elasticsearch 指定文档 ID 的情况下进行批量索引吗?
【发布时间】:2013-03-02 12:05:55
【问题描述】:

是否可以在不指定文档 ID 的情况下进行批量索引?我希望 Elasticsearch 在索引时为我生成一个随机 ID,但可以这样做吗?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    是的,你可以!

    在 0.90.0.Beta1 上测试:

    $ cat requests 
    { "index" : { "_index" : "test", "_type" : "type1" } }
    { "field1" : "value1" }
    
    $ curl -s -XPOST localhost:9200/_bulk --data-binary @requests; echo
    {"took":6,"items":[{"create":{"_index":"test","_type":"type1","_id":"IWqsRqyhRVq-F69OLIngTA","_version":1,"ok":true}}]}
    

    【讨论】:

    • 能否通过脚本提供相同操作的示例JAVA API?
    • 谢谢,@dadoonet。是否可以在不指定 id 的情况下更新多个文档?
    • 不适用于批量 API。查询更新可能是您想要的。
    【解决方案2】:

    这是另一个不指定 _id 的批量上传示例。

    `

    curl -XPOST "http://localhost:9200/_bulk" -d'
        { "index" : { "_index" : "test", "_type" : "demo" } }
        { "title" : "Quick brown rabbits", "content" : "Brown rabbits are commonly seen" }
        { "index" : { "_index" : "test", "_type" : "demo" } }
        { "title" : "Keeping pets healthy", "content" : "My quick brown fox eats rabbits
    

    `

    回复如下

    `

    {
      "took": 451,
      "errors": false,
      "items": [
        {
          "create": {
            "_index": "test",
            "_type": "demo",
            "_id": "AVYDtp_fxosF2Bdj7ghV",
            "_version": 1,
            "_shards": {
              "total": 2,
              "successful": 1,
              "failed": 0
            },
            "status": 201
          }
        },
        {
          "create": {
            "_index": "test",
            "_type": "demo",
            "_id": "AVYDtp_fxosF2Bdj7ghW",
            "_version": 1,
            "_shards": {
              "total": 2,
              "successful": 1,
              "failed": 0
            },
            "status": 201
          }
        }
      ]
    }
    

    `

    【讨论】:

    • 无法使用这个技巧,它说:“type”:“action_request_validation_exception”,“reason”:“验证失败:1:如果设置了版本类型或值,则必须提供一个id;”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    • 1970-01-01
    • 2020-06-20
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多