EasyNet.Solr 4.4.0釋出及例子

TerryLiang發表於2013-08-29

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     }

 

相關文章