用solr的facet实现聚合标签(转载)

Facet,单词意思是侧面,平面。哈哈,怎么学英文单词了……

好吧,言归正题,solr的Facet是一个什么东西呢?我个人理解,反映一个搜索词的平面(或者说某一个分组),起到标签聚合统计的功能。举个列子说,像我们公司的电商类网站那样的对搜索结果聚合分类,品牌等属性。如下图

这个是搜索铁观音这个词,统计了分类和品牌2个侧面(分组),铁观音在生活分类下有64个,茗茶里面48……

上面这个说到底,就是某一类型的标签统计,比如xxx年热词等等,只要有记录,也能统计出来,当然,超大数据量提前优化好solr的性能。

介绍了一下facet之后,来说说怎么实现facet。facet的实现其实很简单,主要在搜索参数上带上就OK。

facet=on/true      #代表开启facet
facet.field=cate  #代表要统计的面(分组),比如上面的分类,品牌,可以多次出现
facet.limit =20    #每个分组最多返回条数
facet.mincount = 1 #这个表示分组下某一条目的最数据量
facet.missing = on/true #统计null的值
facet.method =   #默认为fc, fc表示Field Cache
比如:http://localhost/product/select/?q=铁观音&facet=on&facet.field=category&facet.field=brand&facet.mincount=1在搜索结果中返回xml的facet结果

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
< lst name = "facet_counts" >
< lst name = "facet_queries" />
< lst name = "facet_fields" >
< lst name = "category" >
< int name = "2742" >64</ int >
< int name = "793" >48</ int >
< int name = "2741" >12</ int >
< int name = "801" >6</ int >
< int name = "1087" >1</ int >
</ lst >
< lst name = "brand" >
< int name = "229" >74</ int >
< int name = "227" >16</ int >
< int name = "270" >13</ int >
< int name = "317" >10</ int >
< int name = "0" >4</ int >
< int name = "165" >4</ int >
< int name = "203" >3</ int >
< int name = "147" >2</ int >
< int name = "166" >2</ int >
< int name = "217" >1</ int >
< int name = "342" >1</ int >
< int name = "343" >1</ int >
</ lst >
</ lst >

<lst name="category"> 分组名
<int name="2742">64</int> 分组内条目,name表示条目,64是统计结果数。

用solrJ那就更简单了

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
ModifiableSolrParams params = new ModifiableSolrParams();
params.set( "fq" , fq);
params.set( "facet" , "on" );
params.set( "facet.field" , "category" , "brand" );
params.set( "facet.mincount" , "1" );
params.set( "facet.limit" , "15" );
params.set( "q" , "铁观音" );
QueryResponse qresponse = queryserver.query(params);
FacetField facetField = qresponse.getFacetField(Facet_CATEGORY);
List<Count> counts = null ;
if (facetField != null ) {
  counts = facetField.getValues();
  if (counts != null ) {
  for (Count count : counts) {
  System.out.println(count.getName()+ " " +count.getCount());
  }
  }
}

Facet应用很简单,schema上的索引字段都可以作为面统计

 

猜你喜欢

转载自wb284551926.iteye.com/blog/2210775