检测和重建ElasticSearch指数

时间:2015-10-21 14:18:26

标签: c# elasticsearch nest

我们想要创建一个函数,可以检测我们的预期ES类型和我们的定义之间是否存在差异,以及是否需要重建索引。

我们正在根据documentation使用数据注释生成类型映射。

   [ElasticType(IdProperty = "Id", Name = "blog_post")]
    public class BlogPost
    {
        [ElasticProperty(Name = "_id", Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String)]
        public Guid id { get; set; }

        [ElasticProperty(Type = FieldType.String, Name = "Title", Index = FieldIndexOption.Analyzed)]
        public String Title { get; set; }

        [ElasticProperty(Type = FieldType.Nested, Name = "Keywords", Index = FieldIndexOption.Analyzed)]
        public ICollection<Keyword> Keywords { get; set; }

        [ElasticProperty(Type = FieldType.String, Name = "NotSearchable", Index = FieldIndexOption.NotAnalyzed)]
        public String NotSearchable { get; set; }

        [ElasticProperty(Type = FieldType.String, Name = "Text", Index = FieldIndexOption.Analyzed)]
        public String Text { get; set; }
    }
    public class Keyword
    {
        public Guid id { get; set; }
        public String Text { get; set; }
    }

理论是创建一个新的索引,然后比较Mapping属性,如果有任何差异,那么我们需要重建索引。下面我使用我最初创建第一个索引类型的相同对象创建一个空索引。(理论是随着时间的推移,对象可能会添加新属性,或者我们可能会更改ElasticProperties)。

var response = client.CreateIndex(ci => ci.Index("timbonest2").AddMapping<BlogPost>(m => m.MapFromAttributes()));
            if (response.IsValid)
            {
                var map = client.GetMapping<BlogPost>(f => f.Index("timbonest"));
                var map2 = client.GetMapping<BlogPost>(f => f.Index("timbonest3"));
            }

有了NEST方法或低级方法,一旦我有了映射集合,就会允许这种比较吗?

0 个答案:

没有答案