使用solrj和EasyNet.Solr进行原子更新

Solr 4.0(http://lucene.apache.org/solr/)已经发布了有一段时间了,其中Solr 4.0中有一个不错的特性:Atom Update,也就是原子更新。有了原子更新这个东东,我们就可以只更新某个字段。下面的代码演示了如何用solrj和EasyNet.Solr(http://easynet.codeplex.com)进行原子更新。

使用solrj进行原子更新:

 1 import org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrServer
 2 import org.apache.solr.common.SolrInputDocument
 3 
 4 object SolrTest extends App {
 5   val url = "http://localhost:8080/example/collection1"
 6   val server = new ConcurrentUpdateSolrServer(url, 100, 3)
 7 
 8   //init
 9   //atomSet
10   atomAdd
11 
12   def init = {
13     val doc = new SolrInputDocument()
14     doc.addField("id", "F8V7067-APL-KIT")
15     doc.addField("name", "Belkin Mobile Power Cord for iPod w/ Dock")
16     doc.addField("features", "car power adapter")
17     doc.addField("features", "white")
18 
19     server.add(doc)
20     server.commit()
21     server.shutdown()
22   }
23 
24   def atomSet = {
25     val doc = new SolrInputDocument()
26     doc.addField("id", "F8V7067-APL-KIT")
27 
28     val setOper = new java.util.HashMap[String, String]()
29     setOper.put("set", "iPod & iPod Mini USB 2.0 Cable")
30 
31     doc.addField("name", setOper)
32 
33     server.add(doc)
34     server.commit()
35     server.shutdown()
36   }
37 
38   def atomAdd = {
39     val doc = new SolrInputDocument()
40     doc.addField("id", "F8V7067-APL-KIT")
41 
42     val addOper = new java.util.HashMap[String, String]()
43     addOper.put("add", "add a test feature")
44 
45     doc.addField("features", addOper)
46 
47     server.add(doc)
48     server.commit()
49     server.shutdown()
50   }
51 }

 

使用EasyNet.Solr进行原子更新:

 1 using EasyNet.Solr.Commons;
 2 using EasyNet.Solr.Impl;
 3 using System;
 4 using System.Collections;
 5 using System.Collections.Generic;
 6 using System.Web.Mvc;
 7 
 8 namespace EasyNet.Solr.Website.Controllers
 9 {
10     public class IndexController : Controller
11     {
12         private OptimizeOptions optimizeOptions = new OptimizeOptions();
13         private ISolrResponseParser<NamedList, ResponseHeader> binaryResponseHeaderParser = new BinaryResponseHeaderParser();
14         private IUpdateParametersConvert<NamedList> updateParametersConvert = new BinaryUpdateParametersConvert();
15         private ISolrUpdateConnection<NamedList, NamedList> solrUpdateConnection = new SolrUpdateConnection<NamedList, NamedList>() { ServerUrl = "http://localhost:8080/example/collection1", ContentType = "application/javabin" };
16         private ISolrUpdateOperations<NamedList> updateOperations;
17 
18         public IndexController()
19         {
20             updateOperations = new SolrUpdateOperations<NamedList, NamedList>("/update", solrUpdateConnection, updateParametersConvert) { ResponseWriter = "javabin" };
21         }
22         //
23         // GET: /Index/
24 
25         public ActionResult Index()
26         {
27             //Init();
28             //AtomSet();
29             AtomAdd();
30 
31             return View();
32         }
33 
34         private void Init()
35         {
36             var docs = new List<SolrInputDocument>();
37             var doc = new SolrInputDocument();
38             doc.Add("id", new SolrInputField("id", "F8V7067-APL-KIT"));
39             doc.Add("name", new SolrInputField("name", "Belkin Mobile Power Cord for iPod w/ Dock"));
40             doc.Add("features", new SolrInputField("features", new String[] { "car power adapter", "white" }));
41 
42             docs.Add(doc);
43 
44             var result = updateOperations.Update(new UpdateOptions() { OptimizeOptions = optimizeOptions, Docs = docs });
45             var header = binaryResponseHeaderParser.Parse(result);
46 
47             Response.Write(string.Format("Update Status:{0} QTime:{1}</br>", header.Status, header.QTime));
48         }
49 
50         private void AtomSet()
51         {
52             var docs = new List<SolrInputDocument>();
53             var doc = new SolrInputDocument();
54             doc.Add("id", new SolrInputField("id", "F8V7067-APL-KIT"));
55 
56             var setOper = new Hashtable();
57             setOper.Add("set", "iPod &amp; iPod Mini USB 2.0 Cable");
58 
59             doc.Add("name", new SolrInputField("name", setOper));
60 
61             docs.Add(doc);
62 
63             var result = updateOperations.Update(new UpdateOptions() { OptimizeOptions = optimizeOptions, Docs = docs });
64             var header = binaryResponseHeaderParser.Parse(result);
65 
66             Response.Write(string.Format("Update Status:{0} QTime:{1}</br>", header.Status, header.QTime));
67         }
68 
69         private void AtomAdd()
70         {
71             var docs = new List<SolrInputDocument>();
72             var doc = new SolrInputDocument();
73             doc.Add("id", new SolrInputField("id", "F8V7067-APL-KIT"));
74 
75             var addOper = new Hashtable();
76             addOper.Add("add", "add a test feature ");
77 
78             doc.Add("features", new SolrInputField("features", addOper));
79 
80             docs.Add(doc);
81 
82             var result = updateOperations.Update(new UpdateOptions() { OptimizeOptions = optimizeOptions, Docs = docs });
83             var header = binaryResponseHeaderParser.Parse(result);
84 
85             Response.Write(string.Format("Update Status:{0} QTime:{1}</br>", header.Status, header.QTime));
86         }
87 
88     }
89 
90 }

希望对大家有所帮助,也希望大家关注和使用EasyNet.Solr,如有任何关于Solr、Lucene、EasyNet.Solr等等问题,都可以加入EasyNet开源项目QQ群:181963043。

 

posted @ 2012-12-08 23:07  TerryLiang  阅读(2620)  评论(4编辑  收藏  举报