Delete a document from ElasticSearch index by id
Asked Answered
U

3

10

I have a document in elastic search. I am trying to implement a method where I can specify a string id to delete a document from the index using NEST client.

This is the indexed doc that I want to delete:

"hits":[{"_index":"movies","_type":"list","_id":"100","_score":0.6349302, "_source" : {
  "owner": "Bob",
  "tags": "Bobita",
  "title": "Movie clips of Bob"
}}

This is my C# code which doesn't delete the doc. It says id is NULL.

Uri localhost = new Uri("http://localhost:9200");
            var setting = new ConnectionSettings(localhost);
            setting.SetDefaultIndex("movies");
            var client = new ElasticClient(setting);

            IDeleteResponse resp = client.Delete("100");                

            if (!resp.Found)
            {
                logger.Error("Failed to delete index with id=100");
            }

What am I missing?

Ulysses answered 21/5, 2014 at 18:59 Comment(3)
i know nothing about NEST, but it appears you are missing type type (list)... the key for ES is index/type/id.Massproduce
Where and how do I specify the type and id? I am looking for that syntax and example. My my response belowUlysses
If you look at here searchcode.com/codesearch/view/26606126 you see DeleteById() which I don't get in my Nest client. I have Delete, DeleteIndex, DeleteByQuery() etcUlysses
A
7

I believe the issue here is that NEST cannot properly infer the Id property of your document because you are not specifying a type.

If possible, try this instead:

client.Delete<YourMovieType>("100");
Andryc answered 21/5, 2014 at 20:7 Comment(3)
It did work for me, so it's at least worth trying if you have this problem.Ketty
Not working for me in NEST 5.x. It leaves the document as is.Entero
How do you get the id?Padnag
P
2

Using NEST 7.x on Elasticsearch 7.0, following code works:

 var x = _client.Delete<dynamic>(1);

(where 1 is '_id' value)

Use 'dynamic' if you have not defined the mapping. Else I would suggest to use the actual type.

Painful answered 25/10, 2019 at 7:47 Comment(0)
S
0
await _elasticClient.DeleteAsync(new DeleteRequest(indexName, documentId));
Shan answered 30/1, 2023 at 21:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.