上一篇已經講到了Solr 查詢的相關的參數。這里在講講C#是如何通過客戶端請求和接受solr服務器的數據, 這里推薦使用SolrNet,主要是:SolrNet使用非常方便,而且用戶眾多,一直都在更新,感興趣的可以加入他們的郵件群組,方便迅速了解SolrNet的最新動態。
SorlNet源碼地址:https://github.com/mausch/SolrNet
SolrNet使用說明文檔:https://github.com/mausch/SolrNet/tree/master/Documentation
一、創建一個項目控制臺程序,并引用SolrNet.dll。Demo下載
注意:SolrNet 依賴HttpWebAdapters.dll和Microsoft.PRactices.ServiceLocation.dll 這兩個dll 文件,所以,如果編譯或者測試有問題,引用這兩個dll 文件應該就ok了。
二、在solr 的schema.xml 增加相關的Filed 字段,同時創建一個實體類,與schema.xml中的Filed 字段映射。
public class Product { [SolrUniqueKey("id")] public int id { get; set; } [SolrField("name")] public string name { get; set; } [SolrField("title")] public string title { get; set; } [SolrField("category")] public string category { get; set; } [SolrField("content")] public string content { get; set; } [SolrField("price")] public double price { get; set; } [SolrField("color")] public string color { get; set; } [SolrField("updatetime")] public DateTime updatetime { get; set; } [SolrField("orderBy")] public int orderBy { get; set; } }
同時,schema.xml中也要加上相應的Filed 字段,打開solr_home/mycore1/conf 下的schema.xml文件,增加如下Field 配置,如果不知道如何操作,請參考前一篇文章,《Solr學習總結(二)Solr的安裝與配置》
<field name="id" type="int" indexed="true" stored="true" required="true" multiValued="false" /> <field name="name" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="title" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="category" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="content" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="price" type="double" indexed="true" stored="true" required="true" multiValued="false" /> <field name="color" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="orderBy" type="int" indexed="true" stored="true" required="true" multiValued="false" /> <field name="updatetime" type="date" indexed="true" stored="true" required="true" multiValued="false" />
三、開始調用solrnet:
1.初始化
Startup.Init<Product>("http://localhost:8080/solr/mycore1");
2.增加和修改索引(document)
Solr 索引的增加和修改,都是Add() 方法,solr 會自動判斷是否存在此所以,有就修改,沒有就新增。
ISolrOperations<Product> solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>(); var p = new Product() { id = 201, name = "product 201", title = "title 201", category = "201", content = "title 201 green", color = "green", price = 67.92, updatetime = DateTime.Now.AddDays(-101), orderBy = 101 }; solr.Add(p); solr.Commit();
3. 刪除索引
solrnet 重寫了多個 delete()方法。這里只介紹一個,其他的自己研究吧。
ISolrOperations<Product> solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>(); var p = new Product() { id = 201, }; solr.Delete(p); solr.Commit();
注意:調用 Add()
或是 Delete()
方法,必須在他們之后加上 Commit()
,否是請求是不會被處理的。
4.查詢
ISolrOperations<Product> solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>(); SolrQueryResults<Product> phoneTaggedArticles = solr.Query(new SolrQuery("id:1")); foreach (Product p in phoneTaggedArticles) { Console.WriteLine(string.Format("{0}: {1}", p.id, p.title)); } Console.WriteLine();
到這里,Solrnet的基本用法已經說完了,下一篇,將聊聊Solr的一些高級用法,solr 的復雜查詢,高亮,Facet分組查詢等。
新聞熱點
疑難解答