1、要实现的功能
2、索引中字段
3、solr搜索
4、solr结果
{
"responseHeader":{
"status":0,
"QTime":0,
"params":{
"facet":"true",
"indent":"true",
"q":"shop_id:123",
"wt":"json",
"facet.pivot":"p_type_name,type_name",
"rows":"1"}},
"response":{"numFound":15,"start":0,"docs":[
{
"add_description":" 天然美白精华液,天然水果提取的VC成分同时配合绿茶抗氧化成分,抑制黑色素生成,促进肌肤胶原蛋白的合成,预防斑点生成,亮白肤色同时收敛毛孔,让皮肤变得洁净亮白更加紧致透明,充满弹力\r\n \r\n ",
"type_one":"00010",
"brand_name":"It's skin 伊思",
"sug":["It's skin 伊思",
"精华",
"itsskin 伊思10倍VC美白精华原液"],
"id":"214",
"share_number":0,
"rank":0,
"brand_thumb":"http://n.baai.com/baai/uploadfile/2015/06/26/201506261520194805.jpg",
"total_num":100,
"discuss_number":0,
"sales":4,
"p_type_name":"护肤",
"name":"itsskin 伊思10倍VC美白精华原液",
"name_autocomplete":"itsskin 伊思10倍VC美白精华原液",
"type_three":"00011",
"rebate":100,
"save_number":1,
"love_number":0,
"shop_id":123,
"brand_id":153,
"code":"000100010000011",
"page_view":128,
"type_id":404,
"type_parent":350,
"price":10000,
"price_c":"10000,USD",
"type_name":"精华",
"suggestion":"itsskin 伊思10倍VC美白精华原液",
"p_id":214,
"type_two":"0001000100",
"thumb":"http://n.baai.com/baai/uploadfile/2015/07/18/20150718141410960.jpg",
"_version_":1508018604938887168}]
},
"facet_counts":{
"facet_queries":{},
"facet_fields":{},
"facet_dates":{},
"facet_ranges":{},
"facet_pivot":{
"p_type_name,type_name":[{
"field":"p_type_name",
"value":"护肤",
"count":13,
"pivot":[{
"field":"type_name",
"value":"乳液",
"count":2},
{
"field":"type_name",
"value":"化妆水",
"count":2},
{
"field":"type_name",
"value":"眼霜",
"count":2},
{
"field":"type_name",
"value":"精华",
"count":2},
{
"field":"type_name",
"value":"面膜",
"count":2},
{
"field":"type_name",
"value":"面霜",
"count":2},
{
"field":"type_name",
"value":"洁面",
"count":1}]},
{
"field":"p_type_name",
"value":"彩妆",
"count":2,
"pivot":[{
"field":"type_name",
"value":"BB霜",
"count":2}]}]}}}
5、solrJ实现
public List<SearchType> getTypeTree(Integer type,String name) throws SolrServerException{
SolrServer solrServer = SolrServerManger.getInstance();
SolrQuery sQuery = new SolrQuery();
List<SearchType> types=new ArrayList<SearchType>();
sQuery.setFacet(true);
sQuery.add("facet.pivot", "p_type_name,type_name"); //根据这两维度来分组查询
switch (type) {
case 1:
sQuery.setQuery("name:"+name);
break;
case 2:
sQuery.setQuery("shop_id:"+name);
break;
default:
sQuery.setQuery("*:*");
break;
}
QueryResponse response = solrServer.query(sQuery);
NamedList<List<PivotField>> namedList = response.getFacetPivot();
if(namedList != null){
List<PivotField> pivotList = null;
for(int i=0;i<namedList.size();i++){
pivotList = namedList.getVal(i);
if(pivotList != null){
for(PivotField pivot:pivotList){
SearchType typeTwo=new SearchType();
typeTwo.setName(pivot.getValue().toString());
List<PivotField> fieldList = pivot.getPivot();
if(fieldList != null){
List<SearchType> typeThrees=new ArrayList<SearchType>();
for(PivotField field:fieldList){
SearchType typeThree=new SearchType();
typeThree.setName(field.getValue().toString());
typeThrees.add(typeThree);
}
typeTwo.setSubTypes(typeThrees);
types.add(typeTwo);
}
}
}
}
}
return types;
}
6、solrj测试代码
public static void main(String [] args) throws SolrServerException{
List<SearchType> types=getTypeTree(2, "123");
for(SearchType t:types){
System.err.println(t.getName()+"===========");
for(SearchType tt:t.getSubTypes()){
System.err.println(" ---"+tt.getName());
}
}
}
7、测试结果