Easynet. solr for processing data returned in xml or json format
In easynet. solr, there are two most important interfaces: the codec interface and the solr output result parser interface isolrresponseparser <st, dt>.
Easynet. solr has two built-in codecs: javabincodec and textcodec. Javabincodec corresponds to the javabin protocol in solr and textcodec corresponds to the xml, json, and other text-based protocols in solr. Binarycodecfactory is defined to create javabincodec and textcodecfactory to create textcodec. There is a codecfactory attribute in solrconnection. This is
Public class example
2 {
3 public string id {get; set ;}
4 public string name {get; set ;}
5 public ilist <string> features {get; set ;}
6 public datetime modifieddatetime {get; set ;}
7}
Define exampleobjectdeserialize for Deserialization:
View code
1 public class exampleobjectdeserialize: iobjectdeserialize <example>
2 {
3 public ienumerable <example> deserialize (solrdocumentlist result)
4 {
5 var examples = new list <example> ();
6
7 foreach (solrdocument doc in result)
8 {
9 examples. add (new example ()
10 {
11 id = doc ["id"]. tostring (),
12 name = doc ["name"]. tostring (),
13 modifieddatetime = convert. todatetime (doc ["last_modified"]),
14 features = (ilist <string>) doc ["features"]
15 });
16}
17
18 return examples;
19}
20}
Process the returned data in xml format:
View code
1 var codefactory = new textcodecfactory ();
2 var con = new solrconnection <string> ("http: // localhost: 8088/solr") {codecfactory = codefactory };
3 var objectdeserialize = new exampleobjectdeserialize ();
4 var qop = new solrqueryoperations <string> (con );
5 var options = new namevaluecollection ();
6
7 options. add (commonparams. start, "0 ");
8 options. add (commonparams. rows, "10 ");
9 options. add (highlightparams. highlight, "true ");
10 options. add (highlightparams. fields, "name ");
11 options. add (commonparams. wt, "xml ");
12
13 var response = qop. query (new solrquery ("name: terry"), options );
14
15 // Parse the returned header information
16 var xmlresponseheaderparser = new xmlresponseheaderparser ();
17
18 var responseheader = xmlresponseheaderparser. parser (response );
19
20 // resolution highlight
21 var xmlhighlightingparser = new xmlhighlightingparser ();
22
23 var highlighting = xmlhighlightingparser. parser (response );
24
25 // Parse the query results
26 var xmlqueryresultsparser = new xmlqueryresultsparser <example> (objectdeserialize );
27
28 var examples = xmlqueryresultsparser. parser (response );
Process returned data in json format:
View code
1 var codefactory = new textcodecfactory ();
2 var con = new solrconnection <string> ("http: // localhost: 8088/solr") {codecfactory = codefactory };
3 var objectdeserialize = new exampleobjectdeserialize ();
4 var qop = new solrqueryoperations <string> (con );
5 var options = new namevaluecollection ();
6
7 options. add (commonparams. start, "0 ");
8 options. add (commonparams. rows, "10 ");
9 options. add (highlightparams. highlight, "true ");
10 options. add (highlightparams. fields, "name ");
11 options. add (commonparams. wt, "json ");
12
13 var response = qop. query (new solrquery ("name: terry"), options );
14
15 // Parse header information
16 var jsonresponseheaderparser = new jsonresponseheaderparser ();
17
18 var responseheader = jsonresponseheaderparser. parser (response );
19
20 // resolution highlight
21 var jsonhighlightingparser = new jsonhighlightingparser ();
22
23 var highlighting = jsonhighlightingparser. parser (response );
24
25 // Parse the query results
26 var jsonqueryresultsparser = new jsonqueryresultsparser <example> (objectdeserialize );
27
28 var examples = jsonqueryresultsparser. parser (response );
The above instance processing process is
1. Define icodecfactory. Because xml and json are both text-based, textcodecfactory is used.
2. Create solr connection isolrconnection for the specified icodecfactory.
3. Create isolrqueryoperations <t> for solr query. t indicates the type of the returned structure data, which must be string.
4. Pre-defined iobjectdeserialize <t>, object deserialization interface implementation. T is the data type of the object to be deserialized.
5. Construct the query string and query options.
6. Query and return results.
7. For the query results, you can use various resolvers that implement the isolrresponseparser <st, dt> interface for parsing. St is the original data type. Here it is the stirng type, and dt is the data type of the resolution result.