索引API
获取索引
var settings = new ConnectionSettings(new Uri("http://****:9200/"));
var client = new ElasticClient(settings);
{
var getIndexResponse = client.Indices.Get("index1");
if (getIndexResponse.IsValid)
{
if (getIndexResponse.Indices.Any())
{
IndexState index = getIndexResponse.Indices["index1"];
int numberOfReplicas = index.Settings.NumberOfReplicas.Value;
int numberOfShards = index.Settings.NumberOfShards.Value;
foreach (var property in index.Mappings.Properties)
{
var name = property.Value.Name;
var type = property.Value.Type;
}
}
}
}
创建索引
var createIndexResponse1 = client.Indices.Create("index111");
var createIndexResponse2 = client.Indices.Create("index222", c => c
.Settings(s => s
.NumberOfShards(1)
.NumberOfReplicas(1)
.Setting("index.write.wait_for_active_shards", "2"))
.Aliases(a => a
.Alias("alias_1")
.Alias("alias_2", aa => aa//?
.Filter<object>(f => f
.Term("user", "kimchy")
)
.Routing("kimchy")
))
.Map(m => m
.Properties(p => p
.Text(t => t
.Name("field1")
)
)
)
);
删除索引
var deleteIndexResponse = client.Indices.Delete("index111");
reindex
var reindexOnServerResponse = client.ReindexOnServer(r => r
.Source(s => s.Index("index1"))
.Destination(d => d.Index("index333"))
);
修改setting
var settingsResponse = client.Indices.UpdateSettings("twitter", u => u
.IndexSettings(i => i
.NumberOfReplicas(2)
.RefreshInterval(Time.MinusOne)
.Analysis(a => a
.Analyzers(an => an
.Custom("content", c => c
.Tokenizer("whitespace")
)
)
)
)
);
别名API
查看别名
{
//查看全部别名
var catResponse1 = client.Cat.Aliases();
if (catResponse1.IsValid)
{
catResponse1.Records.Select(r => r.Alias + "->" + r.Index);
}
//查看指定通配符的别名
var catResponse2 = client.Cat.Aliases(c => c.Name("alias_*"));
//查看指定名称的别名
var catResponse3 = client.Cat.Aliases(c => c.Name("alias_2"));
}
批量操作(添加、删除别名)
{
var aliasResponse1 = client.Indices.BulkAlias(a => a
.Remove(al => al
.Index("index222")
.Alias("alias_1")
)
.Add(a1 => a1
.Index("index222")
.Alias("alias_111")
)
.Add(a1 => a1
.Index("index222")
.Alias("alias_222")
)
.Add(a1 => a1
.Index("index*")
.Alias("alias_all")
)
);
//多个索引库指定一个别名
//指向多个索引的别名,用于执行查询不需要做额外的设置,如果需要用于执行索引操作,则需要在被指向的多个索引中指定用于执行索引操作的索引,否则报错误。需要设置个库is_write_index=true
var aliasResponse2 = client.Indices.BulkAlias(a => a
.Add(a1 => a1
.Index("index*")
.Alias("alias_all")
)
);
//删除指定索引库的全部别名
var aliasResponse3 = client.Indices.BulkAlias(a => a
.RemoveIndex(al => al
.Index("index222")
)
);
}