c# - 在Nest 7中添加附件管道

标签 c# elasticsearch nest

我有这样的课:

public class Document
{
    public string Id { get; set; }
    public string Content { get; set; }
    public Attachment Attachment { get; set; }
}

并从文件(例如pdf)中读取上下文,如下所示:
Content = Convert.ToBase64String(File.ReadAllBytes(location))

当我这样写一堆文件时:
var bulkResponse = client.Bulk(b => b
    .Pipeline("attachments")
    .IndexMany(documents)
);

我得到:
"pipeline with id [attachments] does not exist"

以前可以使用类似的代码,但现在我切换到Nest 7?

我使用以下代码创建索引(用于工作):
client.Indices.Create(indexName, c => c
    .Settings(s => s
    .Analysis(a => a
        .Analyzers(ad => ad
        .Custom("windows_path_hierarchy_analyzer", ca => ca
            .Tokenizer("windows_path_hierarchy_tokenizer")
        )
        )
        .Tokenizers(t => t
        .PathHierarchy("windows_path_hierarchy_tokenizer", ph => ph
            .Delimiter('\\')
        )
        )
    )
    )
    .Mappings(m => m
    .Map<Document>(mp => mp
        .AutoMap()
        .AllField(all => all
        .Enabled(false)
        )
        .Properties(ps => ps
        .Object<Attachment>(a => a
            .Name(n => n.Attachment)
            .AutoMap()
        )
        )
    )
    )
);

我也遇到过类似这样的事情,可以用来定义附件管道吗?
client.PutPipeline("attachments", p => p
                .Description("Document attachment pipeline")
                .Processors(pr => pr
                    .Attachment<Document>(a => a
                        .Field(f => f.Content)
                        .TargetField(f => f.Attachment)
                    )
                    .Remove<Document>(r => r
                        .Field(f => f.Content)
                    )
                )
            );

PS:

这可能起作用:
client.Ingest.PutPipeline("attachments", p => p
                .Description("Document attachment pipeline")
                .Processors(pr => pr
                    .Attachment<Document>(a => a
                        .Field(f => f.Content)
                        .TargetField(f => f.Attachment)
                    )

最佳答案

Here's a complete example,从Elastic blog post that targeted Elasticsearch 2.x, using NEST 2.x更新

using System;
using Elasticsearch.Net;
using Nest;

namespace Example
{

    private static void Main()
    {
        var defaultIndex = "attachments";
        var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

        var settings = new ConnectionSettings(pool)
            .DefaultIndex(defaultIndex)
            .DisableDirectStreaming()
            .PrettyJson()
            .OnRequestCompleted(callDetails =>
            {
                if (callDetails.RequestBodyInBytes != null)
                {
                    Console.WriteLine(
                        $"{callDetails.HttpMethod} {callDetails.Uri} \n" +
                        $"{Encoding.UTF8.GetString(callDetails.RequestBodyInBytes)}");
                }
                else
                {
                    Console.WriteLine($"{callDetails.HttpMethod} {callDetails.Uri}");
                }

                Console.WriteLine();

                if (callDetails.ResponseBodyInBytes != null)
                {
                    Console.WriteLine($"Status: {callDetails.HttpStatusCode}\n" +
                             $"{Encoding.UTF8.GetString(callDetails.ResponseBodyInBytes)}\n" +
                             $"{new string('-', 30)}\n");
                }
                else
                {
                    Console.WriteLine($"Status: {callDetails.HttpStatusCode}\n" +
                             $"{new string('-', 30)}\n");
                }
            });

        var client = new ElasticClient(settings);

      if (client.Indices.Exists(defaultIndex).Exists)
      {
        var deleteIndexResponse = client.Indices.Delete(defaultIndex);
      }

        var createIndexResponse = client.Indices.Create(defaultIndex, c => c
            .Settings(s => s
                .Analysis(a => a
                    .Analyzers(ad => ad
                        .Custom("windows_path_hierarchy_analyzer", ca => ca
                            .Tokenizer("windows_path_hierarchy_tokenizer")
                        )
                    )
                    .Tokenizers(t => t
                        .PathHierarchy("windows_path_hierarchy_tokenizer", ph => ph
                            .Delimiter('\\')
                        )
                    )
                )
            )
            .Map<Document>(mp => mp
                .AutoMap()
                .Properties(ps => ps
                    .Text(s => s
                        .Name(n => n.Path)
                        .Analyzer("windows_path_hierarchy_analyzer")
                    )
                    .Object<Attachment>(a => a
                        .Name(n => n.Attachment)
                        .AutoMap()
                    )
                )
            )
        );

      var putPipelineResponse = client.Ingest.PutPipeline("attachments", p => p
        .Description("Document attachment pipeline")
        .Processors(pr => pr
          .Attachment<Document>(a => a
            .Field(f => f.Content)
            .TargetField(f => f.Attachment)
          )
          .Remove<Document>(r => r
            .Field(ff => ff
              .Field(f => f.Content)
            )
          )
        )
      );

      var base64File = Convert.ToBase64String(File.ReadAllBytes(@"C:\Users\russc\Dropbox\Other Linqpad queries\example_one.docx"));

      var indexResponse = client.Index(new Document
      {
        Id = 1,
        Path = @"\\share\documents\examples\example_one.docx",
        Content = base64File
      }, i => i
        .Pipeline("attachments")
        .Refresh(Refresh.WaitFor)
      );

      var searchResponse = client.Search<Document>(s => s
        .Query(q => q
        .Match(m => m
          .Field(a => a.Attachment.Content)
          .Query("NEST")
        )
        )
      );

      foreach(var hit in searchResponse.Hits)
      {
        var attachment = hit.Source.Attachment;
        Console.WriteLine($"Attachment details for doc id {hit.Id}");
        Console.WriteLine($"date: {attachment.Date}");
        Console.WriteLine($"content type: {attachment.ContentType}");
        Console.WriteLine($"author: {attachment.Author}");
        Console.WriteLine($"language: {attachment.Language}");
        Console.WriteLine($"content: {attachment.Content}");
        Console.WriteLine($"content length: {attachment.ContentLength}");
      }
    }

    public class Document
    {
        public int Id { get; set; }
        public string Path { get; set; }
        public string Content { get; set; }
        public Attachment Attachment { get; set; }
    }

}

关于c# - 在Nest 7中添加附件管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57908483/

相关文章:

elasticsearch - 使用C#NEST增加弹性体的磁场极限

sorting - Elasticsearch NEST - SortAceding 不对文档进行排序

c# - Ms Access密码存储

c# - 选中/取消选中 datagridview 上的复选框

amazon-web-services - 删除亚马逊 Elasticsearch 中的旧索引

elasticsearch - 如何在ElasticSearch中的数组中搜索单独的键和值字段?

elasticsearch - 流畅的date_range映射,无需在POCO上定义Nest.DateRange属性

c# - json 字符串不适合我使用 Xamarin 创建的类

c# - 在运行时将图像拖到 RichTextBox 内的 FlowDocument 中

elasticsearch - ElasticSearch聚合+在非数值字段5.3上排序