国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Solr學習總結(五)SolrNet的基本用法及CURD

2019-11-14 13:39:43
字體:
來源:轉載
供稿:網友

  上一篇已經講到了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分組查詢等。

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 彝良县| 汤阴县| 开远市| 专栏| 丰顺县| 信宜市| 灵武市| 万年县| 伊宁市| 亚东县| 海安县| 绍兴县| 江华| 广元市| 遵义市| 林芝县| 伊宁县| 宜州市| 昭通市| 随州市| 长海县| 乐昌市| 平谷区| 阿鲁科尔沁旗| 康乐县| 东兴市| 贡山| 香格里拉县| 沐川县| 山丹县| 达孜县| 丽水市| 安庆市| 吴川市| 从化市| 五台县| 保德县| 马边| 阳曲县| 闽清县| 西安市|