solr分组查询
package cn.sniper.solr.util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.FacetField;
import org.apache.solr.client.solrj.response.FacetField.Count;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import cn.sniper.solr.model.Message;
import cn.sniper.solr.model.Page;
import cn.sniper.solr.model.PaginationVo;
public class SolrUtil {
public static final String URL = "http://localhost:8080/solr/collection1";
private static HttpSolrServer solrServer;
public static final Logger logger = Logger.getLogger(SolrUtil.class);
public static final String LOG_SEPARATOR = "#";
public static final String QUERY_KEYWORD = "QUERY_KEYWORD";
/**
* 初始化
*/
static {
solrServer = new HttpSolrServer(URL);
solrServer.setMaxRetries(1); // defaults to 0. > 1 not recommended.
solrServer.setConnectionTimeout(5000); // 5 seconds to establish TCP
// Setting the XML response parser is only required for cross
// version compatibility and only when one side is 1.4.1 or
// earlier and the other side is 3.1 or later.
solrServer.setParser(new XMLResponseParser()); // binary parser is used by default
// The following settings are provided here for completeness.
// They will not normally be required, and should only be used
// after consulting javadocs to know whether they are truly required.
solrServer.setSoTimeout(1000); // socket read timeout
solrServer.setDefaultMaxConnectionsPerHost(1000);
solrServer.setMaxTotalConnections(1000);
solrServer.setFollowRedirects(false);
// defaults to false
// allowCompression defaults to false.
// Server side must support gzip or deflate for this to have any effect.
solrServer.setAllowCompression(true);
}
/**
* 批量添加
* @param list
*/
public static void addList(List<Map<String, Object>> list) {
if(null != list && list.size() > 0) {
List<SolrInputDocument> docList = new ArrayList<SolrInputDocument>();
for(Map<String, Object> map : list) {
SolrInputDocument doc = new SolrInputDocument();
Set<String> set = map.keySet();
for(String key : set) {
Object value = map.get(key);
doc.addField(key, value);
}
docList.add(doc);
}
try {
solrServer.add(docList);
} catch (SolrServerException e) {
e.printStackTrace();
logger.error(SolrUtil.class.getName() + LOG_SEPARATOR + "addDocumentList" + LOG_SEPARATOR + "批量添加document出现异常。。。" + LOG_SEPARATOR + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
logger.error(SolrUtil.class.getName() + LOG_SEPARATOR + "addDocumentList" + LOG_SEPARATOR + "批量添加document出现异常。。。" + LOG_SEPARATOR + e.getMessage());
}
}
}
/**
* 分组查询
* @param keyword
* @param fields
* @param facetFields
* @return
*/
public static void queryGroup(String keyword, String[] fields, String[] facetFields) {
StringBuffer queryStr = new StringBuffer();
if(fields != null && fields.length > 0) {
for(String field : fields) {
queryStr.append(field).append(" ");
}
} else {
queryStr.append("*");
}
SolrQuery query = new SolrQuery(keyword);
query.setParam("qf", queryStr.toString().trim())
.setParam("defType", "edismax") //qf方式查询,需要用edismax解析器
.setIncludeScore(false) //是否按每组数量高低排序
.setFacet(true) //启用分组
.setRows(0) //设置返回结果条数,如果你时分组查询,你就设置为0
.setFacetMinCount(1) //只显示大于等于1的记录
.setFacetLimit(5) //限制每次返回结果数
.addFilterQuery("proPrice:[500 TO 1000]");
for(String facetField : facetFields) {
query.addFacetField(facetField.trim());//对哪个字段做分组统计
}
try {
QueryResponse resp = solrServer.query(query);
List<FacetField> facets = resp.getFacetFields();
for(FacetField ff : facets) {
System.out.println(ff.getName() + ":" + ff.getValueCount());
List<Count> list = ff.getValues();
for(Count c : list) {
System.out.println(c.getName() + ":" + c.getCount());
}
}
} catch (SolrServerException e) {
e.printStackTrace();
logger.error(SolrUtil.class.getName() + LOG_SEPARATOR + "queryData" + LOG_SEPARATOR + "检索数据出现异常。。。" + LOG_SEPARATOR + e.getMessage());
} finally {
query.clear();
}
}
/**
* 硬提交,写到硬盘
* 在代码中,一般不显示调用,可以在配置文件中配置每隔多少时间自动调用一次
*/
public static void commit() {
try {
solrServer.commit();
} catch (SolrServerException e) {
e.printStackTrace();
logger.error(SolrUtil.class.getName() + LOG_SEPARATOR + "commit" + LOG_SEPARATOR + "硬提交出现异常。。。" + LOG_SEPARATOR + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
logger.error(SolrUtil.class.getName() + LOG_SEPARATOR + "commit" + LOG_SEPARATOR + "硬提交出现异常。。。" + LOG_SEPARATOR + e.getMessage());
}
}
/**
* 软提交,写到内存
* 在代码中,一般不显示调用,可以在配置文件中配置每隔多少时间自动调用一次
*/
public static void softCommit() {
try {
solrServer.commit(true, true, true);
} catch (SolrServerException e) {
e.printStackTrace();
logger.error(SolrUtil.class.getName() + LOG_SEPARATOR + "softCommit" + LOG_SEPARATOR + "软提交出现异常。。。" + LOG_SEPARATOR + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
logger.error(SolrUtil.class.getName() + LOG_SEPARATOR + "softCommit" + LOG_SEPARATOR + "软提交出现异常。。。" + LOG_SEPARATOR + e.getMessage());
}
}
/**
* 索引段优化
* 将磁盘上的多个索引段合成一个大的索引段
*/
public static void optimize() {
try {
solrServer.optimize();
} catch (SolrServerException e) {
e.printStackTrace();
logger.error(SolrUtil.class.getName() + LOG_SEPARATOR + "optimize" + LOG_SEPARATOR + "索引段优化出现异常。。。" + LOG_SEPARATOR + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
logger.error(SolrUtil.class.getName() + LOG_SEPARATOR + "optimize" + LOG_SEPARATOR + "索引段优化出现异常。。。" + LOG_SEPARATOR + e.getMessage());
}
}
public static void main(String[] args) {
SolrUtil.delAll();
SolrUtil.init();
SolrUtil.softCommit();
SolrUtil.queryGroup("*", null, new String[]{"proCategory"});
}
/**
* 构造分组查询数据
*/
public static void init() {
//主键 商品名称(proName) 商品分类(proCategory) 商品价格(proPrice) 入库时间(executeTime)
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("id", 1);
map1.put("proName", "苹果手机");
map1.put("proCategory", "手机");
map1.put("proPrice", 5555.9);
list.add(map1);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("id", 2);
map2.put("proName", "小米手机");
map2.put("proCategory", "手机");
map2.put("proPrice", 1500);
list.add(map2);
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("id", 3);
map3.put("proName", "华硕电脑");
map3.put("proCategory", "电脑");
map3.put("proPrice", 3000);
list.add(map3);
Map<String, Object> map4 = new HashMap<String, Object>();
map4.put("id", 4);
map4.put("proName", "戴尔电脑");
map4.put("proCategory", "电脑");
map4.put("proPrice", 6000);
list.add(map4);
Map<String, Object> map5 = new HashMap<String, Object>();
map5.put("id", 5);
map5.put("proName", "花花公子");
map5.put("proCategory", "服装");
map5.put("proPrice", 500);
list.add(map5);
SolrUtil.addList(list);
}
}