標籤: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