弹性搜索映射器附件,NEST - 索引时抛出错误

Elastic search mapper-attachments, NEST - throwing error on indexing

我最近开始使用elastic search,我试图用mapper-attachment插件索引一个pdf文档,当索引时我遇到错误值不能为空参数名称:索引,有人可以帮忙吗修复这个..

索引代码如下:

    static void Main(string[] args)
    {

        var connectionSettings = new ConnectionSettings(new Uri("http://localhost:9200/"));
        var elasticClient = new ElasticClient(connectionSettings);

        elasticClient.CreateIndex("pdf-index", c => c.AddMapping<Document>(m => m.MapFromAttributes()));

        var attachment = new Attachment
        {
            Content = Convert.ToBase64String(File.ReadAllBytes("test.pdf")),
            ContentType = "application/pdf",
            Name = "test.pdf"
        };

        var doc = new Document()
        {
            Id = 1,
            Title = "test",
            File = attachment
        };

        elasticClient.Index(doc);
    }

    public class Attachment
    {
        [ElasticProperty(Name = "_content")]
        public string Content { get; set; }

        [ElasticProperty(Name = "_content_type")]
        public string ContentType { get; set; }

        [ElasticProperty(Name = "_name")]
        public string Name { get; set; }
    }

    public class Document
    {
        public int Id { get; set; }

        public string Title { get; set; }

        [ElasticProperty(Type = FieldType.Attachment, TermVector = TermVectorOption.WithPositionsOffsets, Store = true)]
        public Attachment File { get; set; }

    }

我猜你忘了在弹性搜索连接设置中添加默认索引。

var uri = new Uri("http://localhost:9200/");
var connectionSettings = new ConnectionSettings(uri, defaultIndex: "my-application");

var elasticClient = new ElasticClient(connectionSettings);

更多信息在这里http://nest.azurewebsites.net/nest/connecting.html

当然默认索引是可选的。那么你必须在索引文档时指定索引。 例如

client.Index(doc, i => i
     .Index(index)
     .Type(type)     
);