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

时间:2015-03-31 03:01:03

标签: elasticsearch attachment nest elasticsearch-plugin

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

enter image description here

以下是索引编码:

    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; }

    }

1 个答案:

答案 0 :(得分:0)

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

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

当然默认索引是可选的。然后你必须在索引doc时指定索引。 例如

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