From f27a8d90508eae062e79cf4d73abbf7a7bf6fb98 Mon Sep 17 00:00:00 2001 From: tangwang Date: Wed, 8 Apr 2026 16:47:20 +0800 Subject: [PATCH] ES文档维护 --- .env | 1 + .env.example | 1 + docs/常用查询 - ES.md | 88 +++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------- 3 files changed, 49 insertions(+), 41 deletions(-) diff --git a/.env b/.env index 4f5e97e..4a69515 100644 --- a/.env +++ b/.env @@ -4,6 +4,7 @@ ES_HOST=http://localhost:9200 ES_USERNAME=saas ES_PASSWORD=4hOaLaf41y2VuI8y +ES_AUTH="${ES_USERNAME}:${ES_PASSWORD}" # Redis Configuration (Optional) - AI 生产 10.200.16.14:6479 REDIS_HOST=10.200.16.14 diff --git a/.env.example b/.env.example index cab8be8..1f26e2e 100644 --- a/.env.example +++ b/.env.example @@ -8,6 +8,7 @@ ES_HOST=http://localhost:9200 ES_USERNAME=saas ES_PASSWORD= +ES_AUTH="${ES_USERNAME}:${ES_PASSWORD}" # Redis (生产默认 10.200.16.14:6479,密码见 docs/QUICKSTART.md §1.6) REDIS_HOST=10.200.16.14 diff --git a/docs/常用查询 - ES.md b/docs/常用查询 - ES.md index 9a3f757..607c325 100644 --- a/docs/常用查询 - ES.md +++ b/docs/常用查询 - ES.md @@ -1,36 +1,42 @@ - - ## Elasticsearch 排查流程 +使用前加载环境变量: +```bash +set -a; source .env; set +a +# 或直接 export +export ES_AUTH="saas:4hOaLaf41y2VuI8y" +export ES="http://127.0.0.1:9200" +``` + ### 1. 集群健康状态 ```bash # 集群整体健康(green / yellow / red) -curl -s -u 'saas:4hOaLaf41y2VuI8y' 'http://127.0.0.1:9200/_cluster/health?pretty' +curl -s -u "$ES_AUTH" 'http://127.0.0.1:9200/_cluster/health?pretty' ``` ### 2. 索引概览 ```bash # 查看所有租户索引状态与体积 -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/_cat/indices/search_products_tenant_*?v' +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/_cat/indices/search_products_tenant_*?v' # 或查看全部索引 -curl -s -u 'saas:4hOaLaf41y2VuI8y' 'http://127.0.0.1:9200/_cat/indices?v' +curl -s -u "$ES_AUTH" 'http://127.0.0.1:9200/_cat/indices?v' ``` ### 3. 分片分布 ```bash # 查看分片在各节点的分布情况 -curl -s -u 'saas:4hOaLaf41y2VuI8y' 'http://127.0.0.1:9200/_cat/shards?v' +curl -s -u "$ES_AUTH" 'http://127.0.0.1:9200/_cat/shards?v' ``` ### 4. 分配诊断(如有异常) ```bash # 当 health 非 green 或 shards 状态异常时,定位具体原因 -curl -s -u 'saas:4hOaLaf41y2VuI8y' -X POST 'http://127.0.0.1:9200/_cluster/allocation/explain?pretty' \ +curl -s -u "$ES_AUTH" -X POST 'http://127.0.0.1:9200/_cluster/allocation/explain?pretty' \ -H 'Content-Type: application/json' \ -d '{"index":"search_products_tenant_163","shard":0,"primary":true}' ``` @@ -69,7 +75,7 @@ journalctl -u elasticsearch -f ```bash # 按需替换:索引名、账号密码、ES 地址 INDEX="search_products_tenant_163" -AUTH='saas:4hOaLaf41y2VuI8y' +AUTH="$ES_AUTH" ES="http://localhost:9200" # 1) 关闭索引(写入类请求会失败,注意维护窗口) @@ -141,7 +147,7 @@ systemctl / df / 日志 → 系统层验证 #### 查询指定 spu_id 的商品(返回 title) ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_170/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_170/_search?pretty' -H 'Content-Type: application/json' -d '{ "size": 11, "_source": ["title"], "query": { @@ -156,7 +162,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te #### 查询所有商品(返回 title) ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_170/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_170/_search?pretty' -H 'Content-Type: application/json' -d '{ "size": 100, "_source": ["title"], "query": { @@ -167,7 +173,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te #### 查询指定 spu_id 的商品(返回 title、keywords、tags) ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_170/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_170/_search?pretty' -H 'Content-Type: application/json' -d '{ "size": 5, "_source": ["title", "keywords", "tags"], "query": { @@ -182,7 +188,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te #### 组合查询:匹配标题 + 过滤标签 ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_170/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_170/_search?pretty' -H 'Content-Type: application/json' -d '{ "size": 1, "_source": ["title", "keywords", "tags"], "query": { @@ -206,7 +212,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te #### 组合查询:匹配标题 + 过滤租户(冗余示例) ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_170/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_170/_search?pretty' -H 'Content-Type: application/json' -d '{ "size": 1, "_source": ["title"], "query": { @@ -234,7 +240,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te #### 测试 index_ik 分析器 ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_170/_analyze' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_170/_analyze' -H 'Content-Type: application/json' -d '{ "analyzer": "index_ik", "text": "14寸第4代-眼珠实身冰雪公仔带手动大推车,搪胶雪宝宝" }' @@ -242,7 +248,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te #### 测试 query_ik 分析器 ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_170/_analyze' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_170/_analyze' -H 'Content-Type: application/json' -d '{ "analyzer": "query_ik", "text": "14寸第4代-眼珠实身冰雪公仔带手动大推车,搪胶雪宝宝" }' @@ -254,7 +260,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te #### 多字段匹配 + 聚合(category1、color、size、material) ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_170/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_170/_search?pretty' -H 'Content-Type: application/json' -d '{ "size": 1, "from": 0, "query": { @@ -364,7 +370,7 @@ GET /search_products_tenant_2/_search #### 按 spu_id 查询(通用索引) ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products/_search?pretty' -H 'Content-Type: application/json' -d '{ "size": 5, "query": { "bool": { @@ -381,7 +387,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products/_s ### 5. 统计租户总文档数 ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_170/_count?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_170/_count?pretty' -H 'Content-Type: application/json' -d '{ "query": { "match_all": {} } @@ -396,7 +402,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te #### 1.1 查询特定租户的商品,显示分面相关字段 ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ "query": { "term": { "tenant_id": "162" } }, @@ -411,7 +417,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te #### 1.2 验证 category1_name 字段是否有数据 ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ "query": { "bool": { "filter": [ @@ -426,7 +432,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te #### 1.3 验证 specifications 字段是否有数据 ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ "query": { "bool": { "filter": [ @@ -445,7 +451,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te #### 2.1 category1_name 分面聚合 ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ "query": { "match_all": {} }, "size": 0, "aggs": { @@ -458,7 +464,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te #### 2.2 specifications.color 分面聚合 ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ "query": { "match_all": {} }, "size": 0, "aggs": { @@ -479,7 +485,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te #### 2.3 specifications.size 分面聚合 ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ "query": { "match_all": {} }, "size": 0, "aggs": { @@ -500,7 +506,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te #### 2.4 specifications.material 分面聚合 ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ "query": { "match_all": {} }, "size": 0, "aggs": { @@ -521,7 +527,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te #### 2.5 综合分面聚合(category + color + size + material) ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ "query": { "match_all": {} }, "size": 0, "aggs": { @@ -563,7 +569,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te #### 3.1 查看 specifications 的 name 字段有哪些值 ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products/_search?pretty' -H 'Content-Type: application/json' -d '{ "query": { "term": { "tenant_id": "162" } }, "size": 0, "aggs": { @@ -579,7 +585,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products/_s #### 3.2 查看某个商品的完整 specifications 数据 ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products/_search?pretty' -H 'Content-Type: application/json' -d '{ "query": { "bool": { "filter": [ @@ -600,7 +606,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products/_s **keyword 精确匹配**(示例词:中文 `法式风格`,英文 `long skirt`) ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ "size": 1, "_source": ["spu_id", "title", "enriched_attributes"], "query": { @@ -623,7 +629,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te **text 全文匹配**(经 `index_ik` / `english` 分词;可与上式对照) ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ "size": 1, "_source": ["spu_id", "title", "enriched_attributes"], "query": { @@ -650,7 +656,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te **keyword 精确匹配** ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ "size": 1, "_source": ["spu_id", "title", "option1_values"], "query": { @@ -668,7 +674,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te **text 全文匹配** ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ "size": 1, "_source": ["spu_id", "title", "option1_values"], "query": { @@ -688,7 +694,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te **keyword 精确匹配** ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ "size": 1, "_source": ["spu_id", "title", "enriched_tags"], "query": { @@ -706,7 +712,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te **text 全文匹配** ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ "size": 1, "_source": ["spu_id", "title", "enriched_tags"], "query": { @@ -726,7 +732,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te > `specifications` 为 **nested**,`value_keyword` 为整词匹配;`value_text.*` 可同时 `term` 子字段或 `match` 主 text。 ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ "size": 1, "_source": ["spu_id", "title", "specifications"], "query": { @@ -758,7 +764,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te #### 4.1 统计有 category1_name 的文档数量 ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_163/_count?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_163/_count?pretty' -H 'Content-Type: application/json' -d '{ "query": { "bool": { "filter": [ @@ -771,7 +777,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te #### 4.2 统计有 specifications 的文档数量 ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_163/_count?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_163/_count?pretty' -H 'Content-Type: application/json' -d '{ "query": { "bool": { "filter": [ @@ -788,7 +794,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te #### 5.1 查找没有 category1_name 但有 category 的文档(MySQL 有数据但 ES 没有) ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ "query": { "bool": { "filter": [ @@ -806,7 +812,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_te #### 5.2 查找有 option 但没有 specifications 的文档(数据转换问题) ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ +curl -u "$ES_AUTH" -X GET 'http://localhost:9200/search_products_tenant_163/_search?pretty' -H 'Content-Type: application/json' -d '{ "query": { "bool": { "filter": [ @@ -862,7 +868,7 @@ GET search_products_tenant_163/_mapping GET search_products_tenant_163/_field_caps?fields=* ```bash -curl -u 'saas:4hOaLaf41y2VuI8y' -X POST \ +curl -u "$ES_AUTH" -X POST \ 'http://localhost:9200/search_products_tenant_163/_count' \ -H 'Content-Type: application/json' \ -d '{ @@ -875,7 +881,7 @@ curl -u 'saas:4hOaLaf41y2VuI8y' -X POST \ } }' -curl -u 'saas:4hOaLaf41y2VuI8y' -X POST \ +curl -u "$ES_AUTH" -X POST \ 'http://localhost:9200/search_products_tenant_163/_count' \ -H 'Content-Type: application/json' \ -d '{ -- libgit2 0.21.2