2016-07-22 94 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); 

希望它有幫助。