3

I'm unable to delete an object from an index by its ID. I've tried solutions in the following:

And they produce a variety of failures or inability to compile which I'm unable to resolve.

Simple example:

public class Doc
{
    public int DocID;
    public string Name;
}

static void Main(string[] args)
{
    try
    {
        string indexName = "idx";
        Uri uri = new Uri("http://localhost:9200");
        ConnectionSettings settings = new ConnectionSettings(uri);
        ElasticClient client = new ElasticClient(settings);
        client.CreateIndex(indexName);

        Doc d1 = new Doc();
        d1.DocID = 1;
        d1.Name = "foo";

        Doc d2 = new Doc();
        d2.DocID = 2;
        d2.Name = "bar";

        var d1add = client.Index(d1, i => i.Index(indexName).Type(typeof(Doc)).Id(d1.DocID));
        Console.WriteLine("D1 Add Response: " + d1add);
        var d2add = client.Index(d2, i => i.Index(indexName).Type(typeof(Doc)).Id(d2.DocID));
        Console.WriteLine("D2 Add Response: " + d2add);

        // 
        // I will try a variety of delete operations here...
        //

        var d1remove = client.Delete<Doc>(d1.DocID);

        Console.WriteLine("D1 Reove Response: " + d1remove);
    }
    catch (Exception e)
    {
        PrintException(e);
    }
}

Produces the following output:

D1 Add Response: Valid NEST response built from a successful low level call on PUT: /idx/doc/1
D2 Add Response: Valid NEST response built from a successful low level call on PUT: /idx/doc/2
================================================================================
 = Exception Type: System.ArgumentException
 = Exception Data: System.Collections.ListDictionaryInternal
 = Inner Exception:
 = Exception Message: Dispatching Delete() from NEST into to Elasticsearch.NET failed
Received a request marked as DELETE
This endpoint accepts DELETE
The request might not have enough information provided to make any of these endpoints:
  - /{index=<NULL>}/{type=doc}/{id=1}

 = Exception Source: Nest
 = Exception StackTrace:    at Nest.LowLevelDispatch.DeleteDispatch[T](IRequest`1 p) in C:\Users\russ\source\elasticsearch-net-2.x\src\Nest\_Generated\_LowLevelDispatch.generated.cs:line 734
   at Nest.ElasticClient.<Delete>b__193_0(IDeleteRequest p, PostData`1 d) in C:\Users\russ\source\elasticsearch-net-2.x\src\Nest\Document\Single\Delete\ElasticClient-Delete.cs:line 48
   at Nest.ElasticClient.Nest.IHighLevelToLowLevelDispatcher.Dispatch[TRequest,TQueryString,TResponse](TRequest request, Func`3 responseGenerator, Func`3 dispatch) in C:\Users\russ\source\elasticsearch-net-2.x\src\Nest\ElasticClient.cs:line 56
   at Nest.ElasticClient.Nest.IHighLevelToLowLevelDispatcher.Dispatch[TRequest,TQueryString,TResponse](TRequest request, Func`3 dispatch) in C:\Users\russ\source\elasticsearch-net-2.x\src\Nest\ElasticClient.cs:line 46
   at Nest.ElasticClient.Delete(IDeleteRequest request) in C:\Users\russ\source\elasticsearch-net-2.x\src\Nest\Document\Single\Delete\ElasticClient-Delete.cs:line 46
   at Nest.ElasticClient.Delete[T](DocumentPath`1 document, Func`2 selector) in C:\Users\russ\source\elasticsearch-net-2.x\src\Nest\Document\Single\Delete\ElasticClient-Delete.cs:line 42
   at ElasticSearchCLI.Program.Main(String[] args) in D:\code\test\ElasticSearchCLI\ElasticSearchCLI\Program.cs:line 52

When I replace the code in question with:

var d1remove = client.DeleteByQuery<Doc>(q => q.Indices(new[] { indexName }).Query(rq => rq.Term(f => f.DocID, idval)));

It won't compile; I'm given the warning of:

There is no argument given that corresponds to the required formal parameter 'types' of 'ElasticClient.DeleteByQuery<T>(Indices, Types, Func<DeleteByQueryDescriptor<T>, IDeleteByQueryRequest>)'

When I change the code in question to:

var d1remove = client.Delete<Doc>(d => d.Id(d1.DocID).Index(indexName));

It also gives an error:

Error   CS1660  Cannot convert lambda expression to type 'DocumentPath<Program.Doc>' because it is not a delegate type  ElasticSearchCLI    D:\code\test\ElasticSearchCLI\ElasticSearchCLI\Program.cs

Finally when I try:

QueryContainer qcremove = null;
qcremove &= new TermQuery { Field = "DocID", Value = d1.DocID};
var deleteRequest = new DeleteByQueryRequest(Indices.Parse(indexName), Types.Parse("Doc"));
deleteRequest.Query = qcremove;
var d1remove = client.DeleteByQuery(deleteRequest);

It compiles, but gives the following error when run:

D1 Add Response: Valid NEST response built from a successful low level call on PUT: /idx/doc/1
D2 Add Response: Valid NEST response built from a successful low level call on PUT: /idx/doc/2
D1 Remove Response: Invalid NEST response built from a unsuccessful low level call on DELETE: /idx/Doc/_query

Any help? Thanks!

Community
  • 1
  • 1
joelc
  • 2,687
  • 5
  • 40
  • 60

1 Answers1

5

You forgot to pass index name parameter:

var response = client.Delete<Document>(1, d => d.Index("indexName"));

I'm fan of specifying default index when creating ConnectionSettings, this way I don't have to set index name parameter in my calls.

var settings = new ConnectionSettings()
    .DefaultIndex("indexName)
    .PrettyJson();

var client = new ElasticClient(settings);

client.Delete<Document>(1);

Hope it helps.

Rob
  • 9,664
  • 3
  • 41
  • 43