【问题标题】:Checking if a document exists in index using NEST使用 NEST 检查索引中是否存在文档
【发布时间】:2016-10-01 00:51:12
【问题描述】:

我正在重新索引我的索引,但每当我尝试删除一个不存在的文档时都会遇到问题,因此我需要检查该文档是否已经存在。

该方法在elasticsearch docs中只是说明。

我发现a question 有一些有趣的代码,我已经尝试过了

var docExists = client.DocumentExists<object>(d => d
    .Index(indexname)
    .Id(myId)
    .Type("Abcdef"));

但是编译器报错

无法将 lambda 表达式转换为类型“Nest.DocumentPath”,因为它不是委托类型

我想我的错误是因为问题涉及 NEST 1.x 而我使用的是 NEST 2.x。

我知道我可以做一个简单的查询,但我想知道是否有像 ES doc-exists 这样的直接方式。

谢谢

【问题讨论】:

  • 您使用的是什么版本的 Nest?
  • @bittusarkar Elasticsearch 2.0.0,Nest 2.0.2

标签: c# elasticsearch elasticsearch-2.0 nest2


【解决方案1】:

DocumentExists 的签名在 NEST 2.x 中略有改变。

现在看起来像:

public IExistsResponse DocumentExists<T>(DocumentPath<T> document, Func<DocumentExistsDescriptor<T>, IDocumentExistsRequest> selector = null) where T : class

你的例子可以表达如下

client.DocumentExists<Document>(myId, d => d
    .Index(indexname)
    .Type("Abcdef"));

如果您对DocumentPath&lt;T&gt; 感到好奇,请阅读this NEST 文档的伟大和平。

【讨论】:

  • 谢谢!尽管client.DocumentExists(new DocumentExistsRequest(indexName, type.Name, myId)),我最终得到了这个解决方案,因为我不能使用通用方法
【解决方案2】:

我最终使用了

client.DocumentExists(new DocumentExistsRequest(indexName, type.Name, myId))

因为我不能使用通用方法DocumentExists&lt;T&gt;(..)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-26
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    相关资源
    最近更新 更多