zoukankan      html  css  js  c++  java
  • 高性能solr c#客户端EasyNet.Solr

    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);

  • 相关阅读:
    CSS 透明度 设置 兼容IE FF
    Hibernate学习---第五节:普通组件和动态组件
    Ant学习---第五节:Ant_Junit介绍(基于3的版本)
    Ant学习---第四节:Ant属性的介绍
    Ant学习---第三节:使用Ant实现一个最小的项目编译
    Ant学习---第二节:Ant添加文件夹和文件夹集的使用
    Ant学习---第一节:Ant安装和简单使用
    JPA学习---第十二节:JPA中的联合主键
    JPA学习---第十一节:JPA中的多对多双向关联实体定义与注解设置及操作
    JPA学习---第十节:JPA中的一对一双向关联
  • 原文地址:https://www.cnblogs.com/TerryLiang/p/2018962.html
Copyright © 2011-2022 走看看