1f6d15fa
tangwang
重构:SPU级别索引、统一索引架构...
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# Base Configuration Guide
店匠通用配置(Base Configuration)使用指南
## 概述
Base配置是店匠(Shoplazza)通用配置,适用于所有使用店匠标准表的客户。该配置采用SPU级别的索引结构,所有客户共享同一个Elasticsearch索引(`search_products`),通过`tenant_id`字段实现数据隔离。
## 核心特性
- **SPU级别索引**:每个ES文档代表一个SPU,包含嵌套的variants数组
- **统一索引**:所有客户共享`search_products`索引
- **租户隔离**:通过`tenant_id`字段实现数据隔离
- **配置简化**:配置只包含ES搜索相关配置,不包含MySQL数据源配置
- **外部友好格式**:API返回格式不包含ES内部字段(`_id`, `_score`, `_source`)
## 配置说明
### 配置文件位置
`config/schema/base/config.yaml`
### 配置内容
Base配置**不包含**以下内容:
- `mysql_config` - MySQL数据库配置
- `main_table` - 主表配置
- `extension_table` - 扩展表配置
- `source_table` / `source_column` - 字段数据源映射
Base配置**只包含**:
- ES字段定义(字段类型、分析器、boost等)
- 查询域(indexes)配置
- 查询处理配置(query_config)
- 排序和打分配置(function_score)
- SPU配置(spu_config)
### 必需字段
- `tenant_id` (KEYWORD, required) - 租户隔离字段
### 主要字段
- `product_id` - 商品ID
- `title`, `brief`, `description` - 文本搜索字段
- `seo_title`, `seo_description`, `seo_keywords` - SEO字段
- `vendor`, `product_type`, `tags`, `category` - 分类和标签字段
- `min_price`, `max_price`, `compare_at_price` - 价格字段
- `variants` (nested) - 嵌套变体数组
## 数据导入流程
### 1. 生成测试数据
```bash
python scripts/generate_test_data.py \
--num-spus 100 \
--tenant-id "1" \
--start-spu-id 1 \
--start-sku-id 1 \
--output test_data.sql
```
### 2. 导入测试数据到MySQL
```bash
python scripts/import_test_data.py \
--db-host localhost \
--db-port 3306 \
--db-database saas \
--db-username root \
--db-password password \
--sql-file test_data.sql \
--tenant-id "1"
```
### 3. 导入数据到Elasticsearch
```bash
python scripts/ingest_shoplazza.py \
--db-host localhost \
--db-port 3306 \
--db-database saas \
--db-username root \
--db-password password \
--tenant-id "1" \
--config base \
--es-host http://localhost:9200 \
--recreate \
--batch-size 500
```
## API使用
### 搜索接口
**端点**: `POST /search/`
**请求头**:
```
X-Tenant-ID: 1
Content-Type: application/json
```
**请求体**:
```json
{
"query": "耳机",
"size": 10,
"from": 0,
"filters": {
"category_keyword": "电子产品"
},
"facets": ["category_keyword", "vendor_keyword"]
}
```
**响应格式**:
```json
{
"results": [
{
"product_id": "1",
"title": "蓝牙耳机 Sony",
"handle": "product-1",
"description": "高品质无线蓝牙耳机",
"vendor": "Sony",
"product_type": "电子产品",
"price": 199.99,
"compare_at_price": 299.99,
"currency": "USD",
"image_url": "//cdn.example.com/products/1.jpg",
"in_stock": true,
"variants": [
{
"variant_id": "1",
"title": "黑色",
"price": 199.99,
"compare_at_price": 299.99,
"sku": "SKU-1-1",
"stock": 50,
"options": {
"option1": "黑色"
}
}
],
"relevance_score": 0.95
}
],
"total": 10,
"max_score": 1.0,
"facets": [
{
"field": "category_keyword",
"label": "category_keyword",
"type": "terms",
"values": [
{
"value": "电子产品",
"label": "电子产品",
"count": 5,
"selected": false
}
]
}
],
"suggestions": [],
"related_searches": [],
"took_ms": 15,
"query_info": {}
}
```
### 响应格式说明
#### 主要变化
1. **`results`替代`hits`**:返回字段从`hits`改为`results`
2. **结构化结果**:每个结果包含`product_id`, `title`, `variants`, `relevance_score`等字段
3. **无ES内部字段**:不包含`_id`, `_score`, `_source`等ES内部字段
4. **嵌套variants**:每个商品包含variants数组,每个variant包含完整的变体信息
|