EasyNet.Solr 4.4.0发布及例子

EasyNet.Solr 4.4.0已经发布,可以直接从http://easynet.codeplex.com/ 下载试用并反馈。最新版本进行了以下改动:

1.根据Solr的变动,更新时依据ContentType来确定提交的数据类型(XML、Javabin、Json等等)。

2.ISolrUpdateOperations、ISolrQueryOperations接口添加了collection参数。

3.只维护基于Javabin协议的实现,其他基于XML、Json等等可以自行实现。

4.SolrQueryConnection采用POST方式,以支持长查询。

EasyNet.Solr简单易用,扩展性强,一般不会因为Solr的版本更新,变更业务代码。

以下是基于Solr 4.4.0发布版本中的例子:

初始化:

 1         static OptimizeOptions optimizeOptions = new OptimizeOptions();
 2         static ISolrResponseParser<NamedList, ResponseHeader> binaryResponseHeaderParser = new BinaryResponseHeaderParser();
 3         static IUpdateParametersConvert<NamedList> updateParametersConvert = new BinaryUpdateParametersConvert();
 4         static ISolrUpdateConnection<NamedList, NamedList> solrUpdateConnection = new SolrUpdateConnection<NamedList, NamedList>() { ServerUrl = "http://localhost:8983/solr/" };
 5         static ISolrUpdateOperations<NamedList> updateOperations = new SolrUpdateOperations<NamedList, NamedList>(solrUpdateConnection, updateParametersConvert) { ResponseWriter = "javabin" };
 6 
 7         static ISolrQueryConnection<NamedList> connection = new SolrQueryConnection<NamedList>() { ServerUrl = "http://localhost:8983/solr/" };
 8         static ISolrQueryOperations<NamedList> operations = new SolrQueryOperations<NamedList>(connection) { ResponseWriter = "javabin" };
 9 
10         static IObjectDeserializer<Example> exampleDeserializer = new ExampleDeserializer();
11         static ISolrResponseParser<NamedList, QueryResults<Example>> binaryQueryResultsParser = new BinaryQueryResultsParser<Example>(exampleDeserializer);

创建索引:

 1         /// <summary>
 2         /// 创建索引
 3         /// </summary>
 4         static void Index()
 5         {
 6             var docs = new List<SolrInputDocument>();
 7             var doc = new SolrInputDocument();
 8             doc.Add("id", new SolrInputField("id", "SOLR1000"));
 9             doc.Add("name", new SolrInputField("name", "Solr, the Enterprise Search Server"));
10             doc.Add("features", new SolrInputField("features", new String[] { "Advanced Full-Text Search Capabilities using Lucene", "Optimized for High Volume Web Traffic", "Standards Based Open Interfaces - XML and HTTP", "Comprehensive HTML Administration Interfaces", "Scalability - Efficient Replication to other Solr Search Servers", "Flexible and Adaptable with XML configuration and Schema", "Good unicode support: h&#xE9;llo (hello with an accent over the e)" }));
11             doc.Add("price", new SolrInputField("price", 0.0f));
12             doc.Add("popularity", new SolrInputField("popularity", 10));
13             doc.Add("inStock", new SolrInputField("inStock", true));
14             doc.Add("incubationdate_dt", new SolrInputField("incubationdate_dt", new DateTime(2006, 1, 17, 0, 0, 0, DateTimeKind.Utc)));
15 
16             docs.Add(doc);
17 
18             var result = updateOperations.Update("collection1", "/update", new UpdateOptions() { OptimizeOptions = optimizeOptions, Docs = docs });
19             var header = binaryResponseHeaderParser.Parse(result);
20 
21             System.Console.WriteLine(string.Format("Update Status:{0} QTime:{1}", header.Status, header.QTime));
22             System.Console.ReadLine();
23         }

Set原子操作:

 1         /// <summary>
 2         /// Set原子操作
 3         /// </summary>
 4         static void AtomSet()
 5         {
 6             var docs = new List<SolrInputDocument>();
 7             var doc = new SolrInputDocument();
 8             doc.Add("id", new SolrInputField("id", "SOLR1000"));
 9 
10             var setOper = new Hashtable();
11             setOper.Add("set", "Solr");
12 
13             doc.Add("name", new SolrInputField("name", setOper));
14 
15             docs.Add(doc);
16 
17             var result = updateOperations.Update("collection1", "/update", new UpdateOptions() { OptimizeOptions = optimizeOptions, Docs = docs });
18             var header = binaryResponseHeaderParser.Parse(result);
19 
20             System.Console.WriteLine(string.Format("Update Status:{0} QTime:{1}", header.Status, header.QTime));
21             System.Console.ReadLine();
22         }

Add原子操作:

 1         /// <summary>
 2         /// Add原则操作
 3         /// </summary>
 4         static void AtomAdd()
 5         {
 6             var docs = new List<SolrInputDocument>();
 7             var doc = new SolrInputDocument();
 8             doc.Add("id", new SolrInputField("id", "SOLR1000"));
 9 
10             var addOper = new Hashtable();
11             addOper.Add("add", "add a test feature ");
12 
13             doc.Add("features", new SolrInputField("features", addOper));
14 
15             docs.Add(doc);
16 
17             var result = updateOperations.Update("collection1", "/update", new UpdateOptions() { OptimizeOptions = optimizeOptions, Docs = docs });
18             var header = binaryResponseHeaderParser.Parse(result);
19 
20             System.Console.WriteLine(string.Format("Update Status:{0} QTime:{1}", header.Status, header.QTime));
21             System.Console.ReadLine();
22         }

根据id删除:

 1         /// <summary>
 2         /// 根据id删除
 3         /// </summary>
 4         static void Delete()
 5         {
 6             var result = updateOperations.Update("collection1", "/update", new UpdateOptions() { OptimizeOptions = optimizeOptions, DelById = new string[] { "SOLR1000" } });
 7             var header = binaryResponseHeaderParser.Parse(result);
 8 
 9             System.Console.WriteLine(string.Format("Update Status:{0} QTime:{1}", header.Status, header.QTime));
10             System.Console.ReadLine();
11         }

 

查询:

 1         /// <summary>
 2         /// 查询
 3         /// </summary>
 4         static void Query()
 5         {
 6             var result = operations.Query("collection1", "/select", SolrQuery.All, null);
 7             var header = binaryResponseHeaderParser.Parse(result);
 8 
 9             var examples = binaryQueryResultsParser.Parse(result);
10 
11             System.Console.WriteLine(string.Format("Query Status:{0} QTime:{1} Total:{2}", header.Status, header.QTime, examples.NumFound));
12             System.Console.ReadLine();
13         }

实体类及反序列器:

 1     /// <summary>
 2     /// 实体类
 3     /// </summary>
 4     class Example
 5     {
 6         public string Id { get; set; }
 7 
 8         public string Name { get; set; }
 9 
10         public string[] Features { get; set; }
11 
12         public float Price { get; set; }
13 
14         public int Popularity { get; set; }
15 
16         public bool InStock { get; set; }
17 
18         public DateTime IncubationDate { get; set; }
19     }
20 
21     /// <summary>
22     /// 反序列化器
23     /// </summary>
24     class ExampleDeserializer : IObjectDeserializer<Example>
25     {
26         public IEnumerable<Example> Deserialize(SolrDocumentList result)
27         {
28             foreach (SolrDocument doc in result)
29             {
30                 yield return new Example()
31                 {
32                     Id = doc["id"].ToString(),
33                     Name = doc["name"].ToString(),
34                     Features = (string[])((ArrayList)doc["features"]).ToArray(typeof(string)),
35                     Price = (float)doc["price"],
36                     Popularity = (int)doc["popularity"],
37                     InStock = (bool)doc["inStock"],
38                     IncubationDate = (DateTime)doc["incubationdate_dt"]
39                 };
40             }
41         }
42     }

 

posted @ 2013-08-29 09:26  TerryLiang  阅读(3335)  评论(12编辑  收藏