简体   繁体   中英

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

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.

I ended up using

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM