SorlNet
Solr学习
上一篇已经讲到了Solr 查询的相关的参数。这里在讲讲C#是如何通过客户端请求和接受solr服务器的数据, 这里推荐使用SolrNet,主要是:SolrNet使用非常方便,而且用户众多,一直都在更新,感兴趣的可以加入他们的邮件群组,方便迅速了解SolrNet的最新动态。
SorlNet源码地址:https://github.com/mausch/SolrNet
SolrNet使用说明文档:https://github.com/mausch/SolrNet/tree/master/Documentation
一、创建一个项目控制台程序,并引用SolrNet.dll。Demo下载

注意:SolrNet 依赖HttpWebAdapters.dll和Microsoft.Practices.ServiceLocation.dll 这两个dll 文件,所以,如果编译或者测试有问题,引用这两个dll 文件应该就ok了。
二、在solr 的schema.xml 增加相关的Filed 字段,同时创建一个实体类,与schema.xml中的Filed 字段映射。

public class Product
{
[SolrUniqueKey("id")]
public int id { get; set; } [SolrField("name")]
public string name { get; set; } [SolrField("title")]
public string title { get; set; } [SolrField("category")]
public string category { get; set; } [SolrField("content")]
public string content { get; set; } [SolrField("price")]
public double price { get; set; } [SolrField("color")]
public string color { get; set; } [SolrField("updatetime")]
public DateTime updatetime { get; set; } [SolrField("orderBy")]
public int orderBy { get; set; }
}

同时,schema.xml中也要加上相应的Filed 字段,打开solr_home\mycore1\conf 下的schema.xml文件,增加如下Field 配置,如果不知道如何操作,请参考前一篇文章,《Solr学习总结(二)Solr的安装与配置》

<field name="id" type="int" indexed="true" stored="true" required="true" multiValued="false" />
<field name="name" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="title" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="category" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="content" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="price" type="double" indexed="true" stored="true" required="true" multiValued="false" />
<field name="color" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="orderBy" type="int" indexed="true" stored="true" required="true" multiValued="false" />
<field name="updatetime" type="date" indexed="true" stored="true" required="true" multiValued="false" />

三、开始调用solrnet:
1.初始化
Startup.Init<Product>("http://localhost:8080/solr/mycore1");
2.增加和修改索引(document)
Solr 索引的增加和修改,都是Add() 方法,solr 会自动判断是否存在此所以,有就修改,没有就新增。

ISolrOperations<Product> solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
var p = new Product()
{
id = 201,
name = "product 201",
title = "title 201",
category = "201",
content = "title 201 green",
color = "green",
price = 67.92,
updatetime = DateTime.Now.AddDays(-101),
orderBy = 101
};
solr.Add(p);
solr.Commit();

3. 删除索引
solrnet 重写了多个 delete()方法。这里只介绍一个,其他的自己研究吧。

ISolrOperations<Product> solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
var p = new Product()
{
id = 201,
};
solr.Delete(p);
solr.Commit();

注意:调用 Add() 或是 Delete()方法,必须在他们之后加上 Commit(),否是请求是不会被处理的。
4.查询

ISolrOperations<Product> solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
SolrQueryResults<Product> phoneTaggedArticles = solr.Query(new SolrQuery("id:1"));
foreach (Product p in phoneTaggedArticles)
{
Console.WriteLine(string.Format("{0}: {1}", p.id, p.title));
}
Console.WriteLine();

到这里,Solrnet的基本用法已经说完了,下一篇,将聊聊Solr的一些高级用法,solr 的复杂查询,高亮,Facet分组查询等。
SorlNet的更多相关文章
- Solr学习总结(五)SolrNet的基本用法及CURD
上一篇已经讲到了Solr 查询的相关的参数.这里在讲讲C#是如何通过客户端请求和接受solr服务器的数据, 这里推荐使用SolrNet,主要是:SolrNet使用非常方便,而且用户众多,一直都在更新, ...
- Open Source
资源来源于http://www.cnblogs.com/Leo_wl/category/246424.html RabbitMQ 安装与使用 摘要: RabbitMQ 安装与使用 前言 吃多了拉就是队 ...
随机推荐
- 不起眼的 z-index 却能牵扯出这么大的学问(转)
z-index在日常开发中算是一个比较常用的样式,一般理解就是设置标签在z轴先后顺序,z-index值大的显示在最前面,小的则会被遮挡,是的,z-index的实际作用就是这样. 但是你真的了解z-in ...
- [置顶] github简单使用
git的介绍可以看这里 http://zh.wikipedia.org/wiki/GitHub 安装和使用参考的这个 http://www.cnblogs.com/cocowool/arch ...
- redhat linux使用Centos yum源
redhat Linux如果是没有购买红帽许可的话是不能使用redhat的yum源的,但是可以通过修改使之能使用Centos的yum源. 步骤一:删除redhat的yum [root@localhos ...
- 实现TextView 文字排版,分散两端对齐
參考:http://www.cnblogs.com/lcyty/p/3265335.html 方法一:使用HTML TextView textview=(TextView)findViewbyId(R ...
- codeforces#256DIV2 D题Multiplication Table
题目地址:http://codeforces.com/contest/448/problem/D 当时是依照找规律做的,规律倒是找出来了,可是非常麻烦非常麻烦. . 看到前几名的红名爷们3分钟就过了, ...
- SE 2014年4月16日
一. 描述BGP路由协议中 BGP路由携带 AS-PATH/ next-hop / ORIGIN / local-preference 属性的特点! BGP协议中的AS-PATH是AS列表,用来 ...
- JAVA 读取图片储存至本地
需求:serlvet经过处理通过报表工具返回一张报表图(柱状图 折线图). 现在需要把这个图存储到本地 以便随时查看 // 构造URL URL url = new URL(endStr); // 打开 ...
- (转)SWT的CheckBoxTreeViewer的相关用法
最近在项目中需要用到遍历某个目录下所有文件,并按照树形结构展示,同时还需要提供对树形菜单的选择展开等操作.在eclipse中提供了CheckboxTreeViewer组件来满足所需要的功能 下面是需要 ...
- 关于http接口开发中json格式数据编码问题处理
关于http接口开发中json格式数据编码问题处理 在实际工作中,接口很多时候返回json格式,但有时返回的格式会有编码问题 假设如下接口:http://service.test.com/interf ...
- maven项目建立pom.xml报无法解析org.apache.maven.plugins:maven-resources-plugin:2.4.3
一.发现问题 建立maven项目后,pom.xml在显示红叉.鼠标放上去,显示Executiondefault-testResources of goalorg.apache.maven.plugin ...