How do I delete an Index using NEST 7.4.1?
Asked Answered
C

2

9

I am new to Elastic search and I have written code to index a list of City. I am using "elasticsearch head" add-on for chrome to check and manipulate the indexes and _doc.

While indexing and CRUD operation of doc is resulting correctly, I had to delete the index manually via elastic-search add-on.

I want to check for the index first, if an index is available, delete it, and create the index & index the list of City again. This is what I want to do. But getting an error in Delete() method saying

argument 1: cannot convert from string to Nest.IDeleteRequest

Following is my code to show you what I am doing:

        public async Task<List<BulkResponseItemBase>> AddNewIndex(string index_name, List<City> model)
        {
            client = new ElasticClient(elstcstngs);
            List<BulkResponseItemBase> elstcManyrespoStatusList = new List<BulkResponseItemBase>();
            if (await CheckIndexExists(index_name))
            {
               //This is where I am getting error - Cannot Convert from string to Nest.IDeleteRequest
                client.Delete(index_name); 
            }
            elstcstngs.DefaultMappingFor<City>(m => m.IndexName(index_name));
            BulkResponse elstcManyrespoStatus = await client.IndexManyAsync<City>(model, null);
            if (elstcManyrespoStatus.Errors)
            {
                foreach (var itemWithError in elstcManyrespoStatus.ItemsWithErrors)
                {
                    elstcManyrespoStatusList.Add(itemWithError);
                    System.Diagnostics.Debug.WriteLine("Failed to index document {0}: {1}", itemWithError.Id, itemWithError.Error);
                }
            }
            return elstcManyrespoStatusList;
        }

I have searched the Elastic search Documentation but could not find any API in NEST 7.4.1 documentation, which will delete the index itself. Instead what I am getting is of NEST version 1.x.

Any link towards a documentation or any help regarding the code will very helpful.

Thank you.

Catechol answered 1/12, 2019 at 12:9 Comment(3)
client.Delete method does not accept index name as parameter. It requires DeleteRequest object. That's what the error says. Or you need to use DeleteIndex method. elastic.co/guide/en/elasticsearch/client/net-api/1.x/…Johanna
I haven't had luck looking for an example in the docs either, but it looks like the newer version of the API does not work with .Delete(indexname), but rather with .Delete(new DeleteRequest(…)). Which implementations of IDeleteRequest do exist? Can you instantiate one of them and parametrize them with the index name?Nephew
No, It was throwing error. Now you have the same Delete() Inside Indices property. Feeling dumb, why I could not get it earlier.Catechol
S
22

As you can see from client.Delete method description enter image description here

it exposes elasticsearch delete API which is responsible for deleting documents from elasticsearch.

If you want to remove index you can do this with

await client.Indices.DeleteAsync("index_name");

Hope that helps.

Slav answered 2/12, 2019 at 7:44 Comment(2)
is there a way to disable that so the user will not be able to delete indeces using this code? Question for my acceptance tests.Gourde
@Gourde not sure what you are trying to achieve, could you please put some more context?Slav
I
0

With the last version by 2023-04-12 following code works:

client.Indices.Delete(new DeleteIndexRequest(Indices.Index("IndexName")));
Indecisive answered 12/4, 2023 at 14:7 Comment(2)
Do you think it's possible to disable that so no user can delete indeces using this command? That's for Acceptance tests.Gourde
This is a C# program piece. You must manage who can run this code in the authorization section of the program yourself.Indecisive

© 2022 - 2024 — McMap. All rights reserved.