指尖上的電商---(9).net開發Solr中的Facet功能

來源:互聯網
上載者:User

標籤:solr   電商   .net   指尖上的電商 facet   

                          上一節中我們示範了在SolrAdmin中使用Facet功能來進行分組統計,這一節我們看看怎樣使用.NET開發Solr中的Facet功能。在講Facet功能的同時,

我們看下.Net中怎樣使用Solr查詢。使用的用戶端工具是easysorl.net,大家可以去codeplex下載。這個工具很好用。

            看如,就是我們要示範的功能

               

            1.模糊查詢

                                    模糊查詢就是搜尋指定的漢字得到一個結果。下面的樣本就是查詢商品名稱中包含白色的所有商品,最終得到的結果如

             

                                          代碼

                                              

        public void Query()        {            if (string.IsNullOrWhiteSpace(textBox1.Text.Trim()))            {                #region 查詢全部                                var result = operations.Query("collection1", "/select", SolrQuery.All, null);                var header = binaryResponseHeaderParser.Parse(result);                var examples = binaryQueryResultsParser.Parse(result);                this.dataGridView1.DataSource = examples.ToList();                #endregion            }            else            {                #region 按商品名模糊查詢                ISolrQuery solrQuery = new SolrQuery(textBox1.Text.Trim());                var result = operations.Query("collection1", "/select", solrQuery, null);                var header = binaryResponseHeaderParser.Parse(result);                var examples = binaryQueryResultsParser.Parse(result);                this.dataGridView1.DataSource = examples.ToList();                #endregion                           }        }


 

            2.精確查詢

                                在查詢的時候,有時候我們要根據商品的ID或者商品的編碼精確的查詢到某一個商品。下面的例子就示範了按商品編碼精確查詢的功能。

              

 

             

   if (string.IsNullOrWhiteSpace(textBox2.Text.Trim()))            {                return;             }            string conditon = "ProductCode:" + textBox2.Text.Trim();            ISolrQuery solrQuery = new SolrQuery(conditon);            var result = operations.Query("collection1", "/select", solrQuery, null);            var header = binaryResponseHeaderParser.Parse(result);            var examples = binaryQueryResultsParser.Parse(result);            this.dataGridView1.DataSource = examples.ToList();


 

 

 

            3.Facet分組統計

                在查詢的時候,有的時候,我們需要對查詢的結果進行分組,比如想知道包含這個商品的每個分類有多少商品,每個價格區間有多少商品。

           下面的例子統計每個分類有多少商品。

                         

             

 /// <summary>        /// facet按類型查詢        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void button3_Click(object sender, EventArgs e)        {            label3.Visible = true;               var  dic=new Dictionary<string,ICollection<string>>();               dic["facet"] = new string[] { "true" };               var options = new List<string>();               options.Add("CategoryName");               dic["facet.field"] = options;            var result = operations.Query("collection1", "/select", SolrQuery.All,dic);            var header = binaryResponseHeaderParser.Parse(result);            var examples = binaryQueryResultsParser.Parse(result);            //分組List<FacetField>            IDictionary<string, IList<FacetField>> facetDic=new BinaryFacetFieldsParser().Parse(result);            string strFacet = "";            foreach (var item in facetDic)            {                strFacet +="分組欄位:"+item.Key+"\r\n";                foreach (var facetItem in item.Value)                {                    strFacet += facetItem.Name + "(" + facetItem.Count.ToString() + ")" + "---";                }            }            label3.Text = strFacet;            this.dataGridView1.DataSource = examples.ToList();        }

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

 


 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.