使用:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25SolrQuery query = new SolrQuery(); if (StringUtils.isNotBlank(name)) { for (String s : name.split(",")) { if (com.qhwl.common.utils.StringUtils.isChinese(s)) { sb.append(s); } else { sb.append("*" + s + "* "); } } query.setQuery(sb.toString());// 从查全部 } else { query.setQuery("*:*");// 无参数,就查全部 }
复制代码
1query.addFacetField("datasource_copy");
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15QueryResponse res = SolrUtil.queryByGroup(query); for (FacetField f : res.getFacetFields()) { System.out.println("name1:" + f.getName() + "value1:" + f.getValueCount()); for (Count count : f.getValues()) { if (count.getCount() > 0) { for (int i = 0; i < orgList.size(); i++) { if (orgList.get(i).getName().equalsIgnoreCase(count.getName())) { orgList.get(i).setCount((int) count.getCount()); } } } System.out.println("name:" + count.getName() + "value:" + count.getCount()); } }
java分组方法:
复制代码
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
26
27
28
29
30/** * solr facet 分组统计 * * @param query * 需传入查询条件 设置分组字段可设置多个 * @return query.setQuery(sb.toString());//设置查询条件 * query.addFacetField(“modified_l”);//增加分组字段 q * rsp.getFacetFields()//获取统计信息 */ public static QueryResponse queryByGroup(SolrQuery query) { try { HttpSolrClient server = new HttpSolrClient.Builder(SOLR_URL + Global.getConfig("solr_core_all")) .withConnectionTimeout(10000).withSocketTimeout(60000).build(); query.setIncludeScore(false);// 是否按每组数量高低排序 query.setFacet(true);// 是否分组查询 query.setRows(0);// 设置返回结果条数,如果你时分组查询,你就设置为0 // query.setFacetSort(true);//分组是否排序 query.setFacetLimit(-1);// 限制每次返回结果数 QueryResponse rsp = server.query(query); return rsp; // List<Count> returnList = // rsp.getFacetField(groupField).getValues(); } catch (Exception e) { e.printStackTrace(); } return null; }
分组时经常会统计分词字段这时候就会出现字段拆分统计,这种统计并没意义,所有创建一个相应的String类型的copy字段
<field name="datasource" type="string" indexed="true" stored="true"/>
<field name="datasource_copy" type="string" indexed="true" stored="true" />
<copyField source="datasource" dest="datasource_copy" />
最后
以上就是无限灰狼最近收集整理的关于solr facet java分组统计的全部内容,更多相关solr内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复