這是ElasticSearch 2.4 版本系列的第四篇:html
在ElasticSearch引擎中進行全文搜索是一件很是酷炫的事,而建立索引是最重要的事,必需要精心設計,建議使用head插件建立索引的映射(Mapping),而對索引文檔數據的平常更新,可使用C#客戶端程序,按照計劃自動進行數據的同步和更新。node
對於一個數據庫開發,好久沒有寫過C#代碼,dot net菜鳥一個,本文簡單分享了使用ElasticSearch的.net客戶端驅動程序向索引中添加文檔的代碼片斷,詳細信息,請參考官方手冊:Elasticsearch.Net and NEST: the .NET clients [5.x] » Introduction正則表達式
一,ElasticSearch的.net客戶端驅動程序數據庫
ElasticSearch官方網站提供了兩個.net客戶端驅動程序,其中Elasticsearch.Net是一個很是底層且靈活的客戶端驅動程序,用戶須要手動建立請求(Request)和響應(Response);而NEST是一個高層的客戶端,其內部使用的依然是Elasticsearch.Net驅動程序,NEST擁有查詢DSL(領域特定語言),可以映射全部請求和響應對象,使用起來比較方便。不一樣版本的NEST驅動程序,其提供的接口變化很大,在熟悉Nest以後,可使用Elasticsearch.Net驅動程序來編寫本身的代碼,免受更新之苦。api
首先,下載ElastiSearch的.net客戶端驅動程序,打開VS的工具(Tools)菜單,經過NuGet包管理器控制檯,輸入命令安裝NEST:數組
PM> Install-Package NEST
安裝以後,系統引用三個DLL文件,樓主安裝的驅動程序版本分別是:服務器
二,NEST驅動程序的簡單使用app
1,鏈接到ElasticSearch引擎服務器elasticsearch
注意,默認索引的名稱必須小寫,建議將索引名,文檔類型名稱,和字段名稱都小寫。ide
using Nest; public static class Setting { public static string strConnectionString=@"http://localhost:9200"; public static Uri Node { get { return new Uri(strConnectionString); } } public static ConnectionSettings ConnectionSettings { get { return new ConnectionSettings(Node).DefaultIndex("default"); } } }
2,建立數據模型
注意,模型的字段名和建立的索引映射中的字段保持一致,推薦都使用小寫字母。Nest驅動程序提供了模型屬性,讀者能夠自行嘗試。
public class MeetupEvents { public long eventid { get; set; } public string orignalid { get; set; } public string eventname { get; set; } public string description { get; set; } }
3,更新文檔
NEST提供兩種更新文檔的方式,逐條更新和批量更新,函數PopulateIndex用於逐條更新索引,函數BulkPopulateIndex用於批量更新索引;
注意,在更新索引時,高亮顯示的代碼指定了索引的元字段_id爲meetupevent實體的主鍵eventid;
using Nest; public class ESProvider { public static ElasticClient client = new ElasticClient(Setting.ConnectionSettings); public static string strIndexName = @"meetup".ToLower(); public static string strDocType = "events".ToLower(); public bool PopulateIndex(MeetupEvents meetupevent) { var index = client.Index(meetupevent,i=>i.Index(strIndexName).Type(strDocType).Id(meetupevent.eventid)); return index.Created; } public bool BulkPopulateIndex(List<MeetupEvents> posts) { var bulkRequest = new BulkRequest(strIndexName,strDocType) { Operations = new List<IBulkOperation>() }; var idxops = posts.Select(o => new BulkIndexOperation<MeetupEvents>(o) { Id=o.eventid}).Cast<IBulkOperation>().ToList(); bulkRequest.Operations = idxops; var response = client.Bulk(bulkRequest); return response.IsValid; } }
4,執行批量更新操做
若是更新的數據量十分龐大,建議,首先對數據源分頁,分batch更新ElasticSearch的索引。ElasticSearch在批量索引文檔時,若是一批文檔數量過大,會致使數據丟失,建議每次索引1000個文檔。
ESProvider es = new ESProvider(); List<MeetupEvents> pbs = new List<MeetupEvents>(); foreach (DataRow dr in MeetupEventsTable.Rows) { MeetupEvents pb = new MeetupEvents(); pb.eventid = long.Parse(dr["EventID"].ToString()); pb.orignalid = dr["OriginalID"].ToString(); pb.eventname = dr["EventName"].ToString(); pb.description = dr["Description"].ToString(); pbs.Add(pb); } es.BulkPopulateIndex(pbs);
5,總結NEST驅動程序的使用
使用如下三段代碼鏈接NEST客戶端:
var node = new Uri("http://myserver:9200"); var settings = new ConnectionSettings(node).DefaultIndex("default"); var client = new ElasticClient(settings);
使用客戶端的Index方法更新/添加單個文檔:
Client.Index(student);
使用客戶端的IndexMany函數更新/添加多個文檔:
var list = new List<Student>(); client.IndexMany<Student>(list);
使用客戶端的Bulk方法批量更新文檔,須要根據實體列表List構造一個BulkRequest參數:
client.Bulk(bulkRequest);
參考文檔: