elsticsearch中安裝了x-pack後,查詢時就須要用戶名和密碼了。
無帳號密碼,不可訪問
curl http://192.168.0.2:9200/testindex/_count?pretty=true
Authentication Requiredhtml
訪問basic認證的頁面
(1)經過user選項帶上帳號密碼,返回正常數據
curl –user elastic:changeme http://192.168.0.2:9200/testindex/_count?pretty=truenode
(2)在url中添加用戶名和密碼來訪問:
http://elastic:changeme@192.168.0.2:9200/testindex/_count?pretty=truegit
(3)在請求頭中添加Authorization來訪問:
Authorization: 「Basic 用戶名和密碼的base64加密字符串」github
//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#查詢組件web
官方組件Elasticsearch.Net & NESTapi
NEST手冊app
--
//創建映射類 [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.htmlcurl
其餘組件
plainElastic.netelasticsearch
參考:ide
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