Elasticsearch使用系列-ES增刪查改基本操作+ik分詞
Elasticsearch使用系列-基本查詢和聚合查詢+sql外掛
Elasticsearch使用系列-.NET6對接Elasticsearch
先安裝nuget包
Install Package NEST
1.建立索引
先建一個Model用於對映索引
/// <summary> /// 商品資訊 /// </summary> public class OrderInfo { public string Id { get; set; } public DateTime CreateTime { get; set; } public string Name { get; set; } public string GoodsName { get; set; } public string Status { get; set; } }
1.1自動轉換ES型別
/// <summary> /// ES操作 /// </summary> public class ESService { //建立連線client private static ElasticClient _client = new ElasticClient(new Uri("http://192.168.101.13:9200")); //建立索引mapping public void Mapping() { //建立索引,自動轉換型別,order為索引名稱 _client.Indices.Create("order", c => c .Map<OrderInfo>(m => m .AutoMap() )); } }
得到order的mapping的為
"order" : { "mappings" : { "properties" : { "createTime" : { "type" : "date" }, "goodsName" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "id" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "name" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "status" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } } } }
1.2通過特性指定欄位的ES型別
/// <summary> /// 商品資訊 /// </summary> public class OrderInfo { [Keyword(Name="Id")] public string Id { get; set; } [Date(Name = "CreateTime")] public DateTime CreateTime { get; set; } [keyword] public string Name { get; set; } [Text] public string GoodsName { get; set; } public string Status { get; set; } }
通過給欄位標特性指定欄位轉換成的ES型別,型別有:Text,Keyword,Number,Boolean,Date等
2.插入資料
2.1.插入單條資料
/// <summary> /// 插入單條 /// </summary> public void Insert() { var order = new OrderInfo() { Id = Guid.NewGuid().ToString(), CreateTime = DateTime.Now, Name = "張三", GoodsName = "手機P50", Status = "購物車" }; var indexResponse = _client.Index(order, i => i.Index("order")); if(!indexResponse.IsValid) { //插入失敗處理 } }
2.2批量插入資料
/// <summary> /// 插入多條 /// </summary> public void InsertList() { var orders = new List<OrderInfo>(); for(int i=1;i<=10;i++) { orders.Add(new OrderInfo() { Id = Guid.NewGuid().ToString(), CreateTime = DateTime.Now, Name = "王五" + i, GoodsName="冰箱"+i, Status="待付款" }); } var bulkIndexResponse = _client.Bulk(b => b .Index("order") .IndexMany(orders)); if(!bulkIndexResponse.IsValid) { //失敗處理 } }
3.資料查詢
3.1查詢全部資料
/// <summary> /// 查詢全部 /// </summary> public void SearchAll() { QueryContainer query = new QueryContainer(); query = new MatchAllQuery(); //查詢全部 var searchResponse = _client.Search<OrderInfo>(s => s .Index("order") .Query(q=>query) ); //獲取查詢資料 List<OrderInfo> datas =searchResponse.Documents.ToList(); }
3.2分頁查詢
/// <summary> /// 分頁查詢 /// </summary> public void SearchPage() { QueryContainer query = new QueryContainer(); query = new MatchAllQuery(); //查詢全部 int pageIndex = 1; int pageSize = 5; var searchResponse = _client.Search<OrderInfo>(s => s .Index("order") .Query(q => query) .From((pageIndex-1)*pageSize) //從第幾條索引開始 .Size(pageSize) //返回多少條 ); //獲取查詢資料 List<OrderInfo> datas = searchResponse.Documents.ToList(); }
3.3遊標查詢Scroll
為了避免深度分頁效能問題,ES預設From&Size的查詢只能查詢到10000條之前的資料,要查詢10000條之後的資料,需要用到遊標查詢
第一次查詢產生scrollId,後面的查詢只需要scrollId就可以記住原來的條件和查詢位置,往後滾動查詢
/// <summary> /// 遊標查詢 /// </summary> public void SearchScroll() { MatchAllQuery query = new MatchAllQuery();//查詢全部 int pageSize = 5; var searchResponse = _client.Search<OrderInfo>(s => s .Index("order") .Query(q => query) .Size(pageSize) //一次返回多少條 .Scroll("10s") //scrollId有效時間 ); //獲取查詢資料 List<OrderInfo> datas = searchResponse.Documents.ToList(); string scrollId = searchResponse.ScrollId;//遊標Id //後面的查詢只需要用scrollId查詢 var searchResponse2 = _client.Scroll<OrderInfo>("10s", scrollId); List<OrderInfo> datas2 = searchResponse2.Documents.ToList(); }
3.4條件查詢
/// <summary> /// 條件查詢 /// </summary> public void SearchWhere() { //查詢name=王五 var searchResponse = _client.Search<OrderInfo>(s => s .Index("order") .Query(q => q.Term(q=>q.Name, "王五1")) ); //獲取查詢資料 List<OrderInfo> datas = searchResponse.Documents.ToList(); }
3.5條件or查詢
/// <summary> /// 條件or查詢 /// </summary> public void SearchOr() { //查詢 name='王五1'or name='王五二' var searchResponse = _client.Search<OrderInfo>(s => s .Index("order") .Query(q => q .Term(o=>o.Name,"王五1")||q .Term(o=>o.Name,"王五2")) ); //獲取查詢資料 List<OrderInfo> datas = searchResponse.Documents.ToList(); }
3.6條件And查詢
/// <summary> /// 條件and查詢 /// </summary> public void SearchAnd() { //查詢 name='王五1'or status='待付款' var searchResponse = _client.Search<OrderInfo>(s => s .Index("order") .Query(q => q .Term(o => o.Name, "王五1") && q .Term("status.keyword", "待付款")) //因為status是text+keyword型別,查詢欄位要加上".keyword" ); //獲取查詢資料 List<OrderInfo> datas = searchResponse.Documents.ToList(); }
3.7聚合統計查詢
/// <summary> /// 聚合查詢統計 /// </summary> public void SearchAggs() { //求員工年齡平均值和最大年齡 var searchResponse = _client.Search<object>(s => s .Index("employee") .Size(0) //不返回源資料 .Aggregations(aggs=>aggs .Average("avgage",avg=>avg.Field("age")) .Max("maxage",max=>max.Field("age")) )); var datas = searchResponse.Aggregations ; }
3.8聚合分組查詢
/// <summary> /// 聚合分組查詢 /// </summary> public void SearchAggsGroup() { //求員工年齡平均值和最大年齡 var searchResponse = _client.Search<object>(s => s .Index("employee") .Size(0) //不返回源資料 .Aggregations(aggs => aggs .Terms("jobgroup", group => group.Field("job")) )); var datas = searchResponse.Aggregations; }
4.sql語句查詢
需要安裝sql外掛,參照前一篇文章,這裡通過rest api向ES發起sql語句的查詢
建一個引數類和HttpHelper
public class QueryParam { public string query { get; set; } } public class HttpHelper { private static HttpClient _httpClient = new HttpClient(); public static string Post(QueryParam param, string url) { HttpContent content = new StringContent(JsonConvert.SerializeObject(param)); content.Headers.ContentType= new MediaTypeHeaderValue("application/json"); string result = _httpClient.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result; content.Dispose(); return result; } }
向ES發起sql語句查詢
/// <summary> /// sql語句查詢 /// </summary> public void SearchSql() { QueryParam queryParam = new QueryParam(); queryParam.query = "select * from employee where job='java'"; string url = "http://192.168.101.13:9200/_xpack/sql?format=csv"; var result = HttpHelper.Post(queryParam, url); Console.WriteLine(result); }
這裡查詢返回的格式為csv格式,資料需要自己解析成物件
更多用法參考NEST官網:https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/index.html