Blame view

config/config_loader.py 13.9 KB
be52af70   tangwang   first commit
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
  """
  Configuration loader and validator for customer-specific search configurations.
  
  This module handles loading, parsing, and validating YAML configuration files
  that define how each customer's data should be indexed and searched.
  """
  
  import yaml
  import os
  from typing import Dict, Any, List, Optional
  from dataclasses import dataclass, field
  from pathlib import Path
  
  from .field_types import (
      FieldConfig, FieldType, AnalyzerType,
      FIELD_TYPE_MAP, ANALYZER_MAP
  )
  
  
  @dataclass
  class IndexConfig:
      """Configuration for an index domain (e.g., default, title, brand)."""
      name: str
      label: str
      fields: List[str]  # List of field names to include
      analyzer: AnalyzerType
      boost: float = 1.0
      example: Optional[str] = None
  
  
  @dataclass
  class RankingConfig:
      """Configuration for ranking expressions."""
      expression: str  # e.g., "bm25() + 0.2*text_embedding_relevance()"
      description: str
  
  
  @dataclass
  class QueryConfig:
      """Configuration for query processing."""
      supported_languages: List[str] = field(default_factory=lambda: ["zh", "en"])
      default_language: str = "zh"
      enable_translation: bool = True
      enable_text_embedding: bool = True
      enable_query_rewrite: bool = True
      rewrite_dictionary: Dict[str, str] = field(default_factory=dict)
  
      # Translation API settings
      translation_api_key: Optional[str] = None
      translation_service: str = "deepl"  # deepl, google, etc.
  
  
  @dataclass
  class SPUConfig:
      """Configuration for SPU aggregation."""
      enabled: bool = False
      spu_field: Optional[str] = None  # Field containing SPU ID
      inner_hits_size: int = 3
  
  
  @dataclass
  class CustomerConfig:
      """Complete configuration for a customer."""
      customer_id: str
      customer_name: str
  
      # Database settings
      mysql_config: Dict[str, Any]
      main_table: str = "shoplazza_product_sku"
      extension_table: Optional[str] = None
  
      # Field definitions
      fields: List[FieldConfig]
  
      # Index structure (query domains)
      indexes: List[IndexConfig]
  
      # Query processing
      query_config: QueryConfig
  
      # Ranking configuration
      ranking: RankingConfig
  
      # SPU configuration
      spu_config: SPUConfig
  
      # ES index settings
      es_index_name: str
      es_settings: Dict[str, Any] = field(default_factory=dict)
  
  
  class ConfigurationError(Exception):
      """Raised when configuration validation fails."""
      pass
  
  
  class ConfigLoader:
      """Loads and validates customer configurations from YAML files."""
  
      def __init__(self, config_dir: str = "config/schema"):
          self.config_dir = Path(config_dir)
  
      def load_customer_config(self, customer_id: str) -> CustomerConfig:
          """
          Load customer configuration from YAML file.
  
          Args:
              customer_id: Customer identifier (used to find config file)
  
          Returns:
              CustomerConfig object
  
          Raises:
              ConfigurationError: If config file not found or invalid
          """
          config_file = self.config_dir / f"{customer_id}_config.yaml"
  
          if not config_file.exists():
              raise ConfigurationError(f"Configuration file not found: {config_file}")
  
          try:
              with open(config_file, 'r', encoding='utf-8') as f:
                  config_data = yaml.safe_load(f)
          except yaml.YAMLError as e:
              raise ConfigurationError(f"Invalid YAML in {config_file}: {e}")
  
          return self._parse_config(config_data, customer_id)
  
      def _parse_config(self, config_data: Dict[str, Any], customer_id: str) -> CustomerConfig:
          """Parse configuration dictionary into CustomerConfig object."""
  
          # Parse fields
          fields = []
          for field_data in config_data.get("fields", []):
              fields.append(self._parse_field_config(field_data))
  
          # Parse indexes
          indexes = []
          for index_data in config_data.get("indexes", []):
              indexes.append(self._parse_index_config(index_data))
  
          # Parse query config
          query_config_data = config_data.get("query_config", {})
          query_config = QueryConfig(
              supported_languages=query_config_data.get("supported_languages", ["zh", "en"]),
              default_language=query_config_data.get("default_language", "zh"),
              enable_translation=query_config_data.get("enable_translation", True),
              enable_text_embedding=query_config_data.get("enable_text_embedding", True),
              enable_query_rewrite=query_config_data.get("enable_query_rewrite", True),
              rewrite_dictionary=query_config_data.get("rewrite_dictionary", {}),
              translation_api_key=query_config_data.get("translation_api_key"),
              translation_service=query_config_data.get("translation_service", "deepl")
          )
  
          # Parse ranking config
          ranking_data = config_data.get("ranking", {})
          ranking = RankingConfig(
              expression=ranking_data.get("expression", "bm25() + 0.2*text_embedding_relevance()"),
              description=ranking_data.get("description", "Default BM25 + text embedding ranking")
          )
  
          # Parse SPU config
          spu_data = config_data.get("spu_config", {})
          spu_config = SPUConfig(
              enabled=spu_data.get("enabled", False),
              spu_field=spu_data.get("spu_field"),
              inner_hits_size=spu_data.get("inner_hits_size", 3)
          )
  
          return CustomerConfig(
              customer_id=customer_id,
              customer_name=config_data.get("customer_name", customer_id),
              mysql_config=config_data.get("mysql_config", {}),
              main_table=config_data.get("main_table", "shoplazza_product_sku"),
              extension_table=config_data.get("extension_table"),
              fields=fields,
              indexes=indexes,
              query_config=query_config,
              ranking=ranking,
              spu_config=spu_config,
              es_index_name=config_data.get("es_index_name", f"search_{customer_id}"),
              es_settings=config_data.get("es_settings", {})
          )
  
      def _parse_field_config(self, field_data: Dict[str, Any]) -> FieldConfig:
          """Parse field configuration from dictionary."""
          name = field_data["name"]
          field_type_str = field_data["type"]
  
          # Map field type string to enum
          if field_type_str not in FIELD_TYPE_MAP:
              raise ConfigurationError(f"Unknown field type: {field_type_str}")
          field_type = FIELD_TYPE_MAP[field_type_str]
  
          # Map analyzer string to enum (if provided)
          analyzer = None
          analyzer_str = field_data.get("analyzer")
          if analyzer_str and analyzer_str in ANALYZER_MAP:
              analyzer = ANALYZER_MAP[analyzer_str]
  
          search_analyzer = None
          search_analyzer_str = field_data.get("search_analyzer")
          if search_analyzer_str and search_analyzer_str in ANALYZER_MAP:
              search_analyzer = ANALYZER_MAP[search_analyzer_str]
  
          return FieldConfig(
              name=name,
              field_type=field_type,
              source_table=field_data.get("source_table"),
              source_column=field_data.get("source_column", name),
              analyzer=analyzer,
              search_analyzer=search_analyzer,
              required=field_data.get("required", False),
              multi_language=field_data.get("multi_language", False),
              languages=field_data.get("languages"),
              boost=field_data.get("boost", 1.0),
              store=field_data.get("store", False),
              index=field_data.get("index", True),
              embedding_dims=field_data.get("embedding_dims", 1024),
              embedding_similarity=field_data.get("embedding_similarity", "dot_product"),
              nested=field_data.get("nested", False),
              nested_properties=field_data.get("nested_properties")
          )
  
      def _parse_index_config(self, index_data: Dict[str, Any]) -> IndexConfig:
          """Parse index configuration from dictionary."""
          analyzer_str = index_data.get("analyzer", "chinese_ecommerce")
          if analyzer_str not in ANALYZER_MAP:
              raise ConfigurationError(f"Unknown analyzer: {analyzer_str}")
  
          return IndexConfig(
              name=index_data["name"],
              label=index_data.get("label", index_data["name"]),
              fields=index_data["fields"],
              analyzer=ANALYZER_MAP[analyzer_str],
              boost=index_data.get("boost", 1.0),
              example=index_data.get("example")
          )
  
      def validate_config(self, config: CustomerConfig) -> List[str]:
          """
          Validate customer configuration.
  
          Args:
              config: Customer configuration to validate
  
          Returns:
              List of validation error messages (empty if valid)
          """
          errors = []
  
          # Validate field references in indexes
          field_names = {field.name for field in config.fields}
          for index in config.indexes:
              for field_name in index.fields:
                  if field_name not in field_names:
                      errors.append(f"Index '{index.name}' references unknown field '{field_name}'")
  
          # Validate SPU config
          if config.spu_config.enabled:
              if not config.spu_config.spu_field:
                  errors.append("SPU aggregation enabled but no spu_field specified")
              elif config.spu_config.spu_field not in field_names:
                  errors.append(f"SPU field '{config.spu_config.spu_field}' not found in fields")
  
          # Validate embedding fields have proper configuration
          for field in config.fields:
              if field.field_type in [FieldType.TEXT_EMBEDDING, FieldType.IMAGE_EMBEDDING]:
                  if field.embedding_dims <= 0:
                      errors.append(f"Field '{field.name}': embedding_dims must be positive")
                  if field.embedding_similarity not in ["dot_product", "cosine", "l2_norm"]:
                      errors.append(f"Field '{field.name}': invalid embedding_similarity")
  
          # Validate MySQL config
          if "host" not in config.mysql_config:
              errors.append("MySQL configuration missing 'host'")
          if "username" not in config.mysql_config:
              errors.append("MySQL configuration missing 'username'")
          if "password" not in config.mysql_config:
              errors.append("MySQL configuration missing 'password'")
          if "database" not in config.mysql_config:
              errors.append("MySQL configuration missing 'database'")
  
          return errors
  
      def save_config(self, config: CustomerConfig, output_path: Optional[str] = None) -> None:
          """
          Save customer configuration to YAML file.
  
          Args:
              config: Configuration to save
              output_path: Optional output path (defaults to config dir)
          """
          if output_path is None:
              output_path = self.config_dir / f"{config.customer_id}_config.yaml"
  
          # Convert config back to dictionary format
          config_dict = {
              "customer_name": config.customer_name,
              "mysql_config": config.mysql_config,
              "main_table": config.main_table,
              "extension_table": config.extension_table,
              "es_index_name": config.es_index_name,
              "es_settings": config.es_settings,
              "fields": [self._field_to_dict(field) for field in config.fields],
              "indexes": [self._index_to_dict(index) for index in config.indexes],
              "query_config": {
                  "supported_languages": config.query_config.supported_languages,
                  "default_language": config.query_config.default_language,
                  "enable_translation": config.query_config.enable_translation,
                  "enable_text_embedding": config.query_config.enable_text_embedding,
                  "enable_query_rewrite": config.query_config.enable_query_rewrite,
                  "rewrite_dictionary": config.query_config.rewrite_dictionary,
                  "translation_api_key": config.query_config.translation_api_key,
                  "translation_service": config.query_config.translation_service,
              },
              "ranking": {
                  "expression": config.ranking.expression,
                  "description": config.ranking.description
              },
              "spu_config": {
                  "enabled": config.spu_config.enabled,
                  "spu_field": config.spu_config.spu_field,
                  "inner_hits_size": config.spu_config.inner_hits_size
              }
          }
  
          with open(output_path, 'w', encoding='utf-8') as f:
              yaml.dump(config_dict, f, default_flow_style=False, allow_unicode=True)
  
      def _field_to_dict(self, field: FieldConfig) -> Dict[str, Any]:
          """Convert FieldConfig to dictionary."""
          result = {
              "name": field.name,
              "type": field.field_type.value,
              "source_table": field.source_table,
              "source_column": field.source_column,
              "required": field.required,
              "boost": field.boost,
              "store": field.store,
              "index": field.index,
          }
  
          if field.analyzer:
              result["analyzer"] = field.analyzer.value
          if field.search_analyzer:
              result["search_analyzer"] = field.search_analyzer.value
          if field.multi_language:
              result["multi_language"] = field.multi_language
              result["languages"] = field.languages
          if field.embedding_dims != 1024:
              result["embedding_dims"] = field.embedding_dims
          if field.embedding_similarity != "dot_product":
              result["embedding_similarity"] = field.embedding_similarity
          if field.nested:
              result["nested"] = field.nested
              result["nested_properties"] = field.nested_properties
  
          return result
  
      def _index_to_dict(self, index: IndexConfig) -> Dict[str, Any]:
          """Convert IndexConfig to dictionary."""
          return {
              "name": index.name,
              "label": index.label,
              "fields": index.fields,
              "analyzer": index.analyzer.value,
              "boost": index.boost,
              "example": index.example
          }