在上一边随笔EasyNet.Solr架构中,写道了EasyNet.Solr具有良好的扩展性,内置了对xml、json文本格式返回数据的处理。步骤如下:
1.定义ICodecFactory,由于xml、json都是基于文本的,所以我们使用TextCodecFactory。
2.创建指定ICodecFactory的Solr连接ISolrConnection。
3. 创建ISolrQueryOperations<T>用于Solr查询,T为返回结构数据类型,此处应该为string类型。
4.预先定义好的IObjectDeserialize<T>,对象反序列化接口实现。T为要反序列化为的对象数据类型。
5.构造查询字符串及查询选项。
6.进行查询,返回结果。
7.对于查询结果,可以用各种实现ISolrResponseParser<ST, DT>接口的解析器进行解析。ST为原数据类型,此处为stirng类型,DT为解析结果的数据类型。
下面的例子,演示了如何用EasyNet.Solr处理xml或者json格式返回数据。
定义Example 实体类:
View Code
1
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 }
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 }
定义ExampleObjectDeserialize用于反序列化:
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 }
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 }
处理xml格式返回数据:
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 // 解析返回头信息
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 < Example > (objectDeserialize);
27
28 var examples = xmlQueryResultsParser.Parser(response);
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 // 解析返回头信息
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 < Example > (objectDeserialize);
27
28 var examples = xmlQueryResultsParser.Parser(response);
处理json格式返回数据:
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 // 解析头信息
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 < Example > (objectDeserialize);
27
28 var examples = jsonQueryResultsParser.Parser(response);
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 // 解析头信息
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 < Example > (objectDeserialize);
27
28 var examples = jsonQueryResultsParser.Parser(response);
从上面示例可以看出,EasyNet.Solr可以很方便的处理xml、json格式数据。