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é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 }

查询:

 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 }
 
 
分类: .NetLucene
标签: solrluceneEasyNet

EasyNet.Solr 4.4.0发布及例子的更多相关文章

  1. CoreWCF 1.0.0 发布,微软正式支持WCF

    2022年4月28日,我们达到了一个重要的里程碑,并发布了CoreWCF的1.0.0版本.对Matt Connew (微软WCF团队成员)来说,这是5年前即 2017年1月开始的漫长旅程的结束.Mat ...

  2. Visual Studio Code 1.0发布,支持中文在内9种语言

    Visual Studio Code 1.0发布,支持中文在内的9种语言:Simplified Chinese, Traditional Chinese, French, German, Italia ...

  3. Apache Flume 1.7.0 发布,日志服务器

    Apache Flume 1.7.0 发布了,Flume 是一个分布式.可靠和高可用的服务,用于收集.聚合以及移动大量日志数据,使用一个简单灵活的架构,就流数据模型.这是一个可靠.容错的服务. 本次更 ...

  4. Percona Server 5.6.33-79.0 发布

    Percona Server 5.6.33-79.0 发布了,该版本基于 MySQL 5.6.33,包含了所有的 bug 修复,是Percona Server 5.6 系列中的正式版本.该版本主要是修 ...

  5. solr&lucene3.6.0源码解析(四)

    本文要描述的是solr的查询插件,该查询插件目的用于生成Lucene的查询Query,类似于查询条件表达式,与solr查询插件相关UML类图如下: 如果我们强行将上面的类图纳入某种设计模式语言的话,本 ...

  6. solr&lucene3.6.0源码解析(三)

    solr索引操作(包括新增 更新 删除 提交 合并等)相关UML图如下 从上面的类图我们可以发现,其中体现了工厂方法模式及责任链模式的运用 UpdateRequestProcessor相当于责任链模式 ...

  7. Rubinius 2.0 发布,Ruby 虚拟机

    Rubinius 2.0 发布了,官方发行说明请看这里. Rubinius是一个运行Ruby程序的虚拟机,其带有Ruby的核心库. Rubinius的设计决定了其调试功能的强大,使得在运行时常规的Ru ...

  8. Restful.Data v2.0发布,谢谢你们的支持和鼓励

    v1.0发布后,承蒙各位博友们的热心关注,也给我不少意见和建议,在此我真诚的感谢 @冰麟轻武 等朋友,你们的支持和鼓励,是这个开源项目最大的推动力. v2.0在除了细枝末节外,在功能上主要做了一下更新 ...

  9. 网页动物园2.0发布,经过几个月的努力,采用JAVA编写!

    网页动物园2.0发布,经过几个月的努力,采用JAVA编写! 网页动物园2.0 正式发布!游戏发布 游戏名称: 网页动物园插件 游戏来源: 原创插件 适用版本: Discuz! X1.5 - X3.5 ...

随机推荐

  1. Swift-开发 # 1.2版本迁移

    { Parallels: 可以将一个win程序拖到mac中运行. } --类似于虚拟机 遇到的几大问题: 1.自动修改无效? --忽略它的存在,坑. 2.无止境的修改,还是错? --使用替换工具-&g ...

  2. 换行符以及for循环的优化

    string str = ""; for (int i = 0; i < _errlistCusEmailInfo.Count; i++)                   ...

  3. API帮助页面

    ASP.NET Web API 2:创建API帮助页面        当你新建了一个web API服务之后,再建一个API帮助页面是很有好处的,这样其他开发人员就会很清楚地知道如何调用你的API接口. ...

  4. Ubuntu 14.04 编译newLISP 10.6.0

    1. 确保安装了gcc4.8.2 2. 安装须要的库: apt-get install libreadline6 libreadline6-dev 3. 下载并解压newLISP源码,这个不多说了,去 ...

  5. mysql分表分库

    单库单表 单库单表是最常见的数据库设计,例如,有一张用户(user)表放在数据库db中,所有的用户都可以在db库中的user表中查到. 单库多表 随着用户数量的增加,user表的数据量会越来越大,当数 ...

  6. MySQL的备份与还原

    原文:MySQL的备份与还原 MySQL备份和还原,都是利用mysqldump.mysql和source命令来完成的. 1.Win32下MySQL的备份与还原 1.1 备份 开始菜单 | 运行 | c ...

  7. MFC 界面编程 可参考资料

    http://www.codeproject.com/Articles/26887/A-user-draw-button-that-supports-PNG-files-with-tr http:// ...

  8. VS2015预览

    VS2015预览版体验   .NET开源了,JAVA颤抖吧...据说VS2015可以开发android,ios,wp应用程序了,还可以开发能运行在mac,linux上的ASP.NET网站,如果真是这样 ...

  9. 探秘IntelliJ IDEA v13的应用服务器

    原文:探秘IntelliJ IDEA v13的应用服务器 IntelliJ IDEA v13应用out-of-the-box支持众多企业级和开源的服务器,包括:GlassFish.WebLogic. ...

  10. DDD分层架构之仓储

    DDD分层架构之仓储(层超类型基础篇) 前一篇介绍了仓储的基本概念,并谈了我对仓储的一些认识,本文将实现仓储的基本功能. 仓储代表聚合在内存中的集合,所以仓储的接口需要模拟得像一个集合.仓储中有很多操 ...