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
{ public IList Serializer(IEnumerable objs) { IList docs = new List(); 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 con = new SolrConnection("http://localhost:8088/solr");
IUpdateOperationParametersConvert updateOpParametersConvert = new BinaryUpdateOperationParametersConvert();
ISolrResponseParser responseHeaderParser = new BinaryResponseHeaderParser();
ISolrUpdateOperations updateOp = new SolrUpdateOperations(con, updateOpParametersConvert); IList examples = new List(); 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 objectSerializer = new ExampleObjectSerializer();
IList 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
{ public IEnumerable Deserialize(SolrDocumentList result) { IList examples = new List(); 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 objectDeserialize = new ExampleObjectDeserialize();
ISolrResponseParser> qrp = new BinaryQueryResultsParser(objectDeserialize);
ISolrQueryOperations qop = new SolrQueryOperations(con);
NameValueCollection options = new NameValueCollection(); options.Add(CommonParams.START, "0");
options.Add(CommonParams.ROWS, "10"); NamedList res = qop.Query(SolrQuery.All, options);
QueryResults exps = qrp.Parser(res);
延伸阅读
最新评论