EasyNet.Solr 4.4.0发布及例子
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 }

EasyNet.Solr 4.4.0发布及例子的更多相关文章
- CoreWCF 1.0.0 发布,微软正式支持WCF
2022年4月28日,我们达到了一个重要的里程碑,并发布了CoreWCF的1.0.0版本.对Matt Connew (微软WCF团队成员)来说,这是5年前即 2017年1月开始的漫长旅程的结束.Mat ...
- Visual Studio Code 1.0发布,支持中文在内9种语言
Visual Studio Code 1.0发布,支持中文在内的9种语言:Simplified Chinese, Traditional Chinese, French, German, Italia ...
- Apache Flume 1.7.0 发布,日志服务器
Apache Flume 1.7.0 发布了,Flume 是一个分布式.可靠和高可用的服务,用于收集.聚合以及移动大量日志数据,使用一个简单灵活的架构,就流数据模型.这是一个可靠.容错的服务. 本次更 ...
- Percona Server 5.6.33-79.0 发布
Percona Server 5.6.33-79.0 发布了,该版本基于 MySQL 5.6.33,包含了所有的 bug 修复,是Percona Server 5.6 系列中的正式版本.该版本主要是修 ...
- solr&lucene3.6.0源码解析(四)
本文要描述的是solr的查询插件,该查询插件目的用于生成Lucene的查询Query,类似于查询条件表达式,与solr查询插件相关UML类图如下: 如果我们强行将上面的类图纳入某种设计模式语言的话,本 ...
- solr&lucene3.6.0源码解析(三)
solr索引操作(包括新增 更新 删除 提交 合并等)相关UML图如下 从上面的类图我们可以发现,其中体现了工厂方法模式及责任链模式的运用 UpdateRequestProcessor相当于责任链模式 ...
- Rubinius 2.0 发布,Ruby 虚拟机
Rubinius 2.0 发布了,官方发行说明请看这里. Rubinius是一个运行Ruby程序的虚拟机,其带有Ruby的核心库. Rubinius的设计决定了其调试功能的强大,使得在运行时常规的Ru ...
- Restful.Data v2.0发布,谢谢你们的支持和鼓励
v1.0发布后,承蒙各位博友们的热心关注,也给我不少意见和建议,在此我真诚的感谢 @冰麟轻武 等朋友,你们的支持和鼓励,是这个开源项目最大的推动力. v2.0在除了细枝末节外,在功能上主要做了一下更新 ...
- 网页动物园2.0发布,经过几个月的努力,采用JAVA编写!
网页动物园2.0发布,经过几个月的努力,采用JAVA编写! 网页动物园2.0 正式发布!游戏发布 游戏名称: 网页动物园插件 游戏来源: 原创插件 适用版本: Discuz! X1.5 - X3.5 ...
随机推荐
- lua for通过循环table一些差异
有两个著名的是:ipairs和pairs,双方都认为,我们都非常熟悉的.其中ipairs刮(idx=1)从明年序遍历,经验nil那退出循环:和pairs遍历,仅仅要里面有值都能够遍历的到. 那假如我须 ...
- Flex 日志管理
在Flex中调试方法有两种: 一是用trace()函数,在flex builder中进行调试: 二是用logTarget类,例如以下代码: // Create a target. var logTar ...
- 持续交付工具ThoughtWorks Go部署step by step
持续交付工具ThoughtWorks Go部署step by step http://blogs.360.cn/360cloud/2014/05/13/%E6%8C%81%E7%BB%AD%E4%BA ...
- 解决Crystal Report XI R2不能在64操作系统正常工作的问题-web程序
原文:[原创]解决Crystal Report XI R2不能在64操作系统正常工作的问题-web程序 我更换了新的电脑,操作系统也从原来32位的windows 2003 R2升级到windows 2 ...
- ZOJ 2724 Windows 消息队列 (优先队列)
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2724 Message queue is the basic fund ...
- Windows Phone 8.1 多媒体(1):相片
原文:Windows Phone 8.1 多媒体(1):相片 Windows Phone 8.1 多媒体(1):相片 Windows Phone 8.1 多媒体(2):视频 Windows Phone ...
- Swift编程语言学习12 ——实例方法(Instance Methods)和类型方法(Type Methods)
方法是与某些特定类型相关联的函数.类.结构体.枚举都能够定义实例方法:实例方法为给定类型的实例封装了详细的任务与功能.类.结构体.枚举也能够定义类型方法:类型方法与类型本身相关联.类型方法与 Obje ...
- MVC验证07-自定义Model级别验证
原文:MVC验证07-自定义Model级别验证 在一般的自定义验证特性中,我们通过继承ValidationAttribute,实现IClientValidatable,只能完成对某个属性的自定义验证. ...
- MVC验证02-自定义验证规则、邮件验证
原文:MVC验证02-自定义验证规则.邮件验证 本文体验MVC自定义验证特性,来实现对邮件的验证.对于刚写完的自定义验证特性,起初只能支持后端验证.如果要让前端jquery支持,还必须对jquery的 ...
- iptables的配置文件/etc/sysconfig/iptables不存在怎么办
iptables的配置文件/etc/sysconfig/iptables不存在怎么办 首先要看一下iptables是否安装了,使用service iptables status或yum info ip ...