Elasticsearch NEST 2.0 无法通过 ID 删除对象

Elasticsearch NEST 2.0 unable to delete object by ID

我无法通过 ID 从索引中删除对象。我尝试了以下解决方案:

并且它们会产生各种我无法解决的失败或无法编译。

简单示例:

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

产生以下输出:

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

当我将有问题的代码替换为:

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

它不会编译;我收到以下警告:

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

当我将有问题的代码更改为:

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

同样报错:

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

最后当我尝试时:

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

编译成功,但在运行时出现如下错误:

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

有什么帮助吗?谢谢!

您忘记传递索引名称参数:

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

我喜欢在创建 ConnectionSettings 时指定默认索引,这样我就不必在调用中设置索引名称参数。

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

var client = new ElasticClient(settings);

client.Delete<Document>(1);

希望对您有所帮助。