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 ...
随机推荐
- shell 判断文件是否存在,没有则创建
没有该文件则创建,有则 ls -l 输出文件信息. #!/bin/bash echo "enter the name:" read filename if test -e $fil ...
- h.264_javascript_资料
1. 用ffmpeg制作推流工具,实现推流系统声音和桌面到rtmp服务器-CSDN论坛-CSDN.NET-中国最大的IT技术社区.html http://bbs.csdn.net/topics/392 ...
- 递归--练习3--noi7592求最大公约数问题
递归--练习3--noi7592求最大公约数问题 一.心得 两个低级错误:1. ll setMax(ll &m,ll &n)中无引用,结果只传值,没传地址2. return f(n,m ...
- JAVA异常处理分析高级进界(下)
既然Throwable是异常处理机制的核心,那么,我们就来分析下它的源码来看看它是如何实现的. 进行分析前,我们可以先想想如果让我们实现一个异常处理机制,我们需要它做什么? 1. 发生异常终止程序执行 ...
- cf812B 搜索
B. Sagheer, the Hausmeister time limit per test 1 second memory limit per test 256 megabytes input s ...
- wikioi 1021 玛丽卡
链接:http://wikioi.com/problem/1021/ 这题挺有意思的,虽然比较水,但是让我想起来那次百度or腾讯的一道最大流的题目,很给力,也是对最后找边进行优化,不过这题比那题简单多 ...
- rxjava 调用retrofit执行网络请求的过程
retrofit流程图 -1.RxJava调用Retrofit,从requestGtPushSaeUserInfo()中获得被观察者observable,然后new一个观察者向它订阅 0.从业务中 ...
- Centos7 安装JDK环境和Tomcat
Linux JDK 64位下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk9-downloads-3848520.ht ...
- sklearn 可视化模型的训练测试收敛情况和特征重要性
show the code: # Plot training deviance def plot_training_deviance(clf, n_estimators, X_test, y_test ...
- C++ int转string(stringstream可转更多类型)
一.使用atoi 说明: itoa( int value, char *string, int radix ); 第一个参数:你要转化的int; 第二个 ...