如何在文档存在时批量插入文档到ElasticSearch而不进行更新

时间:2018-04-30 20:52:17

标签: elasticsearch nest

我在Nest库中使用弹性搜索。我想知道如何在文档存在的情况下批量插入文档到ElasticSearch而不进行更新?

1 个答案:

答案 0 :(得分:3)

以下是将执行创建操作的批量API调用的示例

private static void Main()
{
    var defaultIndex = "documents";
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    var settings = new ConnectionSettings(pool)
        .DefaultIndex(defaultIndex);

    var client = new ElasticClient(settings);

    if (client.IndexExists(defaultIndex).Exists)
        client.DeleteIndex(defaultIndex);

    client.Index(new MyDocument(1) 
    { 
        Message = "new" 
    }, i => i.Refresh(Refresh.WaitFor));

    var documents = new [] 
    {
        new MyDocument(1) { Message = "updated" },
        new MyDocument(2) { Message = "updated" },
        new MyDocument(3) { Message = "updated" },
    };

    client.Bulk(b => b
        .CreateMany(documents)
        .Refresh(Refresh.WaitFor)
    );

    var getResponse = client.Get<MyDocument>(1);

    Console.WriteLine(getResponse.Source.Message == "new");
}

public class MyDocument 
{
    public MyDocument(int id) => Id = id;

    public int Id { get; set; }  

    public string Message { get; set; }
}

输出将为true,意味着在批量调用中未创建标识为1的文档,因为它已存在。如果您查看批量响应,它将是类似于

的HTTP 200响应
{
  "took" : 1387,
  "errors" : true,
  "items" : [
    {
      "create" : {
        "_index" : "documents",
        "_type" : "mydocument",
        "_id" : "1",
        "status" : 409,
        "error" : {
          "type" : "version_conflict_engine_exception",
          "reason" : "[mydocument][1]: version conflict, document already exists (current version [1])",
          "index_uuid" : "DZIgGMZcSlWRycC1MGhJWQ",
          "shard" : "3",
          "index" : "documents"
        }
      }
    },
    {
      "create" : {
        "_index" : "documents",
        "_type" : "mydocument",
        "_id" : "2",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "create" : {
        "_index" : "documents",
        "_type" : "mydocument",
        "_id" : "3",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 1,
        "status" : 201
      }
    }
  ]
}

重要的是,"errors"true,第一个"create"操作响应表明错误是什么。

使用.CreateMany(...)的另一种方法是将.UpdateMany(...)与upsert操作一起使用,指定&#34; no-op&#34;文件存在的情况下的操作

client.Bulk(b => b
    .UpdateMany(documents, (d, document) => d
        .Upsert(document)
        .Script(s => s
            .Source("ctx.op = 'none'")
        )
    )
    .Refresh(Refresh.WaitFor)
);

结果相同,即ID 1的文档未被覆盖,但响应略有不同

{
  "took" : 1307,
  "errors" : false,
  "items" : [
    {
      "update" : {
        "_index" : "documents",
        "_type" : "mydocument",
        "_id" : "1",
        "_version" : 1,
        "result" : "noop",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "status" : 200
      }
    },
    {
      "update" : {
        "_index" : "documents",
        "_type" : "mydocument",
        "_id" : "2",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "update" : {
        "_index" : "documents",
        "_type" : "mydocument",
        "_id" : "3",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 1,
        "status" : 201
      }
    }
  ]
}

请注意,"errors"现在为false,第一个"update"操作为"noop"