前言 老規矩,任何技術的入門我通常都會總結增刪改查,本文我就通過HttpWebRequest和SolrNet的方式實現Solr最基礎的增刪改查(CURD)。對於自己的完整項目,同時不想過於依賴第三方類庫的則通過Http介面的方式來調用Solr。 當然也有人喜歡調用第三方的類庫,簡單方便,不需要自己處 ...
前言
老規矩,任何技術的入門我通常都會總結增刪改查,本文我就通過HttpWebRequest和SolrNet的方式實現Solr最基礎的增刪改查(CURD)。對於自己的完整項目,同時不想過於依賴第三方類庫的則通過Http介面的方式來調用Solr。 當然也有人喜歡調用第三方的類庫,簡單方便,不需要自己處理太多繁瑣的細節就可以輕鬆調用solr來實現自己的業務邏輯。
Http介面實現
private static void Query() { string url = "http://localhost:8080/solr/univeral/select?indent=on&q=title:魔獸&wt=json"; WebRequest request = WebRequest.Create(url); request.Method = "GET"; WebResponse response = request.GetResponse(); Stream stream = response.GetResponseStream(); using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { string strJson = reader.ReadToEnd(); Console.WriteLine(strJson); } } private static void Index() { string url = "http://localhost:8080/solr/univeral/update?_=1466592530544&wt=json&commit=true"; string strData = @"<add> <doc> <field name='id'>2</field> <field name='title'>平凡的世界(根據路遙同名小說改變)</field> <field name='author'>路遙</field> </doc> </add>"; byte[] bytes; bytes = System.Text.Encoding.UTF8.GetBytes(strData); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Accept = "*/*"; request.ContentLength = bytes.Length; request.ContentType = "text/xml; encoding='utf-8'"; Stream requestStream = request.GetRequestStream(); requestStream.Write(bytes,0,bytes.Length); requestStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { Stream responseStream = response.GetResponseStream(); string strJson = new StreamReader(responseStream).ReadToEnd(); Console.WriteLine(strJson); } } private static void Delete() { string url = "http://localhost:8080/solr/univeral/update?_=1466592530544&wt=json&commit=true"; string strData = @"<delete><id>2</id></delete>"; byte[] bytes; bytes = System.Text.Encoding.ASCII.GetBytes(strData); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Accept = "*/*"; request.ContentLength = bytes.Length; request.ContentType = "text/xml; encoding='utf-8'"; Stream requestStream = request.GetRequestStream(); requestStream.Write(bytes, 0, bytes.Length); requestStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { Stream responseStream = response.GetResponseStream(); string strJson = new StreamReader(responseStream).ReadToEnd(); Console.WriteLine(strJson); } }View Code
SolrNet實現
SolrNet最大的優勢就是一直在更新,用戶眾多,並且在github上提供源代碼(https://github.com/mausch/SolrNet)和說明文檔(https://github.com/mausch/SolrNet/tree/master/Documentation)。
使用前先創建對應的model,欄位和schema.xml中的欄位對應,並使用屬性SolrUniqueKey和SolrField標識主見和普通欄位。
public class Book1 { [SolrUniqueKey("id")] public int Id { get; set; } [SolrField("title")] public string Title { get; set; } [SolrField("author")] public string Author { get; set; } }View Code
主程式調用前先初始化:Startup.Init<Book1>("http://localhost:8080/solr/univeral/"); OK, 可以實現增刪改查了。
private static void Delete() { ISolrOperations<Book1> solr = ServiceLocator.Current.GetInstance<ISolrOperations<Book1>>(); Book1 b=new Book1(); b.Id =2; solr.Delete(b); solr.Commit(); } private static void Index() { ISolrOperations<Book1> solr = ServiceLocator.Current.GetInstance<ISolrOperations<Book1>>(); Book1 b = new Book1(); b.Id = 2; b.Title = "平凡的世界"; b.Author = "路遙"; solr.Add(b); Book1 b1 = new Book1(); b1.Id = 3; b1.Title = "圍城"; b1.Author = "錢鐘書"; solr.Add(b1); Book1 b2 = new Book1(); b2.Id = 4; b2.Title = "我要去打魔獸啊"; b2.Author = "張劍"; solr.Add(b2); Book1 b3 = new Book1(); b3.Id = 5; b3.Title = "魔獸世界"; b3.Author = "張三"; solr.Add(b3); solr.Commit(); } private static void Query() { ISolrOperations<Book1> solr = ServiceLocator.Current.GetInstance<ISolrOperations<Book1>>(); SolrQueryResults<Book1> solrResults = solr.Query(new SolrQuery("title:魔獸")); foreach (var solrQueryResult in solrResults) { Console.WriteLine("Id:" + solrQueryResult.Id+",Name:"+solrQueryResult.Title+",Author:"+solrQueryResult.Author); } Console.ReadLine(); }View Code
SolrNet提供了SolrQuery的多個版本有興趣的話可以看看它的源碼,包括容器如何初始化,容器內部如何使用SolrConnection。也是一個不錯的框架,值得推薦。
是否索引(indexed)、是否存儲(stored)
你在配置schema.xml時註意到field的這幾個欄位了麽? 我第一次配置時並沒有關註這幾個屬性,只是覺的有些好奇,都是什麼情況下需要設置這幾個屬性呢?這才專門查了它們幾個之間的區別:
- indexed=true stored=true 需要用關鍵查詢並需要在查詢結果中顯示。 如book.title、book.author
- indexed=false stored=true 不需要用關鍵字查詢,但需要在查詢結果中顯示。 如book.destinationUrl
- indexed=true stored=false 需要用關鍵字查詢但不需要在查詢結果中顯示。
參考資料
http://www.cnblogs.com/zhangweizhong/p/5073997.html