@gordon.gan/specflow 1.4.0-beta → 1.4.2-beta

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.
Files changed (82) hide show
  1. package/README.md +1 -1
  2. package/package.json +1 -1
  3. package/prompts/approval/database-guidance.md +79 -0
  4. package/prompts/approval/generate.md +600 -290
  5. package/skills/database/LICENSE +405 -0
  6. package/skills/database/ORIGIN.md +6 -0
  7. package/skills/database/README.md +30 -0
  8. package/skills/database/elasticsearch/LICENSE.txt +202 -0
  9. package/skills/database/elasticsearch/SKILL.md +199 -0
  10. package/skills/database/elasticsearch/examples/01-fulltext-search.md +215 -0
  11. package/skills/database/elasticsearch/examples/02-aggregation-report.md +206 -0
  12. package/skills/database/elasticsearch/examples/03-reindex-zero-downtime.md +200 -0
  13. package/skills/database/elasticsearch/examples/04-cluster-monitoring.md +204 -0
  14. package/skills/database/elasticsearch/references/01-query-dsl-fulltext.md +162 -0
  15. package/skills/database/elasticsearch/references/02-query-dsl-term.md +210 -0
  16. package/skills/database/elasticsearch/references/03-aggregations-metric.md +161 -0
  17. package/skills/database/elasticsearch/references/04-aggregations-bucket.md +236 -0
  18. package/skills/database/elasticsearch/references/05-mapping-types.md +134 -0
  19. package/skills/database/elasticsearch/references/06-analyzers.md +187 -0
  20. package/skills/database/elasticsearch/references/07-cluster-ops.md +225 -0
  21. package/skills/database/elasticsearch/references/08-elk-integration.md +170 -0
  22. package/skills/database/mysql/SKILL.md +195 -0
  23. package/skills/database/mysql/examples/01-connection-pool.md +75 -0
  24. package/skills/database/mysql/examples/02-slow-query-optimization.md +98 -0
  25. package/skills/database/mysql/examples/03-master-slave-setup.md +144 -0
  26. package/skills/database/mysql/examples/04-backup-strategy.md +212 -0
  27. package/skills/database/mysql/references/01-functions-string.md +103 -0
  28. package/skills/database/mysql/references/02-functions-date.md +152 -0
  29. package/skills/database/mysql/references/03-functions-aggregate-window.md +167 -0
  30. package/skills/database/mysql/references/04-functions-json.md +129 -0
  31. package/skills/database/mysql/references/05-sql-ddl-types.md +235 -0
  32. package/skills/database/mysql/references/06-index-optimization.md +232 -0
  33. package/skills/database/mysql/references/07-replication-ha.md +213 -0
  34. package/skills/database/mysql/references/08-backup-restore.md +207 -0
  35. package/skills/database/mysql/references/09-advanced-features.md +345 -0
  36. package/skills/database/oracle/LICENSE.txt +202 -0
  37. package/skills/database/oracle/SKILL.md +238 -0
  38. package/skills/database/oracle/examples/01-plsql-procedure.md +90 -0
  39. package/skills/database/oracle/examples/02-awr-analysis.md +99 -0
  40. package/skills/database/oracle/examples/03-rman-backup.md +108 -0
  41. package/skills/database/oracle/examples/04-dataguard-setup.md +146 -0
  42. package/skills/database/oracle/references/01-functions-string.md +91 -0
  43. package/skills/database/oracle/references/02-functions-date.md +71 -0
  44. package/skills/database/oracle/references/03-analytic-functions.md +103 -0
  45. package/skills/database/oracle/references/04-plsql-guide.md +303 -0
  46. package/skills/database/oracle/references/05-performance-tuning.md +164 -0
  47. package/skills/database/oracle/references/06-backup-recovery.md +115 -0
  48. package/skills/database/oracle/references/07-dataguard-rac.md +76 -0
  49. package/skills/database/oracle/references/08-security.md +170 -0
  50. package/skills/database/oracle/references/09-sql-syntax.md +152 -0
  51. package/skills/database/oracle/references/10-features.md +174 -0
  52. package/skills/database/postgresql/LICENSE.txt +202 -0
  53. package/skills/database/postgresql/SKILL.md +182 -0
  54. package/skills/database/postgresql/examples/.gitkeep +0 -0
  55. package/skills/database/postgresql/examples/01-jsonb-query.md +72 -0
  56. package/skills/database/postgresql/examples/02-cte-recursive.md +110 -0
  57. package/skills/database/postgresql/examples/03-performance-tuning.md +114 -0
  58. package/skills/database/postgresql/examples/04-streaming-replication.md +113 -0
  59. package/skills/database/postgresql/references/.gitkeep +0 -0
  60. package/skills/database/postgresql/references/01-functions-string.md +174 -0
  61. package/skills/database/postgresql/references/02-functions-datetime.md +54 -0
  62. package/skills/database/postgresql/references/03-functions-aggregate-window.md +142 -0
  63. package/skills/database/postgresql/references/04-functions-jsonb.md +117 -0
  64. package/skills/database/postgresql/references/05-fulltext-search.md +109 -0
  65. package/skills/database/postgresql/references/06-index-types.md +95 -0
  66. package/skills/database/postgresql/references/07-partition-fdw.md +133 -0
  67. package/skills/database/postgresql/references/08-replication-backup.md +215 -0
  68. package/skills/database/redis/LICENSE.txt +202 -0
  69. package/skills/database/redis/SKILL.md +922 -0
  70. package/skills/database/redis/examples/01-cache-usage.md +104 -0
  71. package/skills/database/redis/examples/02-session-storage.md +72 -0
  72. package/skills/database/redis/examples/03-leaderboard.md +63 -0
  73. package/skills/database/redis/examples/04-redis-cluster-setup.md +70 -0
  74. package/skills/database/redis/examples/05-stream-queue.md +65 -0
  75. package/skills/database/redis/references/command-quick-ref.md +180 -0
  76. package/skills/database/redis/references/commands-admin-key.md +413 -0
  77. package/skills/database/redis/references/commands-set-sorted-advanced.md +539 -0
  78. package/skills/database/redis/references/commands-string-hash-list.md +458 -0
  79. package/skills/database/redis/references/memory-optimization.md +150 -0
  80. package/skills/database/redis/references/redis-conf-production.md +139 -0
  81. package/skills/specflow-approval/SKILL.md +102 -181
  82. package/templates/approval.md +321 -221
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,199 @@
1
+ ---
2
+ name: elasticsearch
3
+ description: Provides comprehensive guidance for Elasticsearch including indexing, mappings, query DSL, aggregations, analyzers, cluster management, and ELK Stack integration. Use when the user asks about Elasticsearch, needs to implement search functionality, work with Elasticsearch queries, or manage Elasticsearch clusters.
4
+ license: Complete terms in LICENSE.txt
5
+ ---
6
+
7
+ # Elasticsearch — 分布式搜索与分析引擎
8
+
9
+ Elasticsearch 是基于 Apache Lucene 的分布式搜索和分析引擎,提供近实时的全文搜索、结构化搜索、聚合分析、地理空间查询和向量搜索能力。
10
+
11
+ ## Workflow — 从数据到搜索的 5 步决策流程
12
+
13
+ ```
14
+ 遇到 ES 相关需求时按以下顺序决策:
15
+
16
+ Step 1: 明确场景
17
+ ├── 全文搜索? → Step 2
18
+ ├── 结构化数据分析 (日志/指标)? → Step 2
19
+ ├── 向量/KNN 搜索? → references/ 向量搜索
20
+ ├── 地理空间查询? → references/ 地理查询
21
+ ├── 集群运维/性能问题? → references/ 07-cluster-ops
22
+ └── 数据管道/日志采集? → references/ 08-elk-integration
23
+
24
+ Step 2: 设计索引 (Mapping + Analyzer)
25
+ ├── 确定字段类型 (text/keyword/date/geo/nested…)
26
+ ├── 选择分词器 (standard/ik/pinyin/自定义)
27
+ ├── 规划主分片数 (每个分片 20-50GB), 建后不可改
28
+ └── 设置别名用于零停机重建
29
+
30
+ Step 3: 写入数据
31
+ ├── Index API (单条), Bulk API (批量, 5-15MB/批)
32
+ ├── _update (部分更新), _update_by_query (条件更新)
33
+ └── Logstash/Filebeat (日志采集管道)
34
+
35
+ Step 4: 构建查询
36
+ ├── 全文搜索: match / multi_match / query_string
37
+ ├── 精确过滤: term / terms / range / exists / bool filter
38
+ ├── 复合查询: bool (must/should/filter/must_not)
39
+ ├── 聚合分析: terms + avg → references/ 聚合专题
40
+ └── 排序/分页: sort / search_after (深度分页) / scroll (导出)
41
+
42
+ Step 5: 持续优化
43
+ ├── 性能问题? → Profile API + 慢查询日志
44
+ ├── 数据增长? → ILM + Rollover 自动化 (references/ 07-cluster-ops)
45
+ ├── Mapping 变更? → Reindex + Alias 零停机 (examples/03)
46
+ └── 查询优化? → filter 优先 query, 避免 script, 限制 _source
47
+ ```
48
+
49
+ ## When to Use / When NOT to
50
+
51
+ | ✅ 使用 ES | ❌ 不要用 ES |
52
+ |-----------|-------------|
53
+ | **全文搜索** — 商品/文章/文档关键词搜索 | **复杂事务** — 需要 ACID/多表 JOIN, 选 PostgreSQL |
54
+ | **日志/指标分析** — ELK Stack 日志场景 | **键值缓存** — 简单 KV 查询, 选 Redis |
55
+ | **搜索型应用** — 电商/知识库/文档管理 | **强 Schema 约束** — 需要外键/触发器, 选 RDBMS |
56
+ | **聚合/仪表盘** — 实时统计 + Kibana 可视化 | **海量文档存储无需搜索** — 选 MongoDB |
57
+ | **地理空间查询** — 附近的人/POI/地理围栏 | **纯 OLAP 分析** — PB 级离线分析, 选 ClickHouse |
58
+ | **向量搜索** — 语义相似度/RAG 检索 | **消息队列** — 选 Kafka/Pulsar |
59
+
60
+ **核心原则**:Elasticsearch 是搜索服务器,不是关系型数据库的替代品。
61
+
62
+ ## Boundary — 能力边界
63
+
64
+ | ✅ 完全适用 | ⚠️ 有条件适用 | ❌ 不适用 |
65
+ |-----------|--------------|---------|
66
+ | 全文搜索、模糊搜索、相关性排序 | 强一致性(ES 是近实时,默认 1s refresh) | 代替关系型数据库做核心业务存储 |
67
+ | 日志/指标聚合分析 (Kibana) | 秒级以下数据可见性(需调 refresh_interval) | 复杂 JOIN 查询(ES 有有限 nested 支持)|
68
+ | 地理空间、自动补全、搜索建议 | 极高写入量(需调优线程池和批量写入) | ACID 事务保证 |
69
+ | 文档搜索、知识库、RAG 检索 | 50+ 节点大集群(需专用协调节点) | 存储二进制大文件(存 OSS 路径) |
70
+ | 时序数据 + ILM 滚动 | PB 级深度分页(需 search_after / PIT) | 强关联约束数据模型 |
71
+ | 向量搜索 (dense_vector + KNN) | 自定义分词器(需先测试分析效果) | 替代 Kafka 做消息队列 |
72
+
73
+ ## 核心概念速查
74
+
75
+ | 概念 | ES 术语 | 关系型 DB 类比 | 关键说明 | 深度参考 |
76
+ |------|---------|---------------|---------|---------|
77
+ | **Index** | 索引 | Table | 存储文档的逻辑命名空间,名称必须小写 | references/05 |
78
+ | **Document** | 文档 | Row | JSON 格式基本数据单元,不可变(update = delete+index) | — |
79
+ | **Shard** | 分片 | Partition | 水平切分单元,主分片数建后不可改,推荐 20-50GB/分片 | references/07 |
80
+ | **Replica** | 副本 | Replica | 冗余副本提供高可用和读扩展,可动态调整 | references/07 |
81
+ | **Mapping** | 映射 | Schema | 定义字段类型和分词配置,已有字段类型不可修改 | references/05 |
82
+ | **Analyzer** | 分词器 | — | text 字段必须配置,决定搜索质量 | references/06 |
83
+
84
+ ## 查询 DSL 速查
85
+
86
+ | 查询类别 | 核心查询 | 用途 | 深度参考 |
87
+ |---------|---------|------|---------|
88
+ | **全文查询** | match / match_phrase / multi_match / query_string | 文本分词搜索、短语匹配、多字段搜索 | references/01 |
89
+ | **精确查询** | term / terms / range / exists / ids | keyword 字段精确匹配、范围过滤、存在检查 | references/02 |
90
+ | **复合查询** | bool (must/filter/should/must_not) | 90% 搜索需求可用 bool 实现 | references/02 |
91
+ | **嵌套/父子** | nested / has_child / has_parent | 对象内跨字段关联、父子关系查询 | references/02 |
92
+ | **地理查询** | geo_distance / geo_bounding_box / geo_shape | 附近查询、矩形区域、复杂地理形状 | 见 geospatial 技能 |
93
+ | **向量查询** | knn 参数 / k-NN 插件 | 语义相似度、RAG 检索 | references/05 |
94
+ | **特殊查询** | fuzzy / wildcard / regexp / script / percolate | 模糊纠错、通配符、脚本、反向搜索 | references/02 |
95
+
96
+ ## 聚合速查
97
+
98
+ | 聚合类型 | 核心聚合 | 类似 SQL | 深度参考 |
99
+ |---------|---------|---------|---------|
100
+ | **指标聚合** | avg / sum / min / max / stats / cardinality / percentiles | AVG / SUM / COUNT(DISTINCT) / PERCENTILE | references/03 |
101
+ | **桶聚合** | terms / date_histogram / histogram / range / filters | GROUP BY / 日期分组 / 区间分组 / 过滤分组 | references/04 |
102
+ | **管道聚合** | derivative / moving_fn / bucket_script / bucket_selector | 环比 / 移动平均 / 子聚合计算 / HAVING | references/04 |
103
+
104
+ ## 集群运维概述
105
+
106
+ | 运维领域 | 关键要点 | 深度参考 |
107
+ |---------|---------|---------|
108
+ | **节点类型** | Master(3个专用) + Data(SSD) + Coordinating(大查询) | references/07 |
109
+ | **分片管理** | 分配/再平衡/reroute/延迟分配 | references/07 |
110
+ | **快照备份** | S3/FS/GCS 仓库, SLM 自动管理, 增量快照 | references/07 |
111
+ | **ILM 生命周期** | hot → warm → cold → frozen → delete 自动化 | references/07 |
112
+ | **监控命令** | _cluster/health, _cat/nodes, _cat/shards, hot_threads | references/07 |
113
+ | **安全** | RBAC 角色, 字段/文档级安全, TLS, API Key | references/07 |
114
+
115
+ ## Gotchas — 常见陷阱与反模式
116
+
117
+ | # | 陷阱 | 问题 | 解决方案 |
118
+ |---|------|------|---------|
119
+ | 1 | `term` 查询 `text` 字段 | text 被分词,找不到精确值 | 用 `match` 或 `.keyword` 子字段 |
120
+ | 2 | `from+size` 深度分页 | 越深越慢直至 OOM (限制 10000) | 深翻页用 `search_after`,导出用 `scroll`/PIT |
121
+ | 3 | 建索引后改主分片数 | 建后不可修改 | 提前规划,或重建索引 (reindex) |
122
+ | 4 | 所有字段用 `text` | 聚合/排序报错 | text + keyword 多字段 |
123
+ | 5 | 依赖动态映射 | 时间戳被识别为 long 等 | 生产环境显式 mapping |
124
+ | 6 | 嵌套对象用普通查询 | 跨字段关联条件误匹配 | 必须用 `nested` 查询 |
125
+ | 7 | 分片过多或过少 | 过多→管理开销大,过少→无法扩展 | 每个分片 20-50GB |
126
+ | 8 | 大批量写入不做优化 | 频繁 refresh 产生大量小段 | 关闭 refresh (-1), Bulk API, 副本=0 |
127
+ | 9 | 忽略 filter 缓存 | 重复计算,性能差 | 不需要算分的条件放 filter |
128
+ | 10 | wildcard/regexp 前缀搜索 | 不利用倒排索引,性能极差 | 用 `prefix` 或 edge_ngram |
129
+ | 11 | 脚本查询滥用 | 不可缓存,性能差,调试困难 | 用 ingest pipeline 预处理 |
130
+ | 12 | 集群角色不做分离 | Master 因 Data GC 失联 | 专用 3 个 Master 节点 |
131
+ | 13 | 日志索引无 ILM | 索引无限增长 | ILM 自动化滚动/压缩/删除 |
132
+ | 14 | 忽略 `ignore_above` | keyword 超长导致索引失败 | 设置 `ignore_above: 256` |
133
+ | 15 | 在 text 字段开 `fielddata` | 内存消耗大 | 用 `.keyword` 多字段替代 |
134
+
135
+ ## FAQ
136
+
137
+ **Q1: ES 是关系型数据库的替代品吗?**
138
+ 不是。ES 是搜索服务器,不支持 ACID 事务、外键、复杂 JOIN。正确架构:业务数据存 RDBMS,ES 做搜索和聚合。
139
+
140
+ **Q2: 数据写入后多久能查到?**
141
+ 近实时。写入先到 buffer,默认 1s refresh 后才可搜索。可调 `refresh_interval` 或加 `?refresh` 参数。
142
+
143
+ **Q3: text 和 keyword 字段区别?**
144
+ text:分词后索引,支持 match 搜索,不支持排序/聚合。keyword:完整值索引,支持 term 搜索、排序、聚合。
145
+
146
+ **Q4: 主分片数为什么不能修改?**
147
+ 路由规则 `hash(_id) % shards`,修改后已有数据无法定位。变更需 reindex。
148
+
149
+ **Q5: 如何选择分片数?**
150
+ 每个分片 20-50GB。500GB 原始数据 → 10-25 个主分片。每个节点 ≤25 分片/GB 堆内存。
151
+
152
+ **Q6: ES 为什么搜索快?**
153
+ 倒排索引:将每个词项映射到文档列表,查找直接定位。加分片并行 + filter 缓存。
154
+
155
+ **Q7: 聚合 (Aggregation) 是什么?**
156
+ Bucket = GROUP BY 分组,Metric = AVG/SUM/COUNT,Pipeline = 聚合结果的再分析。详见 references/03, references/04。
157
+
158
+ **Q8: Green/Yellow/Red 状态?**
159
+ Green=全正常,Yellow=主分片正常但副本未分配,Red=主分片丢失。
160
+
161
+ **Q9: 如何零停机重建索引?**
162
+ Alias + Reindex:创建新索引 → Reindex 数据 → 原子切换 Alias → 删除旧索引。见 examples/03。
163
+
164
+ **Q10: ES 和 Solr 怎么选?**
165
+ ES 集群管理内置、近实时搜索 1s、聚合强大、ELK 生态完整。Solr 依赖 ZK,配置复杂。
166
+
167
+ **Q11: Mapping 可以修改吗?**
168
+ 可新增字段,不可修改已有字段类型(如 text→keyword),需重建索引。
169
+
170
+ **Q12: search_after 和 from+size 区别?**
171
+ from+size 深度分页 OOM(限制 10000)。search_after 基于排序值翻页,性能与深度无关。
172
+
173
+ **Q13: 数据备份怎么做?**
174
+ Snapshot API 备份到 S3/GCS/FS。推荐 SLM 自动管理。增量快照只存变化。
175
+
176
+ **Q14: 查询慢如何排查?**
177
+ Profile API → 慢查询日志 → filter vs query → segments 数量 → GC 日志。
178
+
179
+ **Q15: ILM 能解决什么问题?**
180
+ 自动滚动(大小/时间阈值)、自动迁移(热→温→冷)、自动压缩、自动删除。
181
+
182
+ ## Keywords
183
+
184
+ elasticsearch, ES, 搜索引擎, 全文搜索, 倒排索引, Lucene, index, mapping, document, shard, replica, analyzer, ik, pinyin, 查询 DSL, match, term, bool, filter, range, multi_match, query_string, nested, geo, aggregation, 聚合, terms, date_histogram, avg, sum, cardinality, percentiles, pipeline, reindex, bulk, scroll, search_after, ILM, rollover, force_merge, alias, snapshot, cluster, ELK, Logstash, Kibana, Filebeat, KNN, dense_vector, 向量搜索, painless, ingest pipeline, RBAC, profile API, 慢查询, zero downtime
185
+
186
+ ## References
187
+
188
+ - references/01-query-dsl-fulltext.md — 全文查询(match/multi_match/query_string)
189
+ - references/02-query-dsl-term.md — 精确查询与复合查询(term/range/bool)
190
+ - references/03-aggregations-metric.md — 指标聚合(avg/sum/stats/cardinality/percentiles)
191
+ - references/04-aggregations-bucket.md — 桶聚合(terms/date_histogram/range)
192
+ - references/05-mapping-types.md — 映射与字段类型详解
193
+ - references/06-analyzers.md — 分词器(标准/IK/pinyin/自定义)
194
+ - references/07-cluster-ops.md — 集群运维(分片/监控/快照/ILM)
195
+ - references/08-elk-integration.md — Logstash/Filebeat/Kibana 配置
196
+ - examples/01-fulltext-search.md — 全文搜索实战
197
+ - examples/02-aggregation-report.md — 聚合报表实战
198
+ - examples/03-reindex-zero-downtime.md — 零停机重建索引
199
+ - examples/04-cluster-monitoring.md — 集群监控实战
@@ -0,0 +1,215 @@
1
+ # 示例: 全文搜索实战 — 电商商品搜索
2
+
3
+ ## 场景
4
+
5
+ 构建一个电商商品搜索功能,支持关键词搜索、多字段搜索、价格筛选、分页。
6
+
7
+ ## 步骤
8
+
9
+ ### 1. 创建索引与 Mapping
10
+
11
+ ```json
12
+ PUT /products
13
+ {
14
+ "settings": {
15
+ "number_of_shards": 3,
16
+ "number_of_replicas": 1
17
+ },
18
+ "mappings": {
19
+ "dynamic": "strict",
20
+ "properties": {
21
+ "title": {
22
+ "type": "text",
23
+ "analyzer": "ik_max_word",
24
+ "fields": {
25
+ "keyword": { "type": "keyword", "ignore_above": 256 }
26
+ }
27
+ },
28
+ "description": {
29
+ "type": "text",
30
+ "analyzer": "ik_max_word"
31
+ },
32
+ "category": { "type": "keyword" },
33
+ "brand": { "type": "keyword" },
34
+ "price": { "type": "float" },
35
+ "tags": { "type": "keyword" },
36
+ "status": { "type": "keyword" },
37
+ "created_at": { "type": "date" },
38
+ "stock": { "type": "integer" }
39
+ }
40
+ }
41
+ }
42
+ ```
43
+
44
+ ### 2. 导入示例数据
45
+
46
+ ```bash
47
+ POST /products/_bulk
48
+ {"index":{"_id":1}}
49
+ {"title":"Apple MacBook Pro 16英寸 M3 Pro","description":"Apple M3 Pro芯片, 18GB统一内存, 512GB存储","category":"笔记本","brand":"Apple","price":19999,"tags":["electronics","laptop","apple"],"status":"active","stock":50,"created_at":"2024-01-15T10:30:00Z"}
50
+ {"index":{"_id":2}}
51
+ {"title":"华为 MateBook X Pro 2024","description":"13.9英寸 3K触控屏, 32GB内存, 1TB SSD","category":"笔记本","brand":"华为","price":14999,"tags":["electronics","laptop","huawei"],"status":"active","stock":30,"created_at":"2024-01-20T14:00:00Z"}
52
+ {"index":{"_id":3}}
53
+ {"title":"iPhone 15 Pro Max 256GB","description":"A17 Pro芯片, 4800万像素主摄, 钛金属设计","category":"手机","brand":"Apple","price":9999,"tags":["electronics","phone","apple"],"status":"active","stock":100,"created_at":"2024-02-01T09:00:00Z"}
54
+ {"index":{"_id":4}}
55
+ {"title":"Samsung Galaxy S24 Ultra","description":"Snapdragon 8 Gen 3, 200MP相机, S Pen","category":"手机","brand":"Samsung","price":12999,"tags":["electronics","phone","samsung"],"status":"active","stock":80,"created_at":"2024-02-10T11:00:00Z"}
56
+ {"index":{"_id":5}}
57
+ {"title":"机械革命 极光Pro 游戏本","description":"RTX4060显卡, i7-12650H, 15.6英寸165Hz","category":"笔记本","brand":"机械革命","price":6999,"tags":["electronics","laptop","gaming"],"status":"active","stock":20,"created_at":"2024-03-01T16:00:00Z"}
58
+ {"index":{"_id":6}}
59
+ {"title":"Apple MacBook Air M2","description":"M2芯片, 13.6英寸, 8GB内存, 256GB存储","category":"笔记本","brand":"Apple","price":8999,"tags":["electronics","laptop","apple"],"status":"inactive","stock":0,"created_at":"2023-06-01T10:00:00Z"}
60
+ ```
61
+
62
+ ### 3. 基本关键词搜索
63
+
64
+ ```bash
65
+ # 搜索"笔记本" (精确匹配标题)
66
+ GET /products/_search
67
+ {
68
+ "query": {
69
+ "match": {
70
+ "title": "笔记本"
71
+ }
72
+ }
73
+ }
74
+ # 返回: MacBook Pro, MateBook X Pro, 极光Pro
75
+
76
+ # 多字段搜索 (标题+描述)
77
+ GET /products/_search
78
+ {
79
+ "query": {
80
+ "multi_match": {
81
+ "query": "苹果笔记本 M3",
82
+ "fields": ["title^3", "description"],
83
+ "type": "best_fields"
84
+ }
85
+ }
86
+ }
87
+ # title 权重 3 倍, MacBook Pro 排最前
88
+ ```
89
+
90
+ ### 4. 综合搜索 (关键词 + 筛选 + 分页)
91
+
92
+ ```bash
93
+ # 搜索"笔记本", 过滤价格 5000-15000, 只返回 active 商品
94
+ GET /products/_search
95
+ {
96
+ "query": {
97
+ "bool": {
98
+ "must": [
99
+ { "match": { "title": "笔记本" } }
100
+ ],
101
+ "filter": [
102
+ { "term": { "status": "active" } },
103
+ { "range": { "price": { "gte": 5000, "lte": 15000 } } }
104
+ ]
105
+ }
106
+ },
107
+ "sort": [
108
+ { "price": { "order": "asc" } }
109
+ ],
110
+ "_source": ["title", "brand", "price", "stock"]
111
+ }
112
+ # 返回: 极光Pro (6999), MacBook Air (8999), MateBook X Pro (14999)
113
+ ```
114
+
115
+ ### 5. 搜索建议 (Completion Suggester)
116
+
117
+ ```json
118
+ PUT /products/_mapping
119
+ {
120
+ "properties": {
121
+ "title_suggest": {
122
+ "type": "completion"
123
+ }
124
+ }
125
+ }
126
+
127
+ POST /products/_update/1
128
+ {
129
+ "doc": {
130
+ "title_suggest": ["Apple MacBook Pro", "MacBook Pro"]
131
+ }
132
+ }
133
+
134
+ POST /products/_update/3
135
+ {
136
+ "doc": {
137
+ "title_suggest": ["iPhone 15 Pro Max", "iPhone 15"]
138
+ }
139
+ }
140
+
141
+ # 搜索建议
142
+ GET /products/_search
143
+ {
144
+ "suggest": {
145
+ "product_suggest": {
146
+ "prefix": "mac",
147
+ "completion": {
148
+ "field": "title_suggest",
149
+ "size": 5
150
+ }
151
+ }
152
+ }
153
+ }
154
+ # 返回: ["Apple MacBook Pro", "MacBook Pro"]
155
+ ```
156
+
157
+ ### 6. 高亮显示
158
+
159
+ ```bash
160
+ GET /products/_search
161
+ {
162
+ "query": {
163
+ "match": { "title": "笔记本" }
164
+ },
165
+ "highlight": {
166
+ "fields": {
167
+ "title": {},
168
+ "description": {}
169
+ },
170
+ "pre_tags": ["<em>"],
171
+ "post_tags": ["</em>"]
172
+ }
173
+ }
174
+ # title 中"笔记本"会被 <em> 标签包裹
175
+ ```
176
+
177
+ ## 完整搜索 API 示例
178
+
179
+ ```bash
180
+ # 前端搜索框调用的完整 API
181
+ GET /products/_search
182
+ {
183
+ "query": {
184
+ "bool": {
185
+ "must": [
186
+ { "multi_match": {
187
+ "query": "笔记本",
188
+ "fields": ["title^3", "description", "brand"],
189
+ "type": "best_fields"
190
+ }}
191
+ ],
192
+ "filter": [
193
+ { "term": { "status": "active" } },
194
+ { "terms": { "category": ["笔记本", "平板"] }},
195
+ { "range": { "price": { "gte": 3000, "lte": 20000 } }},
196
+ { "term": { "brand": "Apple" }}
197
+ ],
198
+ "should": [
199
+ { "term": { "tags": "hot" }},
200
+ { "term": { "is_new": true }}
201
+ ]
202
+ }
203
+ },
204
+ "sort": [
205
+ { "_score": { "order": "desc" }},
206
+ { "created_at": { "order": "desc" }}
207
+ ],
208
+ "from": 0,
209
+ "size": 20,
210
+ "_source": ["title", "brand", "price", "stock", "category"],
211
+ "highlight": {
212
+ "fields": { "title": { "number_of_fragments": 0 } }
213
+ }
214
+ }
215
+ ```