前言

老规矩,任何技术的入门我通常都会总结增删改查,本文我就通过HttpWebRequest和SolrNet的方式实现Solr最基础的增删改查(CURD)。对于自己的完整项目,同时不想过于依赖第三方类库的则通过Http接口的方式来调用Solr。 当然也有人喜欢调用第三方的类库,简单方便,不需要自己处理太多繁琐的细节就可以轻松调用solr来实现自己的业务逻辑。

Http接口实现

private static void Query()
{ string url = "http://localhost:8080/solr/univeral/select?indent=on&q=title:魔兽&wt=json"; WebRequest request = WebRequest.Create(url);
request.Method = "GET";
WebResponse response = request.GetResponse(); Stream stream = response.GetResponseStream();
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
string strJson = reader.ReadToEnd();
Console.WriteLine(strJson);
}
} private static void Index()
{
string url = "http://localhost:8080/solr/univeral/update?_=1466592530544&wt=json&commit=true"; string strData = @"<add>
<doc>
<field name='id'>2</field>
<field name='title'>平凡的世界(根据路遥同名小说改变)</field>
<field name='author'>路遥</field>
</doc>
</add>"; byte[] bytes;
bytes = System.Text.Encoding.UTF8.GetBytes(strData); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Accept = "*/*";
request.ContentLength = bytes.Length;
request.ContentType = "text/xml; encoding='utf-8'"; Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes,,bytes.Length);
requestStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
string strJson = new StreamReader(responseStream).ReadToEnd();
Console.WriteLine(strJson);
} } private static void Delete()
{
string url = "http://localhost:8080/solr/univeral/update?_=1466592530544&wt=json&commit=true"; string strData = @"<delete><id>2</id></delete>"; byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(strData); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Accept = "*/*";
request.ContentLength = bytes.Length;
request.ContentType = "text/xml; encoding='utf-8'"; Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, , bytes.Length);
requestStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
string strJson = new StreamReader(responseStream).ReadToEnd();
Console.WriteLine(strJson);
} }

SolrNet实现

SolrNet最大的优势就是一直在更新,用户众多,并且在github上提供源代码(https://github.com/mausch/SolrNet)和说明文档(https://github.com/mausch/SolrNet/tree/master/Documentation)。

使用前先创建对应的model,字段和schema.xml中的字段对应,并使用属性SolrUniqueKey和SolrField标识主见和普通字段。

public class Book1
{
[SolrUniqueKey("id")]
public int Id { get; set; }
[SolrField("title")]
public string Title { get; set; }
[SolrField("author")]
public string Author { get; set; } }

主程序调用前先初始化:Startup.Init<Book1>("http://localhost:8080/solr/univeral/");  OK, 可以实现增删改查了。

private static void Delete()
{
ISolrOperations<Book1> solr = ServiceLocator.Current.GetInstance<ISolrOperations<Book1>>(); Book1 b=new Book1();
b.Id =; solr.Delete(b);
solr.Commit();
} private static void Index()
{
ISolrOperations<Book1> solr = ServiceLocator.Current.GetInstance<ISolrOperations<Book1>>(); Book1 b = new Book1();
b.Id = ;
b.Title = "平凡的世界";
b.Author = "路遥"; solr.Add(b); Book1 b1 = new Book1();
b1.Id = ;
b1.Title = "围城";
b1.Author = "钱钟书"; solr.Add(b1); Book1 b2 = new Book1();
b2.Id = ;
b2.Title = "我要去打魔兽啊";
b2.Author = "张剑"; solr.Add(b2); Book1 b3 = new Book1();
b3.Id = ;
b3.Title = "魔兽世界";
b3.Author = "张三"; solr.Add(b3); solr.Commit();
} private static void Query()
{
ISolrOperations<Book1> solr = ServiceLocator.Current.GetInstance<ISolrOperations<Book1>>(); SolrQueryResults<Book1> solrResults = solr.Query(new SolrQuery("title:魔兽")); foreach (var solrQueryResult in solrResults)
{
Console.WriteLine("Id:" + solrQueryResult.Id+",Name:"+solrQueryResult.Title+",Author:"+solrQueryResult.Author);
}
Console.ReadLine();
}

SolrNet提供了SolrQuery的多个版本有兴趣的话可以看看它的源码,包括容器如何初始化,容器内部如何使用SolrConnection。也是一个不错的框架,值得推荐。

是否索引(indexed)、是否存储(stored)

你在配置schema.xml时注意到field的这几个字段了么? 我第一次配置时并没有关注这几个属性,只是觉的有些好奇,都是什么情况下需要设置这几个属性呢?这才专门查了它们几个之间的区别:

  • indexed=true  stored=true 需要用关键查询并需要在查询结果中显示。 如book.title、book.author
  • indexed=false stored=true 不需要用关键字查询,但需要在查询结果中显示。 如book.destinationUrl
  • indexed=true stored=false  需要用关键字查询但不需要在查询结果中显示

参考资料

http://www.cnblogs.com/zhangweizhong/p/5073997.html

通过Http接口及SolrNet 两种方法基于Solr5.5.1 实现CURD的更多相关文章

  1. 微信网页开发之获取用户unionID的两种方法--基于微信的多点登录用户识别

    假设网站A有以下功能需求:1,pc端微信扫码登录:2,微信浏览器中的静默登录功能需求,这两种需求就需要用到用户的unionID,这样才能在多个登录点(终端)识别用户.那么这两种需求下用户的unionI ...

  2. C# web api返回类型设置为json的两种方法

    web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面就介绍两种方法: 方法一:(改配置法) 找到Global.asax文件,在Applic ...

  3. Intent传递对象的两种方法(Serializable,Parcelable) (转)

    今天讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcela ...

  4. Intent传递对象的两种方法

    Android为intent提供了两种传递对象参数类型的方法 分别需要使实体类实现Serializable接口.Parcelable接口 首先我们要知道,传递对象,需要先将对象序列化 一.那么为什么要 ...

  5. Android中Intent传递对象的两种方法(Serializable,Parcelable)

    今天要给大家讲一下Android中 Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是 Bundle.putP ...

  6. Loadrunner 接口测试的两种方法

    其实无论用那种测试方法,接口测试的原理是通过测试程序模拟客户端向服务器发送请求报文,服务器接收请求报文后对相应的报文做出处理然后再把应答报文发送给客户端,客户端接收应答报文这一个过程. 方法一.用Lo ...

  7. 用Java集合中的Collections.sort方法对list排序的两种方法

    用Collections.sort方法对list排序有两种方法第一种是list中的对象实现Comparable接口,如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  8. AE 将地图导出为图片的两种方法

    在ArcGIS的开发中,我们经常需要将当前地图打印(或是转出)到图片文件中.将Map或Layout中的图象转出有两种方法,一种为通过IActiveView的OutPut函数,另外一种是通过IExpor ...

  9. HibernateTemplate、HibernateDaoSupport两种方法实现增删改查Good(转)

    Spring+Hibernate两种方法实现增删改查 首先,定义一个Customer的bean类,设置好Customer.hbm.xml文件.再定义好一个Dao接口.准备好一个jdbc.propert ...

随机推荐

  1. H5单页面手势滑屏切换原理

    H5单页面手势滑屏切换是采用HTML5 触摸事件(Touch) 和 CSS3动画(Transform,Transition)来实现的,效果图如下所示,本文简单说一下其实现原理和主要思路. 1.实现原理 ...

  2. 事务日志已满,原因为“ACTIVE_TRANSACTION”

    汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 异常处理汇总-数据库系列  http://www.cnblogs.com/dunitia ...

  3. Visual Studio:error MSB8020(搬运)

    状况如下: error MSB8020: The builds tools for v120 (Platform Toolset = 'v120') cannot be found. To build ...

  4. [原创]ubuntu16.04LTS使用细节

    如何给自己安装的应用创建桌面图标 拿php开发神器phpstorm为例,找到可执行文件所在路径. 这里是/home/haive/PhpStorm/bin/phpstorm.sh 打开dash,搜索&q ...

  5. 怎样在Dos里切换盘符

    一:在Dos里切换盘符 a:在电脑左下角右击显示图片;(我用的是win10系统,其他系统类似) b:点击运行,输入cmd; c:点击确定: d:输入盘符:(如f:) 或F: 只写字母,不写分号是不行的 ...

  6. OpenGL: 纹理采样 texture sample

    Sampler (GLSL) Sampler通常是在Fragment shader(片元着色器)内定义的,这是一个uniform类型的变量,即处理不同的片元时这个变量是一致不变的.一个sampler和 ...

  7. 《Walking the callstack(转载)》

    本文转载自:https://www.codeproject.com/articles/11132/walking-the-callstack Download demo project with so ...

  8. 搭建TFS 2015 Build Agent环境(一)

    Download the build agent Downloading the build agent is really simple. Navigate to your TFS control ...

  9. 技术笔记:Delphi多线程应用读写锁

    在多线程应用中锁是一个很简单又很复杂的技术,之所以要用到锁是因为在多进程/线程环境下,一段代码可能会被同时访问到,如果这段代码涉及到了共享资源(数据)就需要保证数据的正确性.也就是所谓的线程安全.之前 ...

  10. 2000条你应知的WPF小姿势 基础篇<63-68 Triggers和WPF类逻辑结构>

    在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000ThingsYou Should Know About C# 和 2,00 ...