13377199
tangwang
接口优化
|
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
|
<!-- b5a93a00-49d7-4266-8dbf-3d3f708334ed 23831e56-f1c5-48ab-8ed5-b11125ad0cf9 -->
# SPU-Level Indexing with Shoplazza API Format & BASE Configuration
## Phase 1: Schema Analysis & Design
### 1.1 Analyze SPU/SKU Fields for Indexing
**SPU Fields** (from `shoplazza_product_spu`):
- **Index + Store**: `id`, `shop_id`, `handle`, `title`, `brief`, `vendor`, `product_type`, `tags`, `category`, `image_src`, `published`, `published_at`
- **Index only**: `seo_title`, `seo_description`, `seo_keywords`
- **Store only**: `description` (HTML, no tokenization)
**SKU Fields** (from `shoplazza_product_sku`, as nested array):
- **Index + Store**: `id`, `title`, `sku`, `barcode`, `price`, `compare_at_price`, `option1`, `option2`, `option3`, `inventory_quantity`, `image_src`
**Price Strategy (CONFIRMED - Option B)**:
- Flatten `min_price`, `max_price`, `compare_at_price` at SPU level for fast filtering/sorting
- Keep full variant prices in nested array for display
### 1.2 Design BASE Schema
**Index**: `search_products` (shared by all tenants)
**Key fields**:
- `tenant_id` (KEYWORD, **REQUIRED**) - always filtered, never optional
- SPU-level flattened fields
- `variants` (NESTED array) - SKU data
- Flattened: `min_price`, `max_price`, `compare_at_price`
- Multi-language: `title_zh`, `title_en`, `title_ru`, etc.
- Embeddings: `title_embedding`, `image_embedding`
## Phase 2: BASE Configuration (Universal Standard)
### 2.1 Create BASE Config
**File**: [`config/schema/base/config.yaml`](config/schema/base/config.yaml)
**This is the universal configuration for ALL merchants using Shoplazza tables.**
Key points:
- Index name: `search_products` (shared)
- Required field: `tenant_id` (always filtered in queries)
- SPU-level fields with multi-language support
- Nested variants structure
- Flattened price fields: `min_price`, `max_price`, `compare_at_price`
- Function_score configuration
### 2.2 Update Field Types & Mapping
**Files**: [`config/field_types.py`](config/field_types.py), [`indexer/mapping_generator.py`](indexer/mapping_generator.py)
- Add `NESTED` field type
- Handle nested mapping generation for variants
- Auto-generate flattened price fields
## Phase 3: Data Ingestion for BASE
### 3.1 SPU-Level Data Transformer
**File**: [`indexer/spu_data_transformer.py`](indexer/spu_data_transformer.py)
Features:
- Load SPU from `shoplazza_product_spu`
- Join SKU from `shoplazza_product_sku` (grouped by spu_id)
- Create nested variants array
- Calculate `min_price`, `max_price`, `compare_at_price`
- Generate title & image embeddings
- Inject `tenant_id` from config
### 3.2 Test Data Generator
**File**: [`scripts/generate_shoplazza_test_data.py`](scripts/generate_shoplazza_test_data.py)
Generate 100 SPU records with:
- 10 categories, multiple vendors
- Multi-language (zh/en/ru)
- Price range: $5-$500
- 1-5 variants per SPU (color, size options)
- Insert into MySQL Shoplazza tables
### 3.3 BASE Ingestion Script
**File**: [`scripts/ingest_base.py`](scripts/ingest_base.py)
- Load from MySQL `shoplazza_product_spu` + `shoplazza_product_sku`
- Use `SPUDataTransformer`
- Index into `search_products` with configured `tenant_id`
## Phase 4: Query Updates
### 4.1 Query Builder Enhancements
**File**: [`search/multilang_query_builder.py`](search/multilang_query_builder.py)
- **Auto-inject `tenant_id` filter** (from config, always applied)
- Support nested queries for variants
- Use flattened price fields for filters: `min_price`, `max_price`
### 4.2 Searcher Updates
**File**: [`search/searcher.py`](search/searcher.py)
- Enforce `tenant_id` filtering
- Handle nested inner_hits for variants
## Phase 5: API Response Transformation
### 5.1 Response Transformer
**File**: [`api/response_transformer.py`](api/response_transformer.py)
Transform ES response to Shoplazza format:
- Extract variants from nested array
- Map fields: `product_id`, `title`, `handle`, `vendor`, `product_type`, `tags`, `price`, `variants`, etc.
- Calculate `in_stock` from variants
### 5.2 Update API Models
**File**: [`api/models.py`](api/models.py)
New models:
- `VariantOption`, `ProductVariant`, `ProductResult`
- Updated `SearchResponse` with `results: List[ProductResult]`
- Add placeholders: `suggestions: List[str] = []`, `related_searches: List[str] = []`
### 5.3 Update Search Routes
**File**: [`api/routes/search.py`](api/routes/search.py)
- Use `ResponseTransformer` to convert ES hits
- Return new Shoplazza-compatible format
- **Ensure `tenant_id` is required in request**
## Phase 6: Legacy Migration
|