solr高级查询——group和facet

1. 概述

facet只是简单统计记录数,如果需要获取doc信息,并不能为每组数据返回实际的数据回来,查询实际数据还需要再次进行查询,group类似于关系型数据库中的group by,除了分组外,还能返回实际数据

2. 查询示例

2.1 group

查询solr+memory,显示id和name字段,按照price分组,

http://192.168.10.125:8983/solr/techproducts/select?fl=id,name&q=solr+memory&group=true&group.field=price

分组结果:

{
  "responseHeader":{
    "status":0,
    "QTime":46,
    "params":{
      "q":"solr memory",
      "fl":"id,name",
      "group.field":"price",
      "group":"true"}},
  "grouped":{
    "price":{
      "matches":6,
      "groups":[{
          "groupValue":0.0,
          "doclist":{"numFound":1,"start":0,"docs":[
              {
                "id":"SOLR1000",
                "name":"Solr, the Enterprise Search Server"}]
          }},
        {
          "groupValue":74.99,
          "doclist":{"numFound":1,"start":0,"docs":[
              {
                "id":"VS1GB400C3",
                "name":"CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail"}]
          }},
          ...
    ]}}}

2.2 facet

按照price进行facet

http://192.168.10.125:8983/solr/techproducts/select?q=*:*&facet=true&facet.field=price&facet.limit=8

facet结果片段,显示了不同价格和其对应的数量

"facet_counts":{
    "facet_queries":{},
    "facet_fields":{
      "price":[
        "0.0",3,
        "11.5",1,
        "19.95",1,
        "74.99",1,
        "92.0",1,
        "179.99",1,
        "185.0",1,
        "279.95",1]},
    "facet_ranges":{},
    "facet_intervals":{},
    "facet_heatmaps":{}}}

java代码实现

2.1 group 实现

public static void groupQuery1(HttpSolrClient solrClient,String collecionName) throws Exception{
        // 设置查询条件
        SolrQuery query = new SolrQuery();
        query.setQuery("solr+memory");
        // 返回列
        query.setFields("id,name");
        // 开启group的分组查询
        query.setParam(GroupParams.GROUP, true);
        // 设置分组的字段
        query.setParam(GroupParams.GROUP_FIELD, "price");
        // 设置返回行数
        query.setRows(5);
        QueryResponse qr = solrClient.query(collecionName,query);
        GroupResponse groupResponse = qr.getGroupResponse();
        // 分组字段种类的list
        List<GroupCommand> values = groupResponse.getValues();
        for (GroupCommand groupCommand : values) {
            String groupName = groupCommand.getName();
            // 每种分类字段下包含多少个值
            List<Group> groupValue = groupCommand.getValues();
            for (Group group : groupValue) {
                // 搜索结果
                SolrDocumentList result = group.getResult();
                for (SolrDocument solrDocument : result) {
                    Object id = solrDocument.getFieldValue("id");
                    Object name = solrDocument.getFieldValue("name");
                    System.out.println("groupName: "+groupName+"; id: "+ id+"; name: "+name);
                }
            }
        }
    }

输出结果

groupName: price; id: SOLR1000; name: Solr, the Enterprise Search Server
groupName: price; id: VS1GB400C3; name: CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail
groupName: price; id: VDBDB1A16; name: A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM
groupName: price; id: TWINX2048-3200PRO; name: CORSAIR  XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail
groupName: price; id: 0579B002; name: Canon PIXMA MP500 All-In-One Photo Printer

2.2 facet

public static void queryFacet(HttpSolrClient solrClient,String collecionName) throws Exception{
        SolrQuery query = new SolrQuery();
        query.setQuery("*");
        // 不查询数据,只查询facet结果
        query.setFacet(true);
        // facet字段
        query.addFacetField("price");
        // facet结果总数
        query.setFacetLimit(8);
        // 开始查询
        QueryResponse queryResponse = solrClient.query(collecionName, query);
        List<FacetField> facetFields = queryResponse.getFacetFields();
        for (FacetField facetField : facetFields) {
            String facetName=facetField.getName();
            List<FacetField.Count> values = facetField.getValues();
            for (FacetField.Count count : values) {
                String name = count.getName();
                long num=count.getCount();
                System.out.println("facetName: "+facetName+"; name: "+name+"; num: "+num);
            }
        }
    }

输出结果:

facetName: price; name: 0.0; num: 3
facetName: price; name: 11.5; num: 1
facetName: price; name: 19.95; num: 1
facetName: price; name: 74.99; num: 1
facetName: price; name: 92.0; num: 1
facetName: price; name: 179.99; num: 1
facetName: price; name: 185.0; num: 1
facetName: price; name: 279.95; num: 1
参考链接

https://blog.csdn.net/a925907195/article/details/47257243

http://lucene.apache.org/solr/guide/7_0/faceting.html

http://lucene.apache.org/solr/guide/7_0/result-grouping.html

转载于:https://my.oschina.net/freelili/blog/1939361

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值