• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

高性能solr c#客户端EasyNet.Solr

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

EasyNet.Solr(http://easynet.codeplex.com)是由本人开发的 solr(http://lucene.apache.org/solr)c#客户端。它具有以下特性:

 

1.支持solr 3.1(不兼容solr 1.4.x)

2. 默认支持solr最高效的javabin协议

3.基于接口的序列化和反序列化协议,没有采用反射

4. 可以在架构基础上方便扩展处理solr支持的其他协议,如xml,json等等

 

 以下是基本的使用示例:


索引和检索用到的实体类 Indexing and retrieval of entity class used

public class Example
{
      public string Id { get; set; }
      public string Name { get; set; }
}

创建索引 Create index
序列化实现 Implement serialization

public class ExampleObjectSerializer : IObjectSerializer<Example>
{
      public IList<SolrInputDocument> Serializer(IEnumerable<Example> objs)
      {
          IList<SolrInputDocument> docs = new List<SolrInputDocument>();

          foreach (Example obj in objs)
          {
              SolrInputDocument doc = new SolrInputDocument();

              doc.Add("Id", new SolrInputField("id", obj.Id));
              doc.Add("name", new SolrInputField("name", obj.Name));

              docs.Add(doc);
           }

          return docs;
      }
}

索引 Index

ICodecFactory codecFactory = new BinaryCodecFactory();
ISolrConnection<NamedList> con = new SolrConnection<NamedList>("http://localhost:8088/solr");
IUpdateOperationParametersConvert<NamedList> updateOpParametersConvert = new BinaryUpdateOperationParametersConvert();
ISolrResponseParser<NamedList, ResponseHeader> responseHeaderParser = new BinaryResponseHeaderParser();
ISolrUpdateOperations<NamedList> updateOp = new SolrUpdateOperations<NamedList>(con, updateOpParametersConvert);

IList<Example> examples = new List<Example>();

examples.Add(new Example() { Id = "16", Name = "Terry" + DateTime.Now.ToLongTimeString() });
examples.Add(new Example() { Id = "18", Name = "Terry" + DateTime.Now.ToLongTimeString() });
examples.Add(new Example() { Id = "17", Name = "Terry" + DateTime.Now.ToLongTimeString() });

IObjectSerializer<Example> objectSerializer = new ExampleObjectSerializer();
IList<SolrInputDocument> docs = objectSerializer.Serializer(examples);

AddOptions? addOptions = new AddOptions() { CommitWithin = 10 };
CommitOptions? commitOptions = new CommitOptions() { WaitFlush = true, WaitSearcher = true };
OptimizeOptions? optimizeOptions = new OptimizeOptions() { WaitFlush = true, WaitSearcher = true };

NamedList addRes = updateOp.Add(docs, null, commitOptions, optimizeOptions);

ResponseHeader responseHeader = responseHeaderParser.Parser(addRes);

查询 Query
反序列实现 Implement deserialize

public class ExampleObjectDeserialize : IObjectDeserialize<Example>
{
      public IEnumerable<Example> Deserialize(SolrDocumentList result)
      {
          IList<Example> examples = new List<Example>();

          foreach (SolrDocument doc in result)
          {
              examples.Add(new Example() { Id = doc["id"].ToString(), Name = doc["name"].ToString() });
          }

          return examples;
      }
}

查询 Query

ISolrConnection con = new SolrConnection("http://localhost:8088/solr");
IObjectDeserialize<Example> objectDeserialize = new ExampleObjectDeserialize();
ISolrResponseParser<NamedList, QueryResults<Example>> qrp = new BinaryQueryResultsParser<Example>(objectDeserialize);
ISolrQueryOperations<NamedList> qop = new SolrQueryOperations<NamedList>(con);
NameValueCollection options = new NameValueCollection();

options.Add(CommonParams.START, "0");
options.Add(CommonParams.ROWS, "10");

NamedList res = qop.Query(SolrQuery.All, options);
QueryResults<Example> exps = qrp.Parser(res);


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# 预处理器指令发布时间:2022-07-13
下一篇:
转载生产者与消费者(C#)发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap