2017-03-14 69 views
0

問題描述如何使用附件處理器並使用NEST客戶端刪除附件陣列中的處理器?

我想使用附件處理器和附件陣列中的處理器。我意識到爲此需要使用foreach處理器。

這使得附接的處理器和除去處理器要在陣列中的單個元件上運行(https://www.elastic.co/guide/en/elasticsearch/plugins/current/ingest-attachment-with-arrays.html

我不找到任何好的NEST(C#)實施例用於索引附件的陣列,並且移除所述內容字段。有人可以爲我的用例提供一個NEST(C#)示例嗎?

UPDATE:感謝拉斯凸輪,現在已經可以索引附件的數組,並刪除base64編碼的文件內容與以下管道:

_client.PutPipeline("attachments", p => p 
      .Description("Document attachments pipeline") 
      .Processors(pp => pp 
       .Foreach<ApplicationDto>(fe => fe 
        .Field(f => f.Attachments) 
        .Processor(fep => fep 
         .Attachment<Attachment>(a => a 
          .Field("_ingest._value._content") 
          .TargetField("_ingest._value.attachment") 
         ) 
        ) 
       ).Foreach<ApplicationDto>(fe => fe 
        .Field(f => f.Attachments) 
        .Processor(fep => fep 
         .Remove<Attachment>(r => r 
          .Field("_ingest._value._content") 
         ) 
        ) 
       ) 
      ) 
     ); 

回答

3

您的代碼缺少ForeachProcessor;這個NEST實現幾乎是Elasticsearch JSON示例的直接翻譯。 It's a little easier using the Attachment type available in NEST too,其中attachment對象提取的數據將反序列化成。

void Main() 
{ 
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200")); 
    var defaultIndex = "default-index"; 
    var connectionSettings = new ConnectionSettings(pool) 
     .DefaultIndex(defaultIndex); 

    var client = new ElasticClient(connectionSettings); 

    if (client.IndexExists(defaultIndex).Exists) 
     client.DeleteIndex(defaultIndex); 

    client.PutPipeline("attachments", p => p 
     .Processors(pp => pp 
      .Description("Document attachment pipeline") 
      .Foreach<Document>(fe => fe 
       .Field(f => f.Attachments) 
       .Processor(fep => fep 
        .Attachment<Attachment>(a => a 
         .Field("_ingest._value.data") 
         .TargetField("_ingest._value.attachment") 
        ) 
       ) 
      ) 
     ) 
    ); 

    var indexResponse = client.Index(new Document 
     { 
      Attachments = new List<DocumentAttachment> 
      { 
       new DocumentAttachment { Data = "dGhpcyBpcwpqdXN0IHNvbWUgdGV4dAo=" }, 
       new DocumentAttachment { Data = "VGhpcyBpcyBhIHRlc3QK" } 
      } 
     }, 
     i => i.Pipeline("attachments") 
    ); 

    var getResponse = client.Get<Document>(indexResponse.Id); 
} 

public class Document 
{ 
    public List<DocumentAttachment> Attachments { get; set; } 
} 

public class DocumentAttachment 
{ 
    public string Data { get; set; } 

    public Attachment Attachment { get; set; } 
} 

回報

{ 
    "_index" : "default-index", 
    "_type" : "document", 
    "_id" : "AVrOVuC1vjcwkxZzCHYS", 
    "_version" : 1, 
    "found" : true, 
    "_source" : { 
    "attachments" : [ 
     { 
     "data" : "dGhpcyBpcwpqdXN0IHNvbWUgdGV4dAo=", 
     "attachment" : { 
      "content_type" : "text/plain; charset=ISO-8859-1", 
      "language" : "en", 
      "content" : "this is\njust some text", 
      "content_length" : 24 
     } 
     }, 
     { 
     "data" : "VGhpcyBpcyBhIHRlc3QK", 
     "attachment" : { 
      "content_type" : "text/plain; charset=ISO-8859-1", 
      "language" : "en", 
      "content" : "This is a test", 
      "content_length" : 16 
     } 
     } 
    ] 
    } 
} 

你可以連上RemoveProcessor_source刪除data場了。

相關問題