# Elasticsearch 查询相关

## 根据商品ID查询

```json
GET /uat_spu/_search
{
  "query": {
    "term": {
      "_id": "3588136"
    }
  }
}
```

## 查label_id

```json
GET /uat_spu/_search
{"query":{"bool":{"must":[{"term":{ "platform_sku_essaone": true  }},{"term":{"label_id_list":166}}]}}}
```

## collapse折叠

```json
GET /spu/_search
{
  "_source": ["_id", "name_zh", "goods_id", "*"],
  "size": 1000,
  "query": {
    "match": {
      "name_zh": {
        "query": "园艺"
      }
    }
  },
  "collapse": {
    "field": "goods_id",       // 按 goods_id 字段折叠分组
    "inner_hits": {
      "_source": ["_id"],
      "name": "top_docs",      // 自定义内部结果名称
      "size": 3,               // 每组取1个文档
      "sort": [{"_score": "desc"}] // 按评分降序排序
    }
  },
  "sort": ["_score"]          // 确保外层排序兼容折叠功能
}
```

## 在架、某平台商品数

```json
GET /uat_spu/_search
{
  "track_total_hits": 100000,
  "query": {
    "bool": {
          "filter": [
            {
              "term": {
                "platform_sku_essaone": true
              }
            },
            {
              "term": {
                "platform_rule_essaone": true
              }
            },
            {
              "terms": {
                "goods_copyright": [
                  "1",
                  "2",
                  "3"
                ]
              }
            }
          ]
		  }
  }
}
```

## ID查询

```json
GET /uat_spu/_search
{
  "query": {
    "term": {
      "_id": "3302275"
    }
  }
}
```

## 商品名搜索

```json
GET /spu/_search
{
  "_source": {
  },
  "size": 1000,
  "query": {
    "match": {
      "name_zh": {
        "query": "园艺工具四件套",
        "boost": 111.0
      }
    }
  },
  "aggs" :{
            "goods_agg": {
                "terms": {
                    "field": "goods_id",
                    "size": 100
                },
                "aggs": {
                    "top_hits": {
                        "top_hits": {
                            "size": 1,
                            "sort": [{"_score": "desc"}]
                        }
                    }
                }
            }
        }
}
```

## phrase搜索

```json
GET /uat_spu/_search
{
  "_source":["id","goods_id","name_zh"],
  "query":{
                    "multi_match": {
                      "_name": "base_query",
                      "fields": [
                        "name_zh^2.0",
                        "goods_keyword_zh^1.0",
                        "category_name_zh^1.0",
                        "sale_category_keyword_zh^1.0",
                        "sub_name_zh^1.0"
                      ],
                      "query": "戏水玩具"
                    }
      }
}
```

## aggs聚合查询

```json
GET /spu/_search
{
    "_source": {
       "excludes": [
      "embedding_name_zh",
      "embedding_name_ru", "*"
    ],
    "includes": ["_id", "name_zh"]
  },
  "size": 1000,
  "query": {
    "match": {
      "name_zh": {
        "query": "园艺"
      }
    }
  },
  "aggs" :{
            "goods_agg": {
                "terms": {
                    "field": "goods_id",
                    "size": 100
                },
                "aggs": {
                    "top_hits": {
                        "top_hits": {
                            "size": 1,
                            "sort": [{"_score": "desc"}]
                        }
                    }
                }
            }
        }
}
```

## 检查某个字段是否有值
```json
GET /uat_spu/_search
{
  "track_total_hits": true,
  "size": 0,          // 不返回 hits，只返回总数
  "query": {
    "exists": {
      "field": "goods_keyword_zh"
    }
  }
}
```
如果是nested字段，要这么查：
```json
GET /uat_spu/_search
{
  "track_total_hits": true,
  "size": 0,                    // 只看总数
  "query": {
    "nested": {
      "path": "embedding_pic_h14",
      "query": {
        "exists": {
          "field": "embedding_pic_h14.vector"   // 或 url，只要子文档里任意字段有值
        }
      }
    }
  }
}
```

```json
```

## 分词测试

### ik_smart分词器

```json
POST spu/_analyze
{
  "analyzer": "ik_smart",
  "text": "14寸第4代-眼珠实身冰雪公仔带手动大推车,搪胶雪宝宝"
}
```

### ik_max_word分词器

```json
POST spu/_analyze
{
  "analyzer": "ik_max_word",
  "text": "14寸第4代-眼珠实身冰雪公仔带手动大推车,搪胶雪宝宝"
}
```

### query_ik分词器

```json
POST spu/_analyze
{
  "analyzer": "query_ik",
  "text": "14寸第4代-眼珠实身冰雪公仔带手动大推车,搪胶雪宝宝"
}
```

### index_ik分词器

```json
POST spu_test/_analyze
{
  "analyzer": "index_ik",
  "text": "14寸第4代-眼珠实身冰雪公仔带手动大推车,搪胶雪宝宝"
}
```

## 查商品名

```json
GET /spu/_search
{
  "_source": {
    "includes": [
      "name_zh",
      "category_name_zh",
      "sub_name_zh",
      "label_id_list",
      "brand_id",
      "category_id",
      "price_range",
      "on_sell_days_boost"
    ]
  },
  "collapse": {
    "field": "goods_id",
      "inner_hits": {
    "name": "top_hits",
    "size": 1,
    "_source": false  // 避免返回完整_source
  }
  },
  "from": 0,
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "minimum_should_match": 1,
          "should": [
            {
              "multi_match": {
                "_name": "base_query",
                "fields": [
                  "name_zh^2.0",
                  "goods_keyword_zh^1.0",
                  "category_name_zh^1.0",
                  "sale_category_keyword_zh^1.0",
                  "sub_name_zh^1.0"
                ],
                "minimum_should_match": "66%",
                "query": "网球",
                "tie_breaker": 0.301
              }
            }
          ]
        }
      }
    }
  },
  "size": 1000,
  "track_scores": true
}
```

# 配置相关

## 查看索引信息

```bash
GET /_cat/indices/spu?v
```

```bash
GET /spu/_mapping
```

```bash
GET /spu/_settings
```

## 设置刷新时间

```json
PUT /spu/_settings
{ 
  "refresh_interval": -1
}
```

```json
PUT /uat_spu/_settings
{ 
  "refresh_interval": "600s"
}
```

## 设置preload

```json
POST uat_spu/_close
PUT uat_spu/_settings
{
  "index.store.preload": []
}

PUT uat_spu/_settings
{
  "index.store.preload": ["vex", "veq"]
}
# ["nvd", "dvd", "tim", "doc", "dim"]

POST uat_spu/_open

```

## 修改分片数目
主分片的数目（number_of_shards）只能创建索引的时候修改


## 查看索引的分片数量、大小分布
```json
GET _cat/indices/uat_spu?v&s=store.size:desc
GET _cat/shards/uat_spu
```

## 类别查询：索引中该类别总数

```json
GET /spu/_search
{
  "track_total_hits": 10000000,
  "query": {"terms": {
      "sale_category_all": [37619]
    }}
}
```

## 类别查询：模拟平台规则

除了类别限定，包括：
1. essaone平台展示过滤
2. 非专属商品
3. status 2 4 5
4. goods_copyright 1,2,3

```json
GET /spu/_search
{
  "track_total_hits": 10000000,
  "aggs": {
    "unique_count": {
      "cardinality": {
        "field": "goods_id"
      }
    }
  },
  "collapse": {
    "field": "goods_id"
  },
  "query": {
            "bool": {
              "filter": [
                {
                  "bool": {
                    "minimum_should_match": 1,
                    "should": [
                      {
                        "term": {
                          "platform_sku_essaone": true
                        }
                      },
                      {
                        "bool": {
                          "must": [
                            {
                              "term": {
                                "platform_rule_essaone": true
                              }
                            },
                            {
                              "bool": {
                                "must_not": {
                                  "exists": {
                                    "field": "platform_sku_essaone"
                                  }
                                }
                              }
                            }
                          ]
                        }
                      }
                    ]
                  }
                },
                {
                  "bool": {
                    "must_not": {
                      "exists": {
                        "field": "buyer_id"
                      }
                    }
                  }
                },
                {
                  "terms": {
                    "sale_category_all": [37619]
                  }
                },
                {
                  "terms": {
                    "status": [2,4,5]
                  }
                },
                {
                  "terms": {
                    "goods_copyright": [
                      "1",
                      "2",
                      "3"
                    ]
                  }
                }
              ]
            }
  }
}
```

## 关键词查询

```json
GET /spu/_search
{
	"size": 1000,
	"from": 0,
	"_source": {
		"includes": ["name_zh","goods_keyword_zh","sub_name_zh","category_name_zh","goods_keyword_zh","sale_category_keyword_zh"]
	},
	"query": {
    "multi_match": {
									"query": "遥控车",
									"fields": ["name_zh^3.0", "goods_keyword_zh^1.0", "category_name_zh^1.0", "sale_category_keyword_zh^1.0", "sub_name_zh^1.0"],
									"minimum_should_match": "64%",
									"tie_breaker": 0.3,
									"_name": "base_query"
								}
	},
	"track_scores": true,
	"collapse": {
		"field": "goods_id"
	}
}
```

## 慢查询日志

```json
PUT /spu,spu_test/_settings
{
  "index.search.slowlog.threshold.query.warn": "1s",
  "index.search.slowlog.threshold.query.info": "800ms",
  "index.search.slowlog.threshold.query.debug": "600ms",
  "index.search.slowlog.threshold.query.trace": "400ms",
  "index.search.slowlog.threshold.fetch.warn": "500ms",
  "index.search.slowlog.threshold.fetch.info": "400ms",
  "index.search.slowlog.threshold.fetch.debug": "200ms",
  "index.search.slowlog.threshold.fetch.trace": "100ms"
}
```

```bash
GET /spu,spu_test/_settings?include_defaults=true&filter_path=**.slowlog
```

---

# 一些问题

有一个商品，只命中了"玩具"、没有命中"遥控"，两个词命中一个词（注意是所有的字段 总共只命中一个词，玩具命中多次，但是没有任何一个字段有"遥控"），minimum_should_match应该没有达到76%吧，76%是按照什么算的？为什么命中了这个查询条件？

```json
"multi_match": {
  "_name": "base_query",
  "fields": [
    "name_zh^2.0",
    "goods_keyword_zh^1.0",
    "category_name_zh^1.0",
    "sale_category_keyword_zh^1.0",
    "sub_name_zh^1.0"
  ],
  "minimum_should_match": "76%",
  "query": "遥控玩具",
  "tie_breaker": 0.3
}
```

tie_breaker 导致

```json
GET /spu/_search
{
    "_source": {
       "excludes": [
      "embedding_name_zh",
      "embedding_name_ru", "*"
    ],
    "includes": ["_id", "name_zh"]
  },
  "size": 1000,
  "query": {
    "match": {
      "name_zh": {
        "query": "园艺"
      }
    }
  },
  "aggs" :{
            "goods_agg": {
                "terms": {
                    "field": "goods_id",
                    "size": 100
                },
                "aggs": {
                    "top_hits": {
                        "top_hits": {
                            "size": 1,
                            "sort": [{"_score": "desc"}]
                        }
                    }
                }
            }
        }
}


商品搜索查询模板
```json
GET /spu/_search
{
  "_source": {
    "includes": [
      "name_zh"
    ]
  },
  "aggs": {
    "category_stats": {
      "terms": {
        "field": "sale_category_one_all",
        "size": 100
      }
    },
    "spu_attr_stats": {
      "terms": {
        "field": "attribute_option_list",
        "size": 50
      }
    },
    "unique_count": {
      "cardinality": {
        "field": "goods_id"
      }
    }
  },
  "collapse": {
    "field": "goods_id"
  },
  "from": 0,
  "query": {
    "function_score": {
      "boost_mode": "multiply",
      "functions": [
        {
          "filter": {
            "term": {
              "is_video": true
            }
          },
          "weight": 1.05
        }
      ],
      "query": {
        "bool": {
          "filter": [
            {
              "terms": {
                "status": [2,4,5]
              }
            }
          ],
          "must": [
            {
              "bool": {
                "minimum_should_match": 1,
                "should": [
                  {
                    "multi_match": {
                      "_name": "base_query",
                      "fields": [
                        "name_en^3.0",
                        "goods_keyword_en^1.0",
                        "category_name_en^1.3",
                        "sale_category_keyword_en^1.0",
                        "sub_name_en^1.0"
                      ],
                      "minimum_should_match": "75%",
                      "operator": "AND",
                      "query": "changshen flashing toys",
                      "tie_breaker": 0.9
                    }
                  },
                  {
                    "knn": {
                      "_name": "knn_query",
                      "field": "embedding_name_zh",
                      "k": 40,
                      "num_candidates": 120,
                      "query_vector": [...]
                    }
                  }
                ]
              }
            }
          ]
        }
      },
      "score_mode": "sum"
    }
  },
  "size": 1000,
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ],
  "track_scores": true
}
```