Delete a document from ElasticSearch index by id

ⅰ亾dé卋堺 提交于 2019-12-10 08:29:37

问题


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?


回答1:


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");



回答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.



来源:https://stackoverflow.com/questions/23791636/delete-a-document-from-elasticsearch-index-by-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!