You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何高效获取Marqo索引中的文档总数?

在Marqo中高效获取索引文档数量的方法

你之前用搜索接口计数的方式存在明显局限——当文档数超过设置的limit值时,无法获取准确的总数。Marqo提供了更直接高效的方案:使用索引对象的get_stats()接口,它能返回索引的完整统计数据,其中就包含准确的文档总数。

示例代码

import marqo

mq = marqo.Client(url='http://localhost:8882')

# 获取第一个索引的文档数量
first_stats = mq.index("first-index").get_stats()
print(f"first-index 文档数: {first_stats['numberOfDocuments']}")

# 获取第二个索引的文档数量
second_stats = mq.index("second-index").get_stats()
print(f"second-index 文档数: {second_stats['numberOfDocuments']}")

说明

  • get_stats()方法返回的字典中,numberOfDocuments字段对应索引的真实文档总数,不受任何数量限制
  • 这个方法无需执行搜索操作,性能远优于通过搜索结果计数的方式,尤其适合大索引的文档数查询

内容的提问来源于stack exchange,提问作者Ndaman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 01:50:24