1. 概述
facet只是簡單統計記錄數,如果需要獲取doc信息,並不能爲每組數據返回實際的數據回來,查詢實際數據還需要再次進行查詢,group類似於關係型數據庫中的group by,除了分組外,還能返回實際數據
2. 查詢示例
2.1 group
查詢solr+memory,顯示id和name字段,按照price分組,
http://192.168.10.125:8983/solr/techproducts/select?fl=id,name&q=solr+memory&group=true&group.field=price
分組結果:
{
"responseHeader":{
"status":0,
"QTime":46,
"params":{
"q":"solr memory",
"fl":"id,name",
"group.field":"price",
"group":"true"}},
"grouped":{
"price":{
"matches":6,
"groups":[{
"groupValue":0.0,
"doclist":{"numFound":1,"start":0,"docs":[
{
"id":"SOLR1000",
"name":"Solr, the Enterprise Search Server"}]
}},
{
"groupValue":74.99,
"doclist":{"numFound":1,"start":0,"docs":[
{
"id":"VS1GB400C3",
"name":"CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail"}]
}},
...
]}}}
2.2 facet
按照price進行facet
http://192.168.10.125:8983/solr/techproducts/select?q=*:*&facet=true&facet.field=price&facet.limit=8
facet結果片段,顯示了不同價格和其對應的數量
"facet_counts":{
"facet_queries":{},
"facet_fields":{
"price":[
"0.0",3,
"11.5",1,
"19.95",1,
"74.99",1,
"92.0",1,
"179.99",1,
"185.0",1,
"279.95",1]},
"facet_ranges":{},
"facet_intervals":{},
"facet_heatmaps":{}}}
java代碼實現
2.1 group 實現
public static void groupQuery1(HttpSolrClient solrClient,String collecionName) throws Exception{
// 設置查詢條件
SolrQuery query = new SolrQuery();
query.setQuery("solr+memory");
// 返回列
query.setFields("id,name");
// 開啓group的分組查詢
query.setParam(GroupParams.GROUP, true);
// 設置分組的字段
query.setParam(GroupParams.GROUP_FIELD, "price");
// 設置返回行數
query.setRows(5);
QueryResponse qr = solrClient.query(collecionName,query);
GroupResponse groupResponse = qr.getGroupResponse();
// 分組字段種類的list
List<GroupCommand> values = groupResponse.getValues();
for (GroupCommand groupCommand : values) {
String groupName = groupCommand.getName();
// 每種分類字段下包含多少個值
List<Group> groupValue = groupCommand.getValues();
for (Group group : groupValue) {
// 搜索結果
SolrDocumentList result = group.getResult();
for (SolrDocument solrDocument : result) {
Object id = solrDocument.getFieldValue("id");
Object name = solrDocument.getFieldValue("name");
System.out.println("groupName: "+groupName+"; id: "+ id+"; name: "+name);
}
}
}
}
輸出結果
groupName: price; id: SOLR1000; name: Solr, the Enterprise Search Server
groupName: price; id: VS1GB400C3; name: CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail
groupName: price; id: VDBDB1A16; name: A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM
groupName: price; id: TWINX2048-3200PRO; name: CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail
groupName: price; id: 0579B002; name: Canon PIXMA MP500 All-In-One Photo Printer
2.2 facet
public static void queryFacet(HttpSolrClient solrClient,String collecionName) throws Exception{
SolrQuery query = new SolrQuery();
query.setQuery("*");
// 不查詢數據,只查詢facet結果
query.setFacet(true);
// facet字段
query.addFacetField("price");
// facet結果總數
query.setFacetLimit(8);
// 開始查詢
QueryResponse queryResponse = solrClient.query(collecionName, query);
List<FacetField> facetFields = queryResponse.getFacetFields();
for (FacetField facetField : facetFields) {
String facetName=facetField.getName();
List<FacetField.Count> values = facetField.getValues();
for (FacetField.Count count : values) {
String name = count.getName();
long num=count.getCount();
System.out.println("facetName: "+facetName+"; name: "+name+"; num: "+num);
}
}
}
輸出結果:
facetName: price; name: 0.0; num: 3
facetName: price; name: 11.5; num: 1
facetName: price; name: 19.95; num: 1
facetName: price; name: 74.99; num: 1
facetName: price; name: 92.0; num: 1
facetName: price; name: 179.99; num: 1
facetName: price; name: 185.0; num: 1
facetName: price; name: 279.95; num: 1
參考鏈接
https://blog.csdn.net/a925907195/article/details/47257243
http://lucene.apache.org/solr/guide/7_0/faceting.html
http://lucene.apache.org/solr/guide/7_0/result-grouping.html