*Redis FT.INFO 命令
FT.INFO 用于返回指定索引的完整信息和统计信息,包括文档数量、字段定义、索引配置等。
*语法
FT.INFO index_name
*参数说明
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| index_name | String | 是 | 索引名称 |
*返回值
返回一个包含索引信息的数组,包含以下主要字段: - index_name: 索引名称 - index_options: 索引选项 - index_definition: 索引定义(keytype、prefixes、languagefield 等) - fields: 索引字段列表(每个字段包含 name、type、weight、sortable、noindex 等) - num_docs: 已索引文档数量 - maxdocid: 最大文档 ID - num_terms: 索引中的词项数量 - num_records: 索引记录数 - invertedszmb: 倒排索引大小(MB) - vectorindexsz_mb: 向量索引大小(MB) - totalinvertedindex_blocks: 倒排索引块总数 - offsetvectorssz_mb: 偏移向量大小(MB) - doctablesize_mb: 文档表大小(MB) - sortablevaluessize_mb: 可排序值大小(MB) - keytablesize_mb: 键表大小(MB) - recordsperdoc_avg: 每文档平均记录数 - bytesperrecord_avg: 每条记录平均字节数 - offsetsperterm_avg: 每个词项平均偏移量 - offsetbitsperrecordavg: 每条记录平均偏移位 - hashindexingfailures: 哈希索引失败次数 - indexing: 是否正在索引(0/1) - percent_indexed: 已索引百分比 - numberofuses: 索引使用次数 - gc_stats: GC 统计信息(已运行次数、已删除字节数等) - cursor_stats: 游标统计信息(全局游标数量、索引游标数量) - dialect_stats: 方言统计信息(Dialect 1、2、3、4 的使用次数)
*时间复杂度
O(1)
*
*示例
*基本用法
> FT.INFO myIndex
1) "index_name"
2) "myIndex"
3) "index_options"
4) 1) "NOHL"
2) "NOFREQS"
5) "index_definition"
6) 1) "key_type"
2) "HASH"
3) "prefixes"
4) 1) "doc:"
5) "language_field"
6) "__language"
7) "fields"
8) 1) 1) "title"
2) "type"
3) "TEXT"
4) "WEIGHT"
5) "1"
6) "SORTABLE"
7) "UNF"
2) 1) "content"
2) "type"
3) "TEXT"
4) "WEIGHT"
5) "1"
3) 1) "category"
2) "type"
3) "TAG"
4) "SEPARATOR"
5) ","
6) "SORTABLE"
7) "UNF"
9) "num_docs"
10) "1000"
11) "max_doc_id"
12) "999"
13) "num_terms"
14) "5000"
15) "num_records"
16) "20000"
17) "inverted_sz_mb"
18) "0.5"
19) "total_inverted_index_blocks"
20) "1000"
21) "offset_vectors_sz_mb"
22) "0.1"
23) "doc_table_size_mb"
24) "0.2"
25) "sortable_values_size_mb"
26) "0.1"
27) "key_table_size_mb"
28) "0.05"
29) "records_per_doc_avg"
30) "20"
31) "bytes_per_record_avg"
32) "2"
33) "offsets_per_term_avg"
34) "1.5"
35) "offset_bits_per_record_avg"
36) "8"
37) "hash_indexing_failures"
38) "0"
39) "indexing"
40) "0"
41) "percent_indexed"
42) "1"
43) "number_of_uses"
44) "5000"
45) "gc_stats"
46) 1) "bytes_collected"
2) "1048576"
3) "total_ms_run"
4) "1000"
5) "total_cycles"
6) "50"
47) "cursor_stats"
48) 1) "global_idle"
2) "0"
3) "global_total"
4) "0"
5) "index_capacity"
6) "128"
7) "index_total"
8) "0"
49) "dialect_stats"
50) 1) "dialect_1"
2) "3000"
3) "dialect_2"
4) "1500"
5) "dialect_3"
6) "500"
7) "dialect_4"
8) "0"
*检查索引文档数量
> FT.INFO myIndex | grep num_docs
"num_docs"
"1000"
*检查索引失败次数
> FT.INFO myIndex | grep hash_indexing_failures
"hash_indexing_failures"
"0"
*常见错误
| 错误 | 原因 | 解决 |
|---|---|---|
| Unknown index name | 索引不存在 | 使用 FT.CREATE 创建索引,或检查索引名称拼写 |
| ERR wrong number of arguments | 缺少索引名称 | 提供索引名称参数 |
*最佳实践
- 使用 FT.INFO 监控索引文档数量,确保索引同步正常
- 定期检查
hash_indexing_failures,若不为 0 需排查数据问题 - 监控
inverted_sz_mb和doc_table_size_mb,评估内存占用 - 查看
dialect_stats了解查询方言使用情况,规划迁移到 Dialect 2/3 - 在 RedisInsight 或监控脚本中定期采集 FT.INFO 信息
- 结合
percent_indexed判断索引是否完全构建完成
*FAQ
Q1: FT.INFO 返回的 num_docs 和实际 key 数量不一致?
A: FT.INFO 统计的是已索引文档,如果某些文档因字段类型不匹配或超出内存限制而未被索引,hash_indexing_failures 会增加,导致 num_docs 小于实际键数。
Q2: FT.INFO 是否会阻塞查询? A: 不会,FT.INFO 是只读操作,不会阻塞索引或查询。
Q3: 如何获取索引的字段定义?
A: 从 FT.INFO 返回值中查找 fields 部分,它包含每个字段的完整定义(类型、权重、是否可排序等)。
Q4: percent_indexed 小于 1 表示什么?
A: 表示索引尚未完全构建完成,通常发生在批量写入后,索引引擎正在后台处理新文档。等待一段时间后该值会变为 1。