使用 Nest (Elasticsearch) 执行 DeleteByQuery 时获取 "An item with this key has already been added"

Getting "An item with this key has already been added" when performing a DeleteByQuery with Nest (Elasticsearch)

我正在尝试 运行 删除查询以删除索引中两个时间戳之间的文档,但我得到了一个非常奇怪的结果。

这是我的代码:

// how the index is created
if (!es.IndexExists(indexName).Exists)
{
    es.CreateIndex(descriptor => descriptor
        .Index(indexName)
        .AddMapping<MyDocument>(m => m
            .MapFromAttributes()));
}

// event object that is mapped
public class MyDocument
{
    public long Id { get; set; }

    public long EventTime { get; set; }

    [ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]
    public string EventType { get; set; }

    public DateTime CreatedAt { get; set; }

    public IDictionary<string, object> Values { get; set; }
    // snip
}

// delete call
IElasticClient es;
es.DeleteByQuery<MyDocument>(q => q
    .Query(rq => rq
        .Range(t => t.OnField("eventTime").GreaterOrEquals(startTimestamp).LowerOrEquals(endTimestamp)));

这会抛出一个异常 "An item with the same key has already been added"。这个会引发此异常的删除查询我做错了什么?

这是我通过 elasticsearch 进行的搜索中的示例文档:

{
  "took" : 8,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 96,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "testing2",
      "_type" : "mydocument",
      "_id" : "112",
      "_score" : 1.0,
      "_source":{"id":112,"eventTime":12345690,"eventDate":"1970-05-23T17:21:30-04:00","eventTypeId":0,"ready":false,"name":"","doccount":0,"wordcount":0,"createdAt":"2015-06-25T09:29:33.8996707-04:00","values":{"internal_timestamp":76890.0},"childDocuments":[],"parentDocuments":[]}
    }, /* snip */]
  }
}

Rob 和我刚刚解决了这个问题。

这里出现的问题是我的项目使用 Newtonsoft.Json 版本 7.0.0 和 Nest 版本 1.5.1,而 Nest 1.5.1 需要 6.0.1 .这种不匹配导致查询的序列化抛出异常。

这可以通过将 Nest 升级到版本 1.6.1 或将 Newtonsoft.Json 降级到版本 6.0.1 来解决。

我只是将 newtonsoft.json 更新到最新版本。我当前的版本是 5.0.1,我已经更新到 9.0.1。之后,错误消失了。