• 高性能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);

  • 相关阅读:
    组合数学总结
    字符串算法总结
    数据结构总结
    CDQ分治(学习笔记)
    网络流(学习笔记)
    string
    water
    mine
    洛谷 P4035 【球形空间产生器】
    洛谷 P3306 【随机数生成器】
  • 原文地址:https://www.cnblogs.com/TerryLiang/p/2018962.html
Copyright © 2020-2023  润新知