Easynet. SOLR (http://easynet.codeplex.com) is a SOLR (http://lucene.apache.org/solr) C # client developed by myself. It has the following features:
1. Support for SOLR 3.1 (not compatible with SOLR 1.4.x)
2. By default, SOLR's most efficient javabin protocol is supported.
3. The interface-based serialization and deserialization protocols are not reflected.
4. other protocols supported by SOLR, such as XML and JSON, can be easily extended based on the architecture.
The following is a basic example:
Indexing and retrieval of entity class used
Public Class Example
{
Public String ID {Get; set ;}
Public string name {Get; set ;}
}
Create Index
Serialization for 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 = responseheaderparser. Parser (addres );
Query
Implement implement deserialize in Reverse Sequence
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 );