Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a document from ElasticSearch index by id

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?

like image 407
kheya Avatar asked May 21 '14 18:05

kheya


People also ask

How do I delete a specific index in Elasticsearch?

To delete the index, you must roll over the data stream so a new write index is created. You can then use the delete index API to delete the previous write index.

Which of the following commands is used to delete an index from Elasticsearch?

Once you have the index you wish to remove from Elasticsearch, use the DELETE request followed by the index name.

How do I find Elasticsearch document ID?

To get the ID of a document, simply use the _id field in the search query. For example, if you want to find the document with the ID "12345", you would use the following query: _id:12345. This would return the document with the ID "12345".

How does delete work in Elasticsearch?

You can specify the query criteria in the request URI or the request body using the same syntax as the Search API. When you submit a delete by query request, Elasticsearch gets a snapshot of the data stream or index when it begins processing the request and deletes matching documents using internal versioning.


2 Answers

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");
like image 200
Greg Marzouka Avatar answered Oct 07 '22 02:10

Greg Marzouka


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.

like image 30
Atur Avatar answered Oct 07 '22 02:10

Atur