【问题标题】:How to use the attachment processor and remove processor within an array of attachments with NEST client?如何在 NEST 客户端的附件数组中使用附件处理器和删除处理器?
【发布时间】:2017-03-16 17:11:55
【问题描述】:

问题描述

我想使用附件处理器并删除附件数组中的处理器。我知道为此目的需要 foreach 处理器。

这使得附件处理器和移除处理器能够在阵列的各个元素上运行 (https://www.elastic.co/guide/en/elasticsearch/plugins/current/ingest-attachment-with-arrays.html)

我没有找到任何用于索引附件数组和删除内容字段的好的 NEST(c#) 示例。有人可以为我的用例提供 NEST(C#) 示例吗?

更新:感谢 Russ Cam,现在可以使用以下管道索引附件数组并删除 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")
                        )
                    )
                )
            )
        );

【问题讨论】:

    标签: elasticsearch nest


    【解决方案1】:

    您的代码缺少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 字段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多