您的位置:首页 > 其它

电商指尖---(9).net发展Solr中间Facet特征

2015-07-27 13:28 246 查看
上一节中我们演示了在SolrAdmin中使用Facet功能来进行分组统计。这一节我们看看如何使用.NET开发Solr中的Facet功能。在讲Facet功能的同一时候,

我们看下.Net中如何使用Solr查询。使用的client工具是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
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: