Checking if a document exists in index using NEST

5.5k Views Asked by At

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

2

There are 2 best solutions below

1
Rob On BEST ANSWER

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.

0
I.G. Pascual On

I ended up using

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

as I couldn't use the generic method DocumentExists<T>(..)