泡沫之夏的歌曲,刘德华的简历,北欧神话歌词
nuget引用nest
源码可查elasticclient.cs
new一个elasticclient有多种方式
es地址是http://localhost:9200
,可以直接new,如下所示
var client = new elasticclient();
源码中显示 new elasticclient()
public elasticclient() : this(new connectionsettings(new uri("http://localhost:9200"))) { }
由此可以推断一下,如果本地安装的使用不是9200端口或者远程连接es服务端,可以这么写
string uri = "http://example.com:9200"; var settings = new connectionsettings(new uri(uri)). defaultindex("people"); var client = new elasticclient(settings);
uri uri = new uri("http://example.com:9200"); var client = new elasticclient(uri);
这里只举例官方文档中推荐使用的sniffingconnectionpool
sniffingconnectionpool线程安全,开销很小,允许运行时reseeded。不明白reseeded的运行机制,留个疑问。
public static elasticclient getelasticclientbypool() { var uris = new[] { new uri("http://localhost:9200"), new uri("http://localhost:9201"), new uri("http://localhost:9202"), }; var connectionpool = new sniffingconnectionpool(uris); var settings = new connectionsettings(connectionpool) .defaultindex("people"); var client = new elasticclient(settings); return client; }
参考:
判断索引是否存在
var existsresponse = _elasticclient.indexexists(_indexname); var indexexists = existsresponse.exists
indexexists 为true,索引存在;为false,索引不存在。
创建索引并初始化索引配置和结构
//基本配置 iindexstate indexstate = new indexstate { settings = new indexsettings { numberofreplicas = 1,//副本数 numberofshards = 6//分片数 } }; icreateindexresponse response = _elasticclient.createindex(_indexname, p => p .initializeusing(indexstate) .mappings(m => m.map<people>(r => r.automap())) ); if (response.isvalid) { console.writeline("索引创建成功"); } else { console.writeline("索引创建失败"); }
注意:索引名称必须为小写,如果含有大写字母,异常信息为"invalid index name [test], must be lowercase"
demo地址:createindex
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Blazor server side 自家的一些开源的, 实用型项目的进度之 CEF客户端
.NET IoC模式依赖反转(DIP)、控制反转(Ioc)、依赖注入(DI)
vue+.netcore可支持业务代码扩展的开发框架 VOL.Vue 2.0版本发布
网友评论