2016-07-22 93 views
2

我无法通过其ID删除索引中的对象。我试着解决方案如下:Elasticsearch NEST 2.0无法通过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 

任何帮助?谢谢!

回答

3

你忘了通过索引名称参数:

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

希望它有帮助。