(转)c#使用nest
2021-07-11 02:07
标签:blog base64编码 需要 LIDS process 链接 数据 guide RoCE elsticsearch中安装了x-pack后,查询时就需要用户名和密码了。 访问basic认证的页面 (2)在url中添加用户名和密码来访问: (3)在请求头中添加Authorization来访问: 常用C#查询组件 官方组件Elasticsearch.Net & NEST NEST手册
无账号密码,不可访问
curl http://192.168.0.2:9200/testindex/_count?pretty=true
Authentication Required
(1)通过user选项带上账号密码,返回正常数据
curl –user elastic:changeme http://192.168.0.2:9200/testindex/_count?pretty=true
http://elastic:changeme@192.168.0.2:9200/testindex/_count?pretty=true
Authorization: “Basic 用户名和密码的base64加密字符串”//HTTP Basic 验证客户端 C#实现:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
request.Credentials = CredentialCache.DefaultCredentials;
//获得用户名密码的Base64编码
string code= Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", "username", "password")));
//添加Authorization到HTTP头
request.Headers.Add("Authorization", "Basic " + code);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string content= reader.ReadToEnd();
--
//建立映射类
[ElasticsearchType(Name = "user", IdProperty = "Ncid")]
public class User
{
[Number(Name = "ncid")]
public int Ncid { get; set; }
[Text(Name = "name")]
public string Name { get; set; }
[Text(Name = "webclass")]
public string Webclass { get; set; }
[Date(Name = "birthday", Format = "yyyy-MM-dd", IgnoreMalformed = true, Coerce = true)]
public DateTime? Birthday { get; set; }
[Text(Name = "eduname")]
public string Eduname { get; set; }
[Number(Name = "sex", IgnoreMalformed = true, Coerce = true)]
public int Sex { get; set; }
[Text(Name = "selfment")]
public string Selfment { get; set; }
[Date(Name = "refreshtime", IgnoreMalformed = true, Coerce = true)]
public DateTime? Refreshtime { get; set; }
}
//链接:
private ElasticClient Client()
{
var nodes = new Uri[]
{
new Uri("http://192.168.0.2:9200")
};
var pool = new StaticConnectionPool(nodes);
var settings = new ConnectionSettings(pool)
.DefaultIndex("testindex")
.BasicAuthentication("elastic", "changeme");
return new ElasticClient(settings);
}
//添加记录
var client = Client();
var modUser4 = new User
{
Ncid = 4,
Name = "mygod4",
Sex = 1,
Eduname = "硕士",
Birthday = DateTime.Now.AddYears(-20),
Selfment = "中国长春市长春药店",
Webclass = "www.b.com",
Refreshtime = DateTime.Now,
};
client.Create(modUser4);
//删除记录
var client = Client();
//删除单条记录
var rtnDel = client.Delete
NEST使用方法
http://www.cnblogs.com/huhangfei/p/5726650.html
其他组件
plainElastic.net
参考:
http://www.cnblogs.com/eggTwo/p/4425269.html
http://blog.csdn.net/wulex/article/details/52138564
http://www.cnblogs.com/hyl8218/archive/2011/07/04/2097394.html
http://blog.csdn.net/kingson88/article/details/51252606
原文
https://blog.csdn.net/manimanihome/article/details/55682494
(转)c#使用nest
标签:blog base64编码 需要 LIDS process 链接 数据 guide RoCE
原文地址:https://www.cnblogs.com/Hero-/p/9662483.html