Solr进行Distinct 获取Count
今天碰到一个问题,数据之前入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的更多相关文章
- js在数组arr中随机获取count数量的元素
// 在数组arr中随机获取count数量的元素; const getRandomArrayElements = (arr, num) => { // 新建一个数组,将传入的数组复制过来,用于运 ...
- [转]LINQ语句之Select/Distinct和Count/Sum/Min/Max/Avg
在讲述了LINQ,顺便说了一下Where操作,这篇开始我们继续说LINQ语句,目的让大家从语句的角度了解LINQ,LINQ包括LINQ to Objects.LINQ to DataSets.LINQ ...
- Linq to SQL -- Select、Distinct和Count、Sum、Min、Max、Avg
Select/Distinct操作符 适用场景:o(∩_∩)o… 查询呗. 说明:和SQL命令中的select作用相似但位置不同,查询表达式中的select及所接子句是放在表达式最后并把子句中的变量也 ...
- LINQ to SQL语句之Select/Distinct和Count/Sum/Min/Max/Avg (转)
Select/Distinct操作符 适用场景:o(∩_∩)o… 查询呗. 说明:和SQL命令中的select作用相似但位置不同,查询表达式中的select及所接子句是放在表达式最后并把子句中的变量也 ...
- Linq-语句之Select/Distinct和Count/Sum/Min/Max/Avg
上一篇讲述了LINQ,顺便说了一下Where操作,这篇开始我们继续说LINQ to SQL语句,目的让大家从语句的角度了解LINQ,LINQ包括LINQ to Objects.LINQ to Data ...
- (转)可以一同使用 DISTINCT 和 COUNT 关键词,来计算非重复结果的数目。
点击收看
- solr与.net系列课程(四)solr查询参数的讲解与.net如何获取solr数据
solr与.net系列课程(四)solr查询参数的讲解与.net如何获取solr数据 上一节我们完成了solr连接数据库,细心的朋友会发现一个问题,就是solr其实和语言没有任何关系,配置完成后任何语 ...
- MySQL 里面的Where 和Having和Count 和distinct和Group By对比
mysql> select accid as uid,date(datetime) AS datetime from game.logLogin GROUP BY accid HAVING da ...
- Sql优化(二) 快速计算Distinct Count
原创文章,始发自本人个人博客站点,转载请务必注明出自http://www.jasongj.com 个人博客上本文链接http://www.jasongj.com/2015/03/15/count_di ...
随机推荐
- Bigtable阶段性总结(版本1)
Bigtable的角色:为大规模的结构化数据提供高效的存储.管理与查询. Bigtable的针对性: 大规模数据需要大规模集群支持,带来了存储.管理.查询.容错上的复杂性. 关系型数据库在数据规模较大 ...
- python 浮点数转分数
from fractions import Fraction value = 4.2 print(Fraction(value).limit_denominator())
- 网络TCP建立连接为什么需要三次握手而结束要四次
忽然顿悟了,不管三次握手,还是四次握手,这是保证信息来回两个链路可达(也就是信息能从A到B,也能从B到A)的最低要求.-2018-9-17-晚上九点 举个打电话的例子: A : 你好我是A,你听得到我 ...
- C#MVC中@HTML中的方法
//生成表单 @{ Html.BeginForm("Index", "Simple", FormMethod.Post, new { id = "my ...
- bzoj2152: 聪聪可可 树分治
sb树分治 /************************************************************** Problem: 2152 User: walfy Lang ...
- springboot 用mybatis-generator自动生成bean和dao
1.在pom.xml里添加maven插件 <plugin> <groupId>org.mybatis.generator</groupId> <artifac ...
- 返回值为 Record类型的函数 初始化 Result
function TMiTeC_Storage.GetPhysInfo(Index: integer): TDeviceInfo; begin Finalize(Result); FillChar(R ...
- 设置套接口的选项setsockopt的用法
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- Internet Explorer 11.0.9600.16428 For Windows 7
官方消息http://blogs.msdn.com/b/ie/archive/2013/11/07/ie11-for-windows-7-globally-available-for-consumer ...
- Leetcode 1023. Camelcase Matching
暴力查找 class Solution: def camelMatch(self, queries: List[str], pattern: str) -> List[bool]: q_size ...