solr两种分组查询的方法,以及资源的关闭

public class SolrFacetTest {

@Test
public void jqxxFacet() {
    SolrQuery query = new SolrQuery("*:*");
    query.setStart(0);
    query.setRows(0);
    // 会显示全部,不然只会默认的展示10或者100个数据
    query.setFacetLimit(-1);
    query.setFacet(true);
    query.addFacetField("BJLXDM_s", "BJLXDM_FORMAT_s");
    // 统计为0的不返回
    query.setFacetMinCount(1);
    query.setFacetSort(FacetParams.FACET_SORT_COUNT);
    QueryResponse    response = SolrSample.execute(query, "http://172.30.10.31:10180/solr/XS_JJDXX");
    List<FacetField> facets   = response.getFacetFields();//返回的facet列表
    for (FacetField facet : facets) {
        System.out.println("----------------");
        List<FacetField.Count> counts = facet.getValues();
        for (FacetField.Count count : counts) {
            System.out.println(count.getName() + ":" + count.getCount());
        }
        System.out.println();
    }
}

@Test
public void jqxxFacetPivot() {
    SolrQuery query = new SolrQuery("*:*");
    query.setFacet(true);
    // 多个纬度字段需要拼接在一起
    // query.add("facet.pivot", "BJLXDM_s,BJLXDM_FORMAT_s");
    query.addFacetPivotField("BJLXDM_s,BJLXDM_FORMAT_s");
    QueryResponse               response  = SolrSample.execute(query, "http://172.30.10.31:10180/solr/XS_JJDXX");
    NamedList<List<PivotField>> namedList = response.getFacetPivot();//返回的facet列表
    System.out.println("----------------");
    Map<String, String> map = new HashMap<>();
    if (namedList != null) {
        for (int i = 0; i < namedList.size(); i++) {
            List<PivotField> pivotList = namedList.getVal(i);
            if (pivotList != null) {
                for (PivotField pivot : pivotList) {
                    map.put(pivot.getValue().toString(),
                            pivot.getPivot() != null ? pivot.getPivot().get(0).getValue().toString() : "");
                    List<PivotField> fieldList = pivot.getPivot();
                    if (fieldList != null) {
                        for (PivotField field : fieldList) {
                            System.out.println("---");
                        }
                    }
                }
            }
        }
    }
    System.out.println(JsonUtil.toJsonStr(map));
    System.out.println("----------------");
}

}

public class SolrSample {

public static QueryResponse execute(SolrQuery query, String url) {
    assert null != query && null != url && !url.isEmpty();

    SolrClient client = new HttpSolrClient.Builder().withBaseSolrUrl(url).build();
    try {
        return client.query(query);
    } catch (SolrServerException | IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            if (client != null) client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值