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.

Ashish Gope
  • 320
  • 1
  • 5
  • 13
  • `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. https://www.elastic.co/guide/en/elasticsearch/client/net-api/1.x/delete-indices.html – Chetan Dec 01 '19 at 12:31
  • 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? – knittl Dec 01 '19 at 14:57
  • No, It was throwing error. Now you have the same ```Delete()``` Inside ```Indices``` property. Feeling dumb, why I could not get it earlier. – Ashish Gope Dec 08 '19 at 18:48

2 Answers2

21

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.

Rob
  • 9,664
  • 3
  • 41
  • 43
  • 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. – Marin Aug 08 '23 at 15:38
  • @Marin not sure what you are trying to achieve, could you please put some more context? – Rob Aug 08 '23 at 18:31
0

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

client.Indices.Delete(new DeleteIndexRequest(Indices.Index("IndexName")));
  • Do you think it's possible to disable that so no user can delete indeces using this command? That's for Acceptance tests. – Marin Aug 08 '23 at 15:39
  • This is a C# program piece. You must manage who can run this code in the authorization section of the program yourself. – Abdulkadir Bener Aug 23 '23 at 09:33