今天碰到一个问题,数据之前入solr的时候并没有计算条数,现在需要计算出某几个表中去重后的总数。
由于solr的ISearch并没有相关的Distinct功能.想到一个解决方案是用Solr的Facet分组进行GrupBy,但是因为Facet只能返回100条,而数据肯定大于100个分组.所有该方案PASS了。
后来在网上搜到Solr Count Distinct,这么一个东西,是Solr已经发布的脚本(Solr Search Requests)其中有类似的功能

A 100% accurate count of distinct values (count distinct) is not generally possible without actually observing all of the values together. However there are a number of ways to estimate the count.

“unique” Facet Function
  The unique facet function is Solr’s fastest implementation to calculate the number of distinct values.
  It always provides exact counts on a single Solr node. For distributed search over multiple nodes, it provides exact counts when the number of values per node does not exceed 100 (by default).

When the number of unique values does exceed 100 in any given shard, the following algorithm is used:

It estimates the count by sending the top 100 results from each shard along with the total exact “unique” count for each shard.
  totalSeen is the number of actual results we saw from all shards (i.e. not deduped yet).
  uniqueSeen is the number of unique values we saw from all shards (i.e. deduped).
  notSeen is the number of unique values from each shard that were not sent (because of the 100 cutoff).
  factor = uniqueSeen / totalSeen (i.e. what fraction of values that we saw were unique)
  estimate = uniqueSeen + ( notSeen * factor ) (i.e. we simply apply the factor to the number of values we didn’t see)
  Example use:

$ curl http://localhost:8983/solr/techproducts/query -d '
q=*:*&
json.facet={
x : "unique(manu_exact)" // manu_exact is the manufacturer indexed as a single string
}'
  • 1
  • 2
  • 3
  • 4
  • 5

For more facet functions, adding facet functions to each facet bucket, or sorting by facet function, see Solr Facet Functions


Aggregation Functions
Faceting involves breaking up the domain into multiple buckets and providing information about each bucket.
There are multiple aggregation functions / statistics that can be used:

Aggregation Example Effect
sum sum(sales) summation of numeric values
avg avg(popularity) average of numeric values
sumsq sumsq(rent) sum of squares
min min(salary) minimum value
max max(mul(price,popularity)) maximum value
unique unique(state) number of unique values (count distinct)
hll hll(state) number of unique values using the HyperLogLog algorithm
percentile percentile(salary,50,75,99,99.9)    calculates percentiles

下面是我写的一个例子

curl http://192.168.1.1:8080/solr/xxshard/query?q=*:* -d '
json.facet={
x:"unique(RB040002)"
}'
  • 1
  • 2
  • 3
  • 4

详细用法及其他功能在下面原文中

http://yonik.com/solr-count-distinct/
  http://yonik.com/solr-facet-functions/

Solr进行Distinct 获取Count的更多相关文章

  1. js在数组arr中随机获取count数量的元素

    // 在数组arr中随机获取count数量的元素; const getRandomArrayElements = (arr, num) => { // 新建一个数组,将传入的数组复制过来,用于运 ...

  2. [转]LINQ语句之Select/Distinct和Count/Sum/Min/Max/Avg

    在讲述了LINQ,顺便说了一下Where操作,这篇开始我们继续说LINQ语句,目的让大家从语句的角度了解LINQ,LINQ包括LINQ to Objects.LINQ to DataSets.LINQ ...

  3. Linq to SQL -- Select、Distinct和Count、Sum、Min、Max、Avg

    Select/Distinct操作符 适用场景:o(∩_∩)o… 查询呗. 说明:和SQL命令中的select作用相似但位置不同,查询表达式中的select及所接子句是放在表达式最后并把子句中的变量也 ...

  4. LINQ to SQL语句之Select/Distinct和Count/Sum/Min/Max/Avg (转)

    Select/Distinct操作符 适用场景:o(∩_∩)o… 查询呗. 说明:和SQL命令中的select作用相似但位置不同,查询表达式中的select及所接子句是放在表达式最后并把子句中的变量也 ...

  5. Linq-语句之Select/Distinct和Count/Sum/Min/Max/Avg

    上一篇讲述了LINQ,顺便说了一下Where操作,这篇开始我们继续说LINQ to SQL语句,目的让大家从语句的角度了解LINQ,LINQ包括LINQ to Objects.LINQ to Data ...

  6. (转)可以一同使用 DISTINCT 和 COUNT 关键词,来计算非重复结果的数目。

    点击收看

  7. solr与.net系列课程(四)solr查询参数的讲解与.net如何获取solr数据

    solr与.net系列课程(四)solr查询参数的讲解与.net如何获取solr数据 上一节我们完成了solr连接数据库,细心的朋友会发现一个问题,就是solr其实和语言没有任何关系,配置完成后任何语 ...

  8. MySQL 里面的Where 和Having和Count 和distinct和Group By对比

    mysql> select accid as uid,date(datetime) AS datetime from game.logLogin GROUP BY accid HAVING da ...

  9. Sql优化(二) 快速计算Distinct Count

    原创文章,始发自本人个人博客站点,转载请务必注明出自http://www.jasongj.com 个人博客上本文链接http://www.jasongj.com/2015/03/15/count_di ...

随机推荐

  1. Stub学习

    RPC RPC(Remote Procedure Call)就是某台主机A(一般为client)像调用本地的过程一样去调用另一台主机B(一般为server)上的某个过程.RPC代码可能长成这个样子: ...

  2. Tp5,Service使用

    C层,操控数据库,并处理页面数据展示. M层,纯粹的操作自己所对应的数据库. Service层,可以通用的处理一些逻辑计算,也可以将复杂的数据表处理整合到一起,也可以将复杂的业务逻辑整合到一起. 创建 ...

  3. Java回顾之多线程同步

    在这篇文章里,我们关注线程同步的话题.这是比多线程更复杂,稍不留意,我们就会“掉到坑里”,而且和单线程程序不同,多线程的错误是否每次都出现,也是不固定的,这给调试也带来了很大的挑战. 在这篇文章里,我 ...

  4. Missing artifact com.github.pagehelper:pagehelper:jar:3.4.2-fix的解决方法

    使用pagehelper.3.4.2.jar时报错,应该是无法从网络上下载该jar. 我的解决方案是: 从网络上下载一个pagehelper.3.4.2.jar包,然后复制到.m2目录中 如我的目录是 ...

  5. IOS-网络(小文件下载)

    // // ViewController.m // IOS_0131_小文件下载 // // Created by ma c on 16/1/31. // Copyright © 2016年 博文科技 ...

  6. 012PHP文件处理——copy rename file set_include_path

    <?php //copy rename file set_include_path /*file() 以行为单位返回数组 * */ /*$arr=file('b.txt'); foreach ( ...

  7. Java——三大特性

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  8. 原创:形象的讲解angular中的$q与promise

    promise不是angular首创的,作为一种编程模式,它出现在……1976年,比js还要古老得多.promise全称是 Futures and promises.具体的可以参见 http://en ...

  9. L175 Endorestiform Nucleus: Scientist Just Discovered a New Part of the Human Brain

    The newly named Endorestiform Nucleus sits in the inferior cerebellar小脑 peduncle, at the junction be ...

  10. New Concept English Two 13 31

    $课文29 出租汽车 294. Captain Ben Fawcett has bought an unusual taxi and has begun a new service. 本.弗西特机长买 ...