通过Http接口及SolrNet 两种方法基于Solr5.5.1 实现CURD
前言
老规矩,任何技术的入门我通常都会总结增删改查,本文我就通过HttpWebRequest和SolrNet的方式实现Solr最基础的增删改查(CURD)。对于自己的完整项目,同时不想过于依赖第三方类库的则通过Http接口的方式来调用Solr。 当然也有人喜欢调用第三方的类库,简单方便,不需要自己处理太多繁琐的细节就可以轻松调用solr来实现自己的业务逻辑。
Http接口实现
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
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,0,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, 0, 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标识主见和普通字段。
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
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, 可以实现增删改查了。
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
private static void Delete()
{
ISolrOperations<Book1> solr = ServiceLocator.Current.GetInstance<ISolrOperations<Book1>>();
Book1 b=new Book1();
b.Id =2;
solr.Delete(b);
solr.Commit();
}
private static void Index()
{
ISolrOperations<Book1> solr = ServiceLocator.Current.GetInstance<ISolrOperations<Book1>>();
Book1 b = new Book1();
b.Id = 2;
b.Title = "平凡的世界";
b.Author = "路遥";
solr.Add(b);
Book1 b1 = new Book1();
b1.Id = 3;
b1.Title = "围城";
b1.Author = "钱钟书";
solr.Add(b1);
Book1 b2 = new Book1();
b2.Id = 4;
b2.Title = "我要去打魔兽啊";
b2.Author = "张剑";
solr.Add(b2);
Book1 b3 = new Book1();
b3.Id = 5;
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 需要用关键字查询但不需要在查询结果中显示。