设为首页 收藏本站
查看: 1344|回复: 0

[经验分享] Solr中的group与facet的区别 [转]

[复制链接]

尚未签到

发表于 2017-12-19 14:35:21 | 显示全部楼层 |阅读模式
  Solr中的group与facet的区别
  如果是简单的使用的话,那么Facet与group都可以用来进行数据的聚合查询,但是他们还是有很大的区别的。
  首先上facet跟group的操作:
  Facet的例子:
public voidFacetFieldQuery() throws Exception {

      solrServer = createSolrServer();

      SolrQueryquery = newSolrQuery();//建立一个新的查询

      query.setQuery("jobsName:计算机维护");

      query.setFacet(true);//设置facet=on

      // 分类信息分为:薪水,发布时间,教育背景,工作经验,公司类型,工作类型

      query.addFacetField(new String[] {"salary","publishDate",

            "educateBackground","jobExperience","companytype","jobsType" });//设置需要facet的字段

      query.setFacetLimit(10);// 限制facet返回的数量

      query.setFacetMissing(false);//不统计null的值

      query.setFacetMinCount(1);// 设置返回的数据中每个分组的数据最小值,比如设置为1,则统计数量最小为1,不然不显示


      //query.addFacetQuery("publishDate:[2014-04-11T00:00:00Z TO2014-04-13T00:00:00Z]");

      QueryResponseresponse = solrServer.query(query);

      System.out.println("查询时间:" + response.getQTime());

      List<FacetField>facets = response.getFacetFields();//返回的facet列表


      for (FacetField>
         System.out.println(facet.getName());

         System.out.println("----------------");


         List<Count>counts =>
         for (Count count : counts){

            System.out.println(count.getName()+":"+ count.getCount());

         }

         System.out.println();

      }


   }

运行结果如下:

查询时间:66

salary

----------------

面议:6882

2001-4000:1508

其他:671

4001-6000:536

3000-4499:224

2000-2999:181

6001-8000:179

3000-5000:82

1000-2000:81

4500-5999:75


publishDate

----------------

2014-08-05T00:00:00Z:793

2014-08-04T00:00:00Z:775

2014-07-30T00:00:00Z:601

2014-08-07T00:00:00Z:548

2014-08-06T00:00:00Z:539

2014-08-11T00:00:00Z:472

2014-08-20T00:00:00Z:439

2014-08-12T00:00:00Z:438

2014-08-01T00:00:00Z:405

2014-08-03T00:00:00Z:376


educateBackground

----------------

大专:4486

本科:1872

其他:1344

不限:1147

中专:680

高中:472

薪水范围::430

中技:161

初中:140

硕士:94


jobExperience

----------------

其他:2623

不限:2249

1-3年:1770

1年:1301

2年:773

3-4年:528

3-5年:379

应届毕业生:309

5-7年:162

1年以上:136


companytype

----------------

民营公司:3702

民营:2605

国企:835

股份制企业:729

其他:707

合资:632

外资(非欧美):377

外商独资:350

外资(欧美):271

上市公司:228


jobsType

----------------

全职:10734

兼职:59

实习:39



Group查询:

/**group查询

    * @throws Exception

    */

   public void GroupFieldQuery() throws Exception {

      solrServer = createSolrServer();

       SolrQuery query = new SolrQuery("jobsName:计算机维护");

        // 设置通过facet查询为true,表示查询时使用facet机制

        query.setParam(GroupParams.GROUP,true);   

        query.setParam(GroupParams.GROUP_FIELD,"salary");

        // 设置每个quality对应的

        query.setParam(GroupParams.GROUP_LIMIT,"1");

        // 设置返回doc文档数据,因只需要数量,故设置为0

        query.setRows(10);

        QueryResponse response = solrServer.query(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查询..."+group.getGroupValue()+"数量为:"+group.getResult().getNumFound());

              }   

          }   

            }   

        }


   }

group查询...面议数量为:6882

group查询...4500-5999数量为:75

group查询...2001-4000数量为:1508

group查询...其他数量为:671

group查询...2000-2999数量为:181

group查询...4001-6000数量为:536

group查询...2000-4000数量为:19

group查询...2000-3000数量为:34

group查询...3000-4499数量为:224

group查询...3000-5000数量为:82


facet的查询结果主要是分组信息:有什么分组,每个分组包括多少记录;但是分组中有哪些数据是不可知道的,只有进一步搜索。

  group则类似于关系数据库的group by,可以用于一个或者几个字段去重、显示一个group的前几条记录等。
The Grouping feature only works if groups are inthe same shard. You must use the custom sharding feature to use the Groupingfeature.


两者其实用起来还是有比较大的区别的,但是如果说区别的话可以看下wiki上的这段

  Field Collapsing and Result Grouping aredifferent ways to think about the same Solr feature.
  Field Collapsing collapsesa group of results with the same field value down to a single (or fixed number)of entries. For example, most search engines such as Google collapse on site soonly one or two entries are shown, along with a link to click to see moreresults from that site. Field collapsing can also be used to suppress duplicatedocuments.
  Result Grouping groupsdocuments with a common field value into groups, returning the top documentsper group, and the top groups based on what documents are in the groups. Oneexample is a search at Best Buy for a common term such as DVD, that shows thetop 3 results for each category ("TVs &Video","Movies","Computers", etc)
  下面这两个查询语句一个是facet的一个是group的
  http://localhost:8080/solr/JobsOtherWeb0/select?q=jobsName%3A%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BB%B4%E6%8A%A4&group=true&group.field=salary&group.limit=1&rows=10
http://localhost:8080/solr/JobsOtherWeb0/select?q=jobsName%3A%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BB%B4%E6%8A%A4&facet=true&facet.field=salary&facet.field=publishDate&facet.field=educateBackground&facet.field=jobExperience&facet.field=companytype&facet.field=jobsType&facet.limit=10&facet.missing=false&facet.mincount=1


其中facet查询出的如下:(只截取部分结果)

DSC0000.jpg

根据条件查询出的是查询结果,facet是聚类后的信息跟查询条件是分开的,查询结果也跟facet没关系。

但是下面看group查询的

DSC0001.jpg

也就是你的查询条件是跟group相关的,返回的查询结果也是跟group相关的,比如说你想要查询的结果在每个分组中 都有数据采集,那么就最好用group,这样出来的数据跟group也是相关的,但是有个问题,比如说你要查询group每个采集1个,ok那么你查询的 时候的条件rows就无效了(也不能说无效,主要是看你怎么使用),就是最多每个分组给你返回一个,多了没有了。

再细说点就是如果你想查询归查询聚类归聚类,那么使用facet,如果想使用类似采集的效果,每个group分组采集多少个,那么使用group查询。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-425725-1-1.html 上篇帖子: 装逼是个坑,越陷越深 下篇帖子: 09.Solr单机版安装配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表