1 public class Example
2 {
3 public string Id { get; set; }
4 public string Name { get; set; }
5 public IList Features { get; set; }
6 public DateTime ModifiedDateTime { get; set; }
7 } 定义ExampleObjectDeserialize用于反序列化:
View Code
1 public class ExampleObjectDeserialize : IObjectDeserialize
2 {
3 public IEnumerable Deserialize(SolrDocumentList result)
4 {
5 var examples = new List();
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)doc["features"]
15 });
16 }
17
18 return examples;
19 }
20 } 处理xml格式返回数据:
View Code
1 var codeFactory = new TextCodecFactory();
2 var con = new SolrConnection("http://localhost:8088/solr") { CodecFactory = codeFactory };
3 var objectDeserialize = new ExampleObjectDeserialize();
4 var qop = new SolrQueryOperations(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 //解析返回头信息
16 var xmlResponseHeaderParser = new XmlResponseHeaderParser();
17
18 var responseHeader = xmlResponseHeaderParser.Parser(response);
19
20 //解析高亮
21 var xmlHighlightingParser = new XmlHighlightingParser();
22
23 var highlighting = xmlHighlightingParser.Parser(response);
24
25 //解析查询结果
26 var xmlQueryResultsParser = new XmlQueryResultsParser(objectDeserialize);
27
28 var examples = xmlQueryResultsParser.Parser(response); 处理json格式返回数据:
View Code
1 var codeFactory = new TextCodecFactory();
2 var con = new SolrConnection("http://localhost:8088/solr") { CodecFactory = codeFactory };
3 var objectDeserialize = new ExampleObjectDeserialize();
4 var qop = new SolrQueryOperations(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 //解析头信息
16 var jsonResponseHeaderParser = new JsonResponseHeaderParser();
17
18 var responseHeader = jsonResponseHeaderParser.Parser(response);
19
20 //解析高亮
21 var jsonHighlightingParser = new JsonHighlightingParser();
22
23 var highlighting = jsonHighlightingParser.Parser(response);
24
25 //解析查询结果
26 var jsonQueryResultsParser = new JsonQueryResultsParser(objectDeserialize);
27
28 var examples = jsonQueryResultsParser.Parser(response); 从上面示例可以看出,EasyNet.Solr可以很方便的处理xml、json格式数据。