最近因?yàn)轫?xiàng)目需要使用搜索引擎,因此嘗試使用.Net去操作elasticsearch,把使用過程記錄如下: 1.安裝elasticsearch 2.安裝elasticsearch插件 3.使用NEST操作elasticsearch
elasticsearch下文使用簡稱ES,ES已經(jīng)更新到了6.*,經(jīng)常使用的應(yīng)該是2.*和5.*,其中5.*當(dāng)然對2.*更新了許多功能,但是在初學(xué)者最直觀的改變是關(guān)聯(lián)插件的版本,2.*關(guān)聯(lián)插件的版本號基本上是亂的,需要去插件對應(yīng)的網(wǎng)站上查詢,但是5.*對應(yīng)的插件版本基本上和ES本身的版本號一致,這就避免了為找相應(yīng)插件而做的很多無謂的工作。
1.安裝elasticsearch 安裝之前請先安裝Jdk,我使用的是1.8版本,安裝完后配置環(huán)境變量,安裝過程略過。 我這里使用的是5.6.0版本。下載路徑如下 https://www./downloads/past-releases/elasticsearch-5-6-0 下載完成后,將下載文件直接解壓到安裝目錄下即可。 安裝完成后,就是啟動ES在bin目錄下可以運(yùn)行elasticsearch.bat在命令行界面啟動(命令行不能關(guān)閉),也可以運(yùn)行elasticsearch-service.bat啟動ES的服務(wù)進(jìn)程,這樣就不用每次開機(jī)啟動了。 配置文件在config/elasticsearch.yml中,每次修改完配置需要服務(wù)重啟,切記切記。
啟動成功后,打開瀏覽器,輸入默認(rèn)的url和端口 http://localhost:9200/ 如果啟動成功,則會出現(xiàn)如下頁面,說明安裝成功了 2.安裝elasticsearch插件 ES有許多插件用來幫助我們使用,其中我覺得最主要的有兩個插件 1)esheader 可以使用在web頁面上訪問esheader的集群數(shù)據(jù) 2) analysis-ik 中文的分詞插件,默認(rèn)的分詞功能在搜索時只能將中文分成一個一個的單字
esheader的安裝 esheader在2.*版本上是可以直接在命令行中進(jìn)行安裝的,但是5.*版本上不能這樣做了,網(wǎng)上采用的辦法很麻煩,我找到一種比較簡單的辦法。 其實(shí)說到底esheader就是在web頁面上向es服務(wù)器發(fā)送http請求,并獲取結(jié)果進(jìn)行展示,其實(shí)esheader實(shí)質(zhì)上就是一個java web網(wǎng)站,因此只需要將它部署到tomcat服務(wù)器上,運(yùn)行即可。 ip和端口號,需要和安裝ES配置的相一致,而且需要將ES的配置文件修改為允許跨域訪問即在config/elasticsearch.yml最后追加如下配置,然后重啟服務(wù) http.cors.enabled: true http.cors.allow-origin: "*" 然后訪問tomcat上對應(yīng)的網(wǎng)頁即可,如下圖。說明esheader安裝成功了??! analysis-ik 的安裝 ik可以直接在命令行下面安裝(前提是知道ik文件的url) 在ES的bin目錄下打開控制臺運(yùn)行 ,即可 .\elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.6.0/elasticsearch-analysis-ik-5.6.0.zip 安裝完成后ES的plugins目錄中會添加對應(yīng)插件的目錄
3.使用NEST訪問ES 新建一個.net控制臺項(xiàng)目,打開NuGet管理器,搜索NEST,然后安裝,如圖。我安裝的是5.6.0版本,不知道其余版本是否OK,感興趣的朋友可以試試 安裝完成后,就可以編寫代碼了 ![]() private static void AddData() { //1.通過es服務(wù)器 localhost:9200 來定義es client var node = new Uri("http://localhost:9200"); var indexName = "products"; var settings = new ConnectionSettings(node).DefaultIndex(indexName); var elastic = new ElasticClient(settings); //es服務(wù)器健康檢查 var res = elastic.ClusterHealth(); Console.WriteLine(res.Status); //2.創(chuàng)建索引esbot //if (!elastic.IndexExists(indexName).Exists) //{ // var createIndexResponse = elastic.CreateIndex(indexName); // var mappingBlogPost = elastic.Map<Account>(s => s.AutoMap()); //} //var mappingBlogPost = elastic.Map<Account>(s => s.AutoMap()); //List<Account> accounts = new List<Account>() { new Account { Id = 2, Name = "aaa", Password = "bbb" }, new Account { Id = 3, Name = "ccc", Password = "ddd" } }; List<Product> duodians = new TestEntities().duodian.ToList().Select(a => a.ToProduct()).ToList(); elastic.IndexMany(duodians, indexName); } private static void SimpleSelect() { //1.通過es服務(wù)器 localhost:9200 來定義es client var node = new Uri("http://localhost:9200"); var indexName = "products"; var settings = new ConnectionSettings(node).DefaultIndex(indexName); var elastic = new ElasticClient(settings); //5. 簡單搜索 //var searchResult = elastic.Search<Product>(sr => sr.Query(q => q.MatchAll()).From(0).Size(100)); //var searchResult = elastic.Search<Product>(sr => sr.Query(q => q.QueryString(qs=>qs.Query("蘇泊爾")))); var searchResult = elastic.Search<Product>(sr => sr.Query(q => q.Match(qm => qm.Field(f => f.title).Query("給你省小號搟面杖")))); return; }View Code
![]() [ElasticsearchType(Name = "Product", IdProperty = "GuidId")] public class Product { [Keyword(Name = "GuidId", Index = true)] public string GuidId { get; set; } = Guid.NewGuid().ToString(); [Number(NumberType.Long, Name = "Id")] public int id { get; set; } [Text(Name = "Title", Index = true, Analyzer = "ik_max_word")] public string title { get; set; } [Keyword(Name = "Img", Index = false)] public string img { get; set; } [Keyword(Name = "Lunfanimg", Index = false)] public string lunfanimg { get; set; } [Keyword(Name = "Spec", Index = false)] public string spec { get; set; } [Keyword(Name = "Xcimg", Index = false)] public string xcimg { get; set; } [Keyword(Name = "Skuid", Index = false)] public string skuid { get; set; } [Keyword(Name = "Pimg", Index = false)] public string pimg { get; set; } [Keyword(Name = "Plunfanimg", Index = false)] public string plunfanimg { get; set; } [Keyword(Name = "Pxcimg", Index = false)] public string pxcimg { get; set; } [Keyword(Name = "Categoryid", Index = false)] public string categoryid { get; set; } [Keyword(Name = "Price", Index = false)] public string price { get; set; } [Keyword(Name = "Brandname", Index = false)] public string brandname { get; set; } [Keyword(Name = "Categoryname", Index = false)] public string categoryname { get; set; } [Keyword(Name = "Created", Index = false)] public System.DateTime created { get; set; } } public static class duodianExtension { public static Product ToProduct(this duodian duodian) { return new Product { brandname = duodian.brandname, categoryid = duodian.categoryid, id = duodian.id, img = duodian.img, lunfanimg = duodian.lunfanimg, pimg = duodian.pimg, plunfanimg = duodian.plunfanimg, price = duodian.price, pxcimg = duodian.pxcimg, skuid = duodian.skuid, spec = duodian.spec, title = duodian.title, xcimg = duodian.xcimg, created = duodian.created, categoryname = duodian.categoryname }; } }View Code |
|