前言

在使用ES搜索的时候,或多或少都会面临查询数据总量的情况,下面介绍三种查询数据总量的方式。

其中,方案二解决了当结果数据总量超过1w时,由于ES默认设置(max_result_window:10000,出于性能问题考虑,用户也不想放开这个限制),只能返回命中数等于1w的问题。

方案一

查询全部索引下的文档总数:

GET /_cat/count

查询某个索引下的文档总数(<target>为索引名):

GET /_cat/count/<target>

官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-count.html

方案二

将 track_total_hits" 属性设置为 true(<target>为索引名)

GET <target>/_search
{
"track_total_hits": true,
// 以下可以为任意检索条件
"query": {
"match_all" : {
}
}
}

官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-your-data.html

方案三(不推荐)

此命令是查询索引状态的,当然也包含了每个索引下的文档数量。但是在部分情况下,是不够准确的,因为这个数量包含了隐藏的嵌套文档。参考官方文档的解释:

These metrics are retrieved directly from Lucene, which Elasticsearch uses internally to power indexing and search. As a result, all document counts include hidden nested documents.

To get an accurate count of Elasticsearch documents, use the cat count or count APIs.

GET /_cat/indices
GET /_cat/indices/<target>

官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-indices.html

Elasticsearch查询文档总数的更多相关文章

  1. ES 07 - Elasticsearch查询文档的六种方法

    目录 1 Query String Search(查询串检索) 2 Query DSL(ES特定语法检索) 3 Query Filter(过滤检索) 4 Full Text Search(全文检索) ...

  2. ElasticSearch入门 第五篇:使用C#查询文档

    这是ElasticSearch 2.4 版本系列的第五篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...

  3. Elasticsearch 使用集群 - 创建和查询文档

    章节 Elasticsearch 基本概念 Elasticsearch 安装 Elasticsearch 使用集群 Elasticsearch 健康检查 Elasticsearch 列出索引 Elas ...

  4. ElasticSearch(2)-文档

    上一篇 ES(1) 官网原地址:https://www.elastic.co/guide/en/elasticsearch/reference/1.7/_cluster_health.html ES权 ...

  5. mongodb查询文档

    说到查询,我们一般就想起了关系型数据库的查询了,比如:order by(排序).limit(分页).范围查询(大于某个值,小于某个值..,in查询,on查询,like查询等待很多),同样mongodb ...

  6. elasticsearch 路由文档到分片

    路由文档到分片 当你索引一个文档,它被存储在单独一个主分片上.Elasticsearch是如何知道文档属于哪个分片的呢?当你创建一个新文档,它是如何知道是应该存储在分片1还是分片2上的呢? 进程不能是 ...

  7. elasticsearch——海量文档高性能索引系统

    elasticsearch elasticsearch是一个高性能高扩展性的索引系统,底层基于apache lucene. 可结合kibana工具进行可视化. 概念: index 索引: 类似SQL中 ...

  8. ElasticSearch——原始文档和倒排索引

    一.原始文档 如上图所示, 第二象限是一份原始文档,有title和content2个字段,字段取值分别为”我是中国人”和” 热爱共X产党”,这一点没什么可解释的.我们把原始文档写入Elasticsea ...

  9. Elasticsearch 删除文档

    章节 Elasticsearch 基本概念 Elasticsearch 安装 Elasticsearch 使用集群 Elasticsearch 健康检查 Elasticsearch 列出索引 Elas ...

随机推荐

  1. [bug] TypeError : unsupported operand type(s) for += : 'NoneType' and 'int'

    原因 Flask购物网站中,每点击货物一次,数据库中货物的浏览次数+1,默认浏览次数为NULL,故无法完成运算 解决 将数据库中相应字段默认值设为0,注意要先断开数据库连接

  2. 搞清楚 硬件环境 os环境 网络环境 搞清楚测试工具 测试步骤 自己搭测试环境 自测

    1,遇事的第一反应要从变化情绪转变为做出判断.判断什么?判断这一件事对自己是否重要,是否关乎我的个人利益,是否影响我的人际关系等等等等.如果答案都是否,那就没必要着急忙慌了.如果答案是是 冷静,其实是 ...

  3. zabbix添加菜单栏

    1.更改字体(中文乱码多半是因为字体不支持中文) define('ZBX_GRAPH_FONT_NAME', 'DejaVuSans'); // font file name define('ZBX_ ...

  4. Linux_rpm包管理

    一.rpm包命令规范 1.包的组成 主包:bind-9.7.1-1.el5.i586.rpm 子包:bind-libs-9.7.1-1.el5.i586.rpm bind-utils-9.7.1-1. ...

  5. maven build和push image中遇到的坑(学习过程记录)

    最近在做jenkins的持续集成构建,其中一项是要实现docker容器化部署.项目本身是maven项目,我对于maven和docker都没有什么认知基础,于是求助百度和官网,从头开始啃起.遇到了不少的 ...

  6. shell基础之for循环语句

    For语句 格式:for name [ [ in [ word ... ] ] ; ] do list ; done for 变量名 in 取值列表; do 命令 done 或者 for 变量名 in ...

  7. sprintf和snprintf函数

    printf()/sprintf()/snprintf()区别  先贴上其函数原型 printf( const char *format, ...)    格式化输出字符串,默认输出到终端-----s ...

  8. 第8章 Shell函数的知识与实践

    shell 函数常见的语法格式 function 函数名(){          return n } 简化1 function 函数名{     ... } 简化2 函数名(){     ... } ...

  9. Python保留指定位数的小数

    Python保留指定位数的小数 1 '%.2f' %f 方法(推荐) f = 1.23456 print('%.4f' % f) print('%.3f' % f) print('%.2f' % f) ...

  10. 降维-基于RDD的API

    降维-基于RDD的API Singular value decomposition (SVD) Performance SVD Example Principal component analysis ...