效果:
solr时间分组统计拼写:
Calendar calendar = new GregorianCalendar();
try {
calendar.setTime(DateUtils.parseDate(DateUtils.getYear(), "YYYY"));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
calendar.add(Calendar.YEAR, -10);
query.addDateRangeFacet("publishDate", calendar.getTime(), new Date(), "+1YEAR");
QueryResponse 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());
}
}
Map<Integer, Integer> map = new TreeMap<Integer, Integer>(new Comparator<Integer>() {
public int compare(Integer obj1, Integer obj2) {
// 降序排序
return obj2.compareTo(obj1);
}
});
;
List<RangeFacet> ranges = res.getFacetRanges();
for (RangeFacet r : ranges) {
List<org.apache.solr.client.solrj.response.RangeFacet.Count> countList = r.getCounts();
for (org.apache.solr.client.solrj.response.RangeFacet.Count o : countList) {
System.out.println("key=" + o.getValue() + " value=" + o.getCount());
map.put((Integer.parseInt(o.getValue().substring(0, 4)) + 1), o.getCount());
}
}
按年份查询 {、[ 开闭区间:
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
int year = Integer.parseInt(publishDate) + 1;
query.addFilterQuery("publishDate:{" + sdf.format(DateUtils.parseDate(publishDate, "YYYY")) + " TO "
+ sdf.format(DateUtils.parseDate(year + "", "YYYY")) + "]");
} catch (Exception e) {
e.printStackTrace();
}