Search code examples
c#elasticsearchelasticsearch-2.0nest2

Checking if a document exists in index using NEST


I'm reindexing my index, but I've encountered a problem whenever I try to delete a non-existing document, so I need to check if the document already exists.

The approach is just explained in the elasticsearch docs.

I found a question with some interesting code, which I already tried

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

But the compiler is giving an error

Cannot convert lambda expression to type 'Nest.DocumentPath<object>' because it's not a delegate type

I suppose my error comes because the question refers to NEST 1.x and I'm using NEST 2.x.

I know I can do a simple query, but I want to know if there is a direct way like ES doc-exists.

Thanks


Solution

  • Signature of DocumentExists changed a little bit in NEST 2.x.

    Right now it looks like:

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

    Your example could be expressed as follows

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

    If you are curious about DocumentPath<T> please read this great peace of NEST docs.