指尖上的电商---(9).net开发Solr中的Facet功能

 上一节中我们演示了在SolrAdmin中使用Facet功能来进行分组统计,这一节我们看看怎样使用.NET开发Solr中的Facet功能。在讲Facet功能的同时,

我们看下.Net中怎样使用Solr查询。使用的客户端工具是easysorl.net,大家可以去codeplex下载。这个工具很好用。

            看如下图,下图就是我们要演示的功能

                

            1.模糊查询

                                    模糊查询就是搜索指定的汉字得到一个结果。下面的示例就是查询商品名称中包含白色的所有商品,最终得到的结果如下图

              

                                          代码

                                              

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public void Query()  
  2. {  
  3.   
  4.     if (string.IsNullOrWhiteSpace(textBox1.Text.Trim()))  
  5.     {  
  6.         #region 查询全部  
  7.           
  8.         var result = operations.Query("collection1""/select", SolrQuery.All, null);  
  9.         var header = binaryResponseHeaderParser.Parse(result);  
  10.         var examples = binaryQueryResultsParser.Parse(result);  
  11.         this.dataGridView1.DataSource = examples.ToList();  
  12.  
  13.         #endregion  
  14.     }  
  15.     else  
  16.     {  
  17.         #region 按商品名模糊查询  
  18.   
  19.         ISolrQuery solrQuery = new SolrQuery(textBox1.Text.Trim());  
  20.         var result = operations.Query("collection1""/select", solrQuery, null);  
  21.         var header = binaryResponseHeaderParser.Parse(result);  
  22.         var examples = binaryQueryResultsParser.Parse(result);  
  23.         this.dataGridView1.DataSource = examples.ToList();  
  24.  
  25.         #endregion  
  26.          
  27.     }  
  28. }  


 

            2.精确查询

                                在查询的时候,有时候我们要根据商品的ID或者商品的编码精确的查询到某一个商品。下面的例子就演示了按商品编码精确查询的功能。

               

 

             

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. if (string.IsNullOrWhiteSpace(textBox2.Text.Trim()))  
  2.          {  
  3.              return;   
  4.          }  
  5.          string conditon = "ProductCode:" + textBox2.Text.Trim();  
  6.          ISolrQuery solrQuery = new SolrQuery(conditon);  
  7.          var result = operations.Query("collection1""/select", solrQuery, null);  
  8.          var header = binaryResponseHeaderParser.Parse(result);  
  9.          var examples = binaryQueryResultsParser.Parse(result);  
  10.          this.dataGridView1.DataSource = examples.ToList();  


 

 

 

            3.Facet分组统计

                在查询的时候,有的时候,我们需要对查询的结果进行分组,比如想知道包含这个商品的每个分类有多少商品,每个价格区间有多少商品。

           下面的例子统计每个分类有多少商品。

                          

             

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /// <summary>  
  2.        /// facet按类型查询  
  3.        /// </summary>  
  4.        /// <param name="sender"></param>  
  5.        /// <param name="e"></param>  
  6.        private void button3_Click(object sender, EventArgs e)  
  7.        {  
  8.            label3.Visible = true;  
  9.   
  10.               var  dic=new Dictionary<string,ICollection<string>>();  
  11.               dic["facet"] = new string[] { "true" };  
  12.   
  13.               var options = new List<string>();  
  14.               options.Add("CategoryName");  
  15.               dic["facet.field"] = options;  
  16.            var result = operations.Query("collection1""/select", SolrQuery.All,dic);  
  17.            var header = binaryResponseHeaderParser.Parse(result);  
  18.            var examples = binaryQueryResultsParser.Parse(result);  
  19.   
  20.            //分组List<FacetField>  
  21.            IDictionary<string, IList<FacetField>> facetDic=new BinaryFacetFieldsParser().Parse(result);  
  22.            string strFacet = "";  
  23.            foreach (var item in facetDic)  
  24.            {  
  25.                strFacet +="分组字段:"+item.Key+"\r\n";  
  26.                foreach (var facetItem in item.Value)  
  27.                {  
  28.                    strFacet += facetItem.Name + "(" + facetItem.Count.ToString() + ")" + "---";  
  29.                }  
  30.            }  
  31.            label3.Text = strFacet;  
  32.            this.dataGridView1.DataSource = examples.ToList();  
  33.        }  

 
           Demo下载: http://download.csdn.net/detail/zx13525079024/7385945

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1.    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值