###########################################################################################
Group 和 Facet的区别就是:
查询条件是跟group相关的,返回的查询结果也是跟group相关的,比如说你想要查询的结果在每个分组中都有数据采集,那么就最好用group,这样出来的数据跟group也是相关的,但是有个问题,比如说你要查询group每个采集1个,ok那么你查询的时候的条件rows就无效了(也不能说无效,主要是看你怎么使用),就是最多每个分组给你返回一个,多了没有了。
再细说点就是如果你想查询归查询聚类归聚类,那么使用facet,如果想使用类似采集的效果,每个group分组采集多少个,那么使用group查询。
###########################################################################################
public class TestSimpleSolr {
private static final String solrUrl = "http://127.0.0.1:8080/solr/";
private static final String solrCore = "solrcore1";
// 进行 Facet 的也必须 在配置文件中 INDEX = true
@Test
public void testQueryFacet() throws Exception {
HttpSolrClient client = new HttpSolrClient(solrUrl);
SolrQuery query = new SolrQuery("手机");
query.setStart(0); //索引
query.setRows(20); //每页显示20条
query.set("df", "item_keywords"); //默认域
query.addSort("item_price", ORDER.asc); //排序
query.addSort("item_cname", ORDER.desc);
query.addSort("id", ORDER.asc);
//query.setFields("id"); //设置返回的域 只返回id 其他为NULL 这个就是FL
query.addFilterQuery("item_price:[100000 TO 1000000]"); //添加过滤条件
/** ----------Facet相关-------------- **/
query.setFacet(true); //设置开启facet
query.setFacetMinCount(1); // 设置返回的数据中每个分组的数据最小值,比如设置为1,则统计数量最小为1,不然不显示
query.setFacetLimit(10); // 限制facet返回的数量
// query.addFacetField("item_cname","item_price"); //设置需要facet的字段
query.addFacetField("item_cname"); //设置需要facet的字段
query.addFacetQuery("item_price:[1 TO 100000]"); //设置facetQuery
query.addFacetQuery("item_price:[100000 TO 1000000]");
query.addFacetQuery("item_price:[1000000 TO *]");
query.setFacetMissing(false); //不统计null的值
query.setFacetSort(FacetParams.FACET_SORT_COUNT);
/** ----------Facet相关-------------- **/
/** -------------Highlight相关--------- **/
query.setHighlight(true); //设置高亮
query.addHighlightField("item_title"); //设置高亮的域
query.setHighlightSimplePre("<H1>"); //前缀
query.setHighlightSimplePost("</H1>"); //后缀
/** -------------Highlight相关--------- **/
QueryResponse response = client.query(solrCore, query);
/** ----------Facet处理结果相关---------- **/
Map<String, Integer> facetQuery = response.getFacetQuery();
for (Map.Entry<String, Integer> map : facetQuery.entrySet()) {
System.out.println(map.getKey() + ":" + map.getValue());
}
List<FacetField> facetFields = response.getFacetFields();
for (FacetField ff : facetFields) {
System.out.println("------------------");
System.out.println(ff.getName() + ":" + ff.getValueCount());
List<Count> counts = ff.getValues();
for (Count count : counts) {
System.out.println("name=" + count.getName() + "\tcount=" + count.getCount());
}
System.out.println("--------------------");
}
/** ----------Facet处理结果相关---------- **/
SolrDocumentList result = response.getResults();
/** ----------Highlight处理结果相关 ------------ **/
for (SolrDocument doc : result) {
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
List<String> list = highlighting.get(doc.get("id")).get("item_title");
if (list != null) {
doc.setField("item_title", list.get(0));
}
}
/** ----------Highlight处理结果相关 ------------ **/
System.out.println("状态 = " + response.getStatus());
long numFound = result.getNumFound();
System.out.println("查询总数 = " + numFound);
List<SolrItem> beans = SolrUtil.getBeans(SolrItem.class, result);
for (SolrItem solrItem : beans) {
System.out.println(solrItem);
}
client.close();
}
//进行Group by的必须索引 Index = true
@Test
public void testGroup() throws Exception {
HttpSolrClient client = new HttpSolrClient(solrUrl);
SolrQuery query = new SolrQuery("手机");
query.set("df", "item_keywords"); //默认域
query.setParam(GroupParams.GROUP, true);
query.setParam(GroupParams.GROUP_FIELD, "item_cname");
query.setParam(GroupParams.GROUP_QUERY, "item_price:[100000 TO 1000000]", "item_price:[1000000 TO *]");
query.setParam(GroupParams.GROUP_LIMIT, "1");
query.setStart(0); //索引
query.setRows(20); //每页显示20条
QueryResponse response = client.query(solrCore ,query);
if (response != null) {
GroupResponse groupResponse = response.getGroupResponse();
if (groupResponse != null) {
List<GroupCommand> groupList = groupResponse.getValues();
for (GroupCommand groupCommand : groupList) {
List<Group> groups = groupCommand.getValues();
for (Group group : groups) {
System.out.println(group.getGroupValue() + ":" + group.getResult().getNumFound());
}
}
}
}
client.close();
}
@Test
public void testAdd() throws Exception {
HttpSolrClient client = new HttpSolrClient(solrUrl);
String sql = "SELECT"+
" t1.id,t1.title as item_title,"+
" t1.cid as item_cid,"+
" t1.sell_point as item_sell_point,"+
" t1.price as item_price,"+
" t1.num as item_num,t1.image as item_image,"+
" t2.name as item_cname,"+
" t3.item_desc"+
" FROM"+
" tb_item t1"+
" LEFT JOIN tb_item_cat t2 ON t1.cid = t2.id"+
" LEFT JOIN tb_item_desc t3 ON t1.id = t3.item_id";
List<SolrItem> list = new BaseDao().findResult(SolrItem.class, sql);
long start = System.currentTimeMillis();
SolrUtil.addBeans(list, client, solrCore);
long end = System.currentTimeMillis();
System.out.println(end - start);
}
@Test
public void testDel() throws Exception {
HttpSolrClient client = new HttpSolrClient(solrUrl);
SolrUtil.deleteByQuery(client, solrCore, "*:*");
client.commit(solrCore);
}
}
本文出自 “12265610” 博客,谢绝转载!
原文地址:http://12275610.blog.51cto.com/12265610/1920746