Files
life-echo/api/.agents/skills/redis-development/rules/rqe-index-creation.md
Sully 53e0065e3e refactor(api): TOML 配置 SSOT、统一错误契约、Auth/事务加固与可观测性 (#33)
配置 SSOT(TOML + .env)
统一错误契约
Auth 与事务边界
Redis / Celery 可靠性:业务 Redis(DB/0)与 Celery broker/backend(DB/1)显式拆分;连接池、sync client
可观测性(OpenTelemetry + LGTM)
2026-05-22 13:44:50 +08:00

74 lines
1.8 KiB
Markdown

---
title: Index Only Fields You Query
impact: HIGH
impactDescription: Reduces index size and improves write performance
tags: rqe, ft.create, index, schema
description: Index Only Fields You Query
alwaysApply: true
---
## Index Only Fields You Query
Create indexes with only the fields you need to search, filter, or sort on.
**Correct:** Index specific fields and use prefixes.
```
FT.CREATE idx:products ON HASH PREFIX 1 product:
SCHEMA
name TEXT WEIGHT 2.0
description TEXT
category TAG SORTABLE
price NUMERIC SORTABLE
location GEO
```
**Java** (Jedis):
```java
import redis.clients.jedis.search.*;
Schema schema = new Schema()
.addTextField("name", 1)
.addTagField("categories");
// Good: Specify prefix to index only matching keys
IndexDefinition def = new IndexDefinition(IndexDefinition.Type.HASH)
.setPrefixes("person:");
jedis.ftCreate("idx", IndexOptions.defaultOptions().setDefinition(def), schema);
```
**Incorrect:** Over-indexing or indexing unused fields.
```
# Bad: Indexing every field "just in case"
FT.CREATE idx:products ON HASH PREFIX 1 product:
SCHEMA
name TEXT
description TEXT
category TEXT
subcategory TEXT
brand TEXT
sku TEXT
price NUMERIC
cost NUMERIC
margin NUMERIC
...
```
**Java** (Jedis):
```java
// Bad: No prefix means all hashes get indexed
IndexDefinition def = new IndexDefinition(IndexDefinition.Type.HASH);
// This will index every hash in the database!
```
**Tips:**
- Start with the minimum required fields
- Add fields as query patterns emerge
- Use `FT.INFO` to monitor index size
- Always specify a prefix to avoid indexing unrelated keys
Reference: [Redis Search Indexing](https://redis.io/docs/latest/develop/interact/search-and-query/indexing/)