@gordon.gan/specflow 1.4.0-beta → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +1 -1
- package/prompts/approval/database-guidance.md +79 -0
- package/prompts/approval/generate.md +569 -290
- package/skills/database/LICENSE +405 -0
- package/skills/database/ORIGIN.md +6 -0
- package/skills/database/README.md +30 -0
- package/skills/database/elasticsearch/LICENSE.txt +202 -0
- package/skills/database/elasticsearch/SKILL.md +199 -0
- package/skills/database/elasticsearch/examples/01-fulltext-search.md +215 -0
- package/skills/database/elasticsearch/examples/02-aggregation-report.md +206 -0
- package/skills/database/elasticsearch/examples/03-reindex-zero-downtime.md +200 -0
- package/skills/database/elasticsearch/examples/04-cluster-monitoring.md +204 -0
- package/skills/database/elasticsearch/references/01-query-dsl-fulltext.md +162 -0
- package/skills/database/elasticsearch/references/02-query-dsl-term.md +210 -0
- package/skills/database/elasticsearch/references/03-aggregations-metric.md +161 -0
- package/skills/database/elasticsearch/references/04-aggregations-bucket.md +236 -0
- package/skills/database/elasticsearch/references/05-mapping-types.md +134 -0
- package/skills/database/elasticsearch/references/06-analyzers.md +187 -0
- package/skills/database/elasticsearch/references/07-cluster-ops.md +225 -0
- package/skills/database/elasticsearch/references/08-elk-integration.md +170 -0
- package/skills/database/mysql/SKILL.md +195 -0
- package/skills/database/mysql/examples/01-connection-pool.md +75 -0
- package/skills/database/mysql/examples/02-slow-query-optimization.md +98 -0
- package/skills/database/mysql/examples/03-master-slave-setup.md +144 -0
- package/skills/database/mysql/examples/04-backup-strategy.md +212 -0
- package/skills/database/mysql/references/01-functions-string.md +103 -0
- package/skills/database/mysql/references/02-functions-date.md +152 -0
- package/skills/database/mysql/references/03-functions-aggregate-window.md +167 -0
- package/skills/database/mysql/references/04-functions-json.md +129 -0
- package/skills/database/mysql/references/05-sql-ddl-types.md +235 -0
- package/skills/database/mysql/references/06-index-optimization.md +232 -0
- package/skills/database/mysql/references/07-replication-ha.md +213 -0
- package/skills/database/mysql/references/08-backup-restore.md +207 -0
- package/skills/database/mysql/references/09-advanced-features.md +345 -0
- package/skills/database/oracle/LICENSE.txt +202 -0
- package/skills/database/oracle/SKILL.md +238 -0
- package/skills/database/oracle/examples/01-plsql-procedure.md +90 -0
- package/skills/database/oracle/examples/02-awr-analysis.md +99 -0
- package/skills/database/oracle/examples/03-rman-backup.md +108 -0
- package/skills/database/oracle/examples/04-dataguard-setup.md +146 -0
- package/skills/database/oracle/references/01-functions-string.md +91 -0
- package/skills/database/oracle/references/02-functions-date.md +71 -0
- package/skills/database/oracle/references/03-analytic-functions.md +103 -0
- package/skills/database/oracle/references/04-plsql-guide.md +303 -0
- package/skills/database/oracle/references/05-performance-tuning.md +164 -0
- package/skills/database/oracle/references/06-backup-recovery.md +115 -0
- package/skills/database/oracle/references/07-dataguard-rac.md +76 -0
- package/skills/database/oracle/references/08-security.md +170 -0
- package/skills/database/oracle/references/09-sql-syntax.md +152 -0
- package/skills/database/oracle/references/10-features.md +174 -0
- package/skills/database/postgresql/LICENSE.txt +202 -0
- package/skills/database/postgresql/SKILL.md +182 -0
- package/skills/database/postgresql/examples/.gitkeep +0 -0
- package/skills/database/postgresql/examples/01-jsonb-query.md +72 -0
- package/skills/database/postgresql/examples/02-cte-recursive.md +110 -0
- package/skills/database/postgresql/examples/03-performance-tuning.md +114 -0
- package/skills/database/postgresql/examples/04-streaming-replication.md +113 -0
- package/skills/database/postgresql/references/.gitkeep +0 -0
- package/skills/database/postgresql/references/01-functions-string.md +174 -0
- package/skills/database/postgresql/references/02-functions-datetime.md +54 -0
- package/skills/database/postgresql/references/03-functions-aggregate-window.md +142 -0
- package/skills/database/postgresql/references/04-functions-jsonb.md +117 -0
- package/skills/database/postgresql/references/05-fulltext-search.md +109 -0
- package/skills/database/postgresql/references/06-index-types.md +95 -0
- package/skills/database/postgresql/references/07-partition-fdw.md +133 -0
- package/skills/database/postgresql/references/08-replication-backup.md +215 -0
- package/skills/database/redis/LICENSE.txt +202 -0
- package/skills/database/redis/SKILL.md +922 -0
- package/skills/database/redis/examples/01-cache-usage.md +104 -0
- package/skills/database/redis/examples/02-session-storage.md +72 -0
- package/skills/database/redis/examples/03-leaderboard.md +63 -0
- package/skills/database/redis/examples/04-redis-cluster-setup.md +70 -0
- package/skills/database/redis/examples/05-stream-queue.md +65 -0
- package/skills/database/redis/references/command-quick-ref.md +180 -0
- package/skills/database/redis/references/commands-admin-key.md +413 -0
- package/skills/database/redis/references/commands-set-sorted-advanced.md +539 -0
- package/skills/database/redis/references/commands-string-hash-list.md +458 -0
- package/skills/database/redis/references/memory-optimization.md +150 -0
- package/skills/database/redis/references/redis-conf-production.md +139 -0
- package/skills/specflow-approval/SKILL.md +100 -181
- package/templates/approval.md +295 -221
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# 全文查询 (Full Text Queries)
|
|
2
|
+
|
|
3
|
+
全文查询会对查询文本进行分词,然后搜索倒排索引。这些查询会计算相关性分数 (`_score`)。
|
|
4
|
+
|
|
5
|
+
## match — 标准全文匹配
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# 基本 match (默认 OR 逻辑)
|
|
9
|
+
GET /products/_search
|
|
10
|
+
{
|
|
11
|
+
"query": {
|
|
12
|
+
"match": {
|
|
13
|
+
"title": "apple macbook"
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
# AND 逻辑 (必须匹配所有词项)
|
|
19
|
+
GET /products/_search
|
|
20
|
+
{
|
|
21
|
+
"query": {
|
|
22
|
+
"match": {
|
|
23
|
+
"title": {
|
|
24
|
+
"query": "apple macbook",
|
|
25
|
+
"operator": "and"
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
# minimum_should_match (至少匹配 75% 词项)
|
|
32
|
+
GET /products/_search
|
|
33
|
+
{
|
|
34
|
+
"query": {
|
|
35
|
+
"match": {
|
|
36
|
+
"title": {
|
|
37
|
+
"query": "apple macbook pro",
|
|
38
|
+
"minimum_should_match": "75%"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
**业务场景**:商品搜索、文章搜索、文档搜索。用户输入关键词时自动分词匹配。
|
|
46
|
+
|
|
47
|
+
## match_phrase — 短语匹配
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
# 精确短语匹配 (顺序必须一致)
|
|
51
|
+
GET /products/_search
|
|
52
|
+
{
|
|
53
|
+
"query": {
|
|
54
|
+
"match_phrase": {
|
|
55
|
+
"title": "macbook pro"
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
# 带 slop 的短语匹配 (允许词项间插入 slop 个词)
|
|
61
|
+
GET /products/_search
|
|
62
|
+
{
|
|
63
|
+
"query": {
|
|
64
|
+
"match_phrase": {
|
|
65
|
+
"title": {
|
|
66
|
+
"query": "macbook pro",
|
|
67
|
+
"slop": 1
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
**业务场景**:搜索"红米手机"不希望匹配"红色小米手机";搜索完整书名/产品名。
|
|
75
|
+
|
|
76
|
+
## multi_match — 多字段匹配
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
# 同时在 title 和 description 中搜索
|
|
80
|
+
GET /products/_search
|
|
81
|
+
{
|
|
82
|
+
"query": {
|
|
83
|
+
"multi_match": {
|
|
84
|
+
"query": "轻薄笔记本",
|
|
85
|
+
"fields": ["title", "description"]
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
# 带权重 (title 字段权重 2 倍)
|
|
91
|
+
GET /products/_search
|
|
92
|
+
{
|
|
93
|
+
"query": {
|
|
94
|
+
"multi_match": {
|
|
95
|
+
"query": "轻薄笔记本",
|
|
96
|
+
"fields": ["title^2", "description", "tags^0.5"]
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
# type 说明:
|
|
102
|
+
# - best_fields (默认): 取最佳匹配字段分数
|
|
103
|
+
# - most_fields: 合并所有匹配字段分数
|
|
104
|
+
# - cross_fields: 将词项拆分到多字段 (适合姓名搜索)
|
|
105
|
+
GET /products/_search
|
|
106
|
+
{
|
|
107
|
+
"query": {
|
|
108
|
+
"multi_match": {
|
|
109
|
+
"query": "Will Smith",
|
|
110
|
+
"fields": ["first_name", "last_name"],
|
|
111
|
+
"type": "cross_fields"
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
**业务场景**:搜索框同时匹配标题、描述、标签等字段;人名搜索。
|
|
118
|
+
|
|
119
|
+
## query_string — 完整查询语法
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
# 支持 AND/OR/NOT, 通配符, 正则等
|
|
123
|
+
GET /products/_search
|
|
124
|
+
{
|
|
125
|
+
"query": {
|
|
126
|
+
"query_string": {
|
|
127
|
+
"query": "title:(apple AND macbook) AND price:[10000 TO 20000]",
|
|
128
|
+
"default_operator": "and"
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
# 支持短语和多字段
|
|
134
|
+
GET /products/_search
|
|
135
|
+
{
|
|
136
|
+
"query": {
|
|
137
|
+
"query_string": {
|
|
138
|
+
"query": "\"macbook pro\" OR \"iphone\"",
|
|
139
|
+
"fields": ["title^2", "description"]
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
**注意**:query_string 解析语法错误会抛出异常,生产环境推荐用 `simple_query_string`。
|
|
146
|
+
|
|
147
|
+
## simple_query_string — 安全版查询语法
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
GET /products/_search
|
|
151
|
+
{
|
|
152
|
+
"query": {
|
|
153
|
+
"simple_query_string": {
|
|
154
|
+
"query": "\"macbook pro\" +apple -samsung",
|
|
155
|
+
"fields": ["title", "description"],
|
|
156
|
+
"default_operator": "or"
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
**支持语法**:`+` (AND), `|` (OR), `-` (NOT), `"` (短语), `*` (前缀)。语法错误不会抛异常。
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
# 精确查询与复合查询
|
|
2
|
+
|
|
3
|
+
精确查询不会对搜索词进行分词,直接匹配倒排索引中的精确值。
|
|
4
|
+
|
|
5
|
+
## term / terms — 精确值匹配
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# 精确匹配 keyword 字段 (不要对 text 字段用 term!)
|
|
9
|
+
GET /products/_search
|
|
10
|
+
{
|
|
11
|
+
"query": {
|
|
12
|
+
"term": {
|
|
13
|
+
"tags": "electronics"
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
# 多值匹配
|
|
19
|
+
GET /products/_search
|
|
20
|
+
{
|
|
21
|
+
"query": {
|
|
22
|
+
"terms": {
|
|
23
|
+
"tags": ["electronics", "laptop"]
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
# ids 查询
|
|
29
|
+
GET /products/_search
|
|
30
|
+
{
|
|
31
|
+
"query": {
|
|
32
|
+
"ids": {
|
|
33
|
+
"values": ["1", "2", "3"]
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
**⚠️ 常见陷阱**:`term` 查询对 `text` 字段几乎永远不匹配,因为 text 字段被分词后存储的是词项而非完整内容。
|
|
40
|
+
|
|
41
|
+
## range — 范围查询
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
# 数值范围
|
|
45
|
+
GET /products/_search
|
|
46
|
+
{
|
|
47
|
+
"query": {
|
|
48
|
+
"range": {
|
|
49
|
+
"price": { "gte": 10000, "lte": 20000 }
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
# 日期范围 (支持日期数学)
|
|
55
|
+
GET /products/_search
|
|
56
|
+
{
|
|
57
|
+
"query": {
|
|
58
|
+
"range": {
|
|
59
|
+
"created_at": { "gte": "now-7d/d", "lte": "now" }
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
# 日期数学: now-1h, now+1d, now/d, now-1M/M, 2024-01-01||+1y
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## exists — 存在性检查
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
# 查找有 description 字段的文档
|
|
71
|
+
GET /products/_search
|
|
72
|
+
{
|
|
73
|
+
"query": {
|
|
74
|
+
"exists": { "field": "description" }
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
# 查找没有 description 的文档 (using must_not)
|
|
79
|
+
GET /products/_search
|
|
80
|
+
{
|
|
81
|
+
"query": {
|
|
82
|
+
"bool": {
|
|
83
|
+
"must_not": [
|
|
84
|
+
{ "exists": { "field": "description" } }
|
|
85
|
+
]
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## bool 查询 — 最常用的组合查询
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
# bool 结构: must (AND+算分), filter (AND+缓存), should (OR), must_not (NOT)
|
|
95
|
+
GET /products/_search
|
|
96
|
+
{
|
|
97
|
+
"query": {
|
|
98
|
+
"bool": {
|
|
99
|
+
"must": [
|
|
100
|
+
{ "match": { "title": "轻薄笔记本" } }
|
|
101
|
+
],
|
|
102
|
+
"filter": [
|
|
103
|
+
{ "term": { "status": "active" } },
|
|
104
|
+
{ "range": { "price": { "gte": 3000, "lte": 8000 } } }
|
|
105
|
+
],
|
|
106
|
+
"should": [
|
|
107
|
+
{ "match": { "description": "轻薄" } }
|
|
108
|
+
],
|
|
109
|
+
"minimum_should_match": 1
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
**filter vs query 选择**:
|
|
116
|
+
- filter:精确匹配、范围过滤 → 可缓存,不贡献算分
|
|
117
|
+
- query:全文搜索 → 贡献算分,不可缓存
|
|
118
|
+
|
|
119
|
+
## nested — 嵌套对象查询
|
|
120
|
+
|
|
121
|
+
```json
|
|
122
|
+
// mapping 定义
|
|
123
|
+
PUT /orders
|
|
124
|
+
{
|
|
125
|
+
"mappings": {
|
|
126
|
+
"properties": {
|
|
127
|
+
"items": {
|
|
128
|
+
"type": "nested",
|
|
129
|
+
"properties": {
|
|
130
|
+
"product_id": { "type": "keyword" },
|
|
131
|
+
"quantity": { "type": "integer" },
|
|
132
|
+
"price": { "type": "float" }
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
```bash
|
|
141
|
+
# nested 查询 (保证跨字段关联正确)
|
|
142
|
+
GET /orders/_search
|
|
143
|
+
{
|
|
144
|
+
"query": {
|
|
145
|
+
"nested": {
|
|
146
|
+
"path": "items",
|
|
147
|
+
"query": {
|
|
148
|
+
"bool": {
|
|
149
|
+
"must": [
|
|
150
|
+
{ "term": { "items.product_id": "p100" } },
|
|
151
|
+
{ "range": { "items.quantity": { "gte": 2 } } }
|
|
152
|
+
]
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## 其他特殊查询
|
|
161
|
+
|
|
162
|
+
```bash
|
|
163
|
+
# fuzzy — 模糊纠错
|
|
164
|
+
GET /products/_search
|
|
165
|
+
{
|
|
166
|
+
"query": {
|
|
167
|
+
"fuzzy": {
|
|
168
|
+
"title": {
|
|
169
|
+
"value": "macbok",
|
|
170
|
+
"fuzziness": "AUTO",
|
|
171
|
+
"prefix_length": 2,
|
|
172
|
+
"transpositions": true
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
# "macbok" → 匹配 "macbook"
|
|
178
|
+
|
|
179
|
+
# wildcard — 通配符 (性能差, 谨慎使用)
|
|
180
|
+
GET /products/_search
|
|
181
|
+
{
|
|
182
|
+
"query": {
|
|
183
|
+
"wildcard": {
|
|
184
|
+
"title.keyword": "Mac*"
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
# regexp — 正则查询 (性能开销大)
|
|
190
|
+
GET /products/_search
|
|
191
|
+
{
|
|
192
|
+
"query": {
|
|
193
|
+
"regexp": {
|
|
194
|
+
"title.keyword": "Macbook\\s(Pro|Air)"
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
# boosting — 权重控制 (降权不排除)
|
|
200
|
+
GET /products/_search
|
|
201
|
+
{
|
|
202
|
+
"query": {
|
|
203
|
+
"boosting": {
|
|
204
|
+
"positive": { "match": { "title": "手机" } },
|
|
205
|
+
"negative": { "term": { "status": "discontinued" } },
|
|
206
|
+
"negative_boost": 0.2
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
```
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# 指标聚合 (Metric Aggregations)
|
|
2
|
+
|
|
3
|
+
指标聚合对文档集中的某个字段进行数值计算,类似于 SQL 聚合函数。
|
|
4
|
+
|
|
5
|
+
## 基本结构
|
|
6
|
+
|
|
7
|
+
```json
|
|
8
|
+
{
|
|
9
|
+
"size": 0,
|
|
10
|
+
"aggs": {
|
|
11
|
+
"my_agg_name": { // 自定义聚合名称
|
|
12
|
+
"avg": { // 聚合类型
|
|
13
|
+
"field": "price"
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## 常用指标聚合
|
|
21
|
+
|
|
22
|
+
### avg — 平均值
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
GET /products/_search
|
|
26
|
+
{
|
|
27
|
+
"size": 0,
|
|
28
|
+
"aggs": {
|
|
29
|
+
"avg_price": {
|
|
30
|
+
"avg": { "field": "price" }
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### sum / min / max
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
GET /products/_search
|
|
40
|
+
{
|
|
41
|
+
"size": 0,
|
|
42
|
+
"aggs": {
|
|
43
|
+
"total_revenue": { "sum": { "field": "price" } },
|
|
44
|
+
"min_price": { "min": { "field": "price" } },
|
|
45
|
+
"max_price": { "max": { "field": "price" } }
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### stats — 批量统计
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
# 一次性返回 count/min/max/avg/sum
|
|
54
|
+
GET /products/_search
|
|
55
|
+
{
|
|
56
|
+
"size": 0,
|
|
57
|
+
"aggs": {
|
|
58
|
+
"price_stats": {
|
|
59
|
+
"stats": { "field": "price" }
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### extended_stats — 扩展统计
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
# 额外返回方差、标准差、总和平方等
|
|
69
|
+
GET /products/_search
|
|
70
|
+
{
|
|
71
|
+
"size": 0,
|
|
72
|
+
"aggs": {
|
|
73
|
+
"price_extended": {
|
|
74
|
+
"extended_stats": { "field": "price" }
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### cardinality — 去重计数 (COUNT DISTINCT)
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
GET /products/_search
|
|
84
|
+
{
|
|
85
|
+
"size": 0,
|
|
86
|
+
"aggs": {
|
|
87
|
+
"unique_brands": {
|
|
88
|
+
"cardinality": {
|
|
89
|
+
"field": "brand",
|
|
90
|
+
"precision_threshold": 100 // 精度阈值 (默认 3000)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
**说明**:cardinality 基于 HyperLogLog++ 算法,近似去重。`precision_threshold` 越高越精确,但消耗更多内存。
|
|
98
|
+
|
|
99
|
+
### value_count — 非空值计数
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
GET /products/_search
|
|
103
|
+
{
|
|
104
|
+
"size": 0,
|
|
105
|
+
"aggs": {
|
|
106
|
+
"by_brand": {
|
|
107
|
+
"terms": { "field": "brand" },
|
|
108
|
+
"aggs": {
|
|
109
|
+
"price_count": { "value_count": { "field": "price" } }
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
通常作为子聚合,用于计算每个桶的样本数。
|
|
117
|
+
|
|
118
|
+
### percentiles — 百分位
|
|
119
|
+
|
|
120
|
+
```bash
|
|
121
|
+
GET /products/_search
|
|
122
|
+
{
|
|
123
|
+
"size": 0,
|
|
124
|
+
"aggs": {
|
|
125
|
+
"price_percentiles": {
|
|
126
|
+
"percentiles": {
|
|
127
|
+
"field": "price",
|
|
128
|
+
"percents": [1, 5, 25, 50, 75, 95, 99]
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### percentile_ranks — 百分位排名
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
# 值落在指定阈值内的百分比
|
|
139
|
+
GET /products/_search
|
|
140
|
+
{
|
|
141
|
+
"size": 0,
|
|
142
|
+
"aggs": {
|
|
143
|
+
"price_ranks": {
|
|
144
|
+
"percentile_ranks": {
|
|
145
|
+
"field": "price",
|
|
146
|
+
"values": [5000, 10000]
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## 业务场景速查
|
|
154
|
+
|
|
155
|
+
| 聚合 | 典型场景 |
|
|
156
|
+
|------|---------|
|
|
157
|
+
| avg | 商品均价、平均评分、平均响应时间 |
|
|
158
|
+
| sum | 总销售额、总库存量、总访问量 |
|
|
159
|
+
| stats | 批量统计摘要(报表概览) |
|
|
160
|
+
| cardinality | 独立访客 UV、去重品牌数、唯一 IP 数 |
|
|
161
|
+
| percentiles | 价格分布分析、APM P99 延迟、收入分布 |
|