(轉)c#使用nest
elsticsearch中安裝了x-pack後,查詢時就需要用戶名和密碼了。
無賬號密碼,不可訪問
curl http://192.168.0.2:9200/testindex/_count?pretty=true
Authentication Required
訪問basic認證的頁面
(1)通過user選項帶上賬號密碼,返回正常數據
curl –user elastic:changeme http://192.168.0.2:9200/testindex/_count?pretty=true
(2)在url中添加用戶名和密碼來訪問:
http://elastic:[email protected]:9200/testindex/_count?pretty=true
(3)在請求頭中添加Authorization來訪問:
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();
常用C#查詢組件
官方組件Elasticsearch.Net & NEST
NEST手冊
--
//建立映射類
[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<User>(new DocumentPath<User>(2));
//刪除多條記錄
var delIds = new List<int>() { 4, 5 };
var bulkDel = new BulkRequest() { Operations = new List<IBulkOperation>() };
foreach (var id in delIds)
{
bulkDel.Operations.Add(new BulkDeleteOperation<User>(id));
}
var resultDel = client.Bulk(bulkDel);
//更新記錄
var client = Client();
//更新單條記錄
IUpdateRequest<User, User> request = new UpdateRequest<User, User>(new DocumentPath<User>(2))
{
Doc = new User()
{
Name = "胡三刀",
Selfment = "test4update........"
}
};
var resp = client.Update<User, User>(request);
//更新多條記錄
var insIds = new List<int>() { 4, 5 };
var bulkUpdate = new BulkRequest() { Operations = new List<IBulkOperation>() };
foreach (var id in insIds)
{
var operation = new BulkUpdateOperation<User, object>(id);
operation.Doc = new User{ Name = "胡一刀" };
bulkUpdate.Operations.Add(operation);
}
var result = client.Bulk(bulkUpdate);
//查詢
//查詢一條數據
var modUser = client.Get<User>(3);
var tweet = JsonConvert.SerializeObject(modUser.Source);
//查詢多條數據
var modList = client.Search<User>( s => s
.From(0)
.Size(10)
.Query(q =>
q.Term(t => t.Webclass, "www.b.com")
|| q.Match(mq => mq.Field(f => f.Selfment).Query("中國"))
)
);
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