今天碰到一个问题,数据之前入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. vim 安装vim-airline

    在.vimrc中添加 Plugin 'vim-airline/vim-airline' Plugin 'vim-airline/vim-airline-themes' 然后打开vim编辑器执行 :Pl ...

  2. Windows服务程序_测试01

    1. #include <stdio.h> #include <Windows.h> #include <tchar.h> #include <process ...

  3. Qt532_WebKit_SSL问题

    1.打开网页"http://www.baidu.com",它会跳转至"https://www.baidu.com/",使用 SSL了,于是 WebView 出现 ...

  4. Java Spring-Bean

    2017-11-06 18:59:30 Bean初始化和销毁方法 配置初始化和销毁的方法:* init-method=”setup”* destroy-method=”teardown”执行销毁的时候 ...

  5. Spring Boot安装

    Spring引导依赖项groupId使用org.springframework.boot. 通常Maven POM文件将继承自spring-boot-starter-parent项目,并将依赖性声明为 ...

  6. 偶数求1/2+1/4+...+1/n奇数1/1+1/3+...+1/n

    题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n Scanner scanner = new Scanner(Sy ...

  7. UVA-11419 SAM I AM (最小点覆盖)

    题目大意:在一个n*m的网格中,有k个目标,现在可以任选一行或列消除在其上的所有目标,求出最少选择次数及选法. 题目分析:经典的最小点覆盖问题,并且输出一个最小点覆盖集.在求出最大匹配之后,以未覆盖的 ...

  8. windows使用git记录

    1.免密码clone远程服务器代码开启ssh 生成私钥公钥 命令:查看自己配置的邮箱 git config user.name git config user.email 生成密钥:邮箱填写上面查看出 ...

  9. MDI窗体及涉及到的相关问题

    MDI窗体是个什么东东尼?多文档窗体唠,相对的还有个SDI,就是单文档窗体.一般情况下我们会将软件的主窗体设置为MDI窗体.那么在主窗体的菜单部分调出来的其他窗体就是主窗体的子窗体了. (1)在主窗体 ...

  10. webapi 文件下载输出接口

    public async ask<HttpResponseMessage> GetGuidingPrincipleDownload(string name ) { bool status ...