Blame view

indexer/mapping_generator.py 3.91 KB
be52af70   tangwang   first commit
1
  """
59b0a342   tangwang   创建手写 mapping JSON
2
  Elasticsearch mapping loader.
be52af70   tangwang   first commit
3
  
59b0a342   tangwang   创建手写 mapping JSON
4
  Loads Elasticsearch index mapping from JSON file.
be52af70   tangwang   first commit
5
6
7
  """
  
  from typing import Dict, Any
59b0a342   tangwang   创建手写 mapping JSON
8
  import json
325eec03   tangwang   1. 日志、配置基础设施,使用优化
9
  import logging
59b0a342   tangwang   创建手写 mapping JSON
10
  from pathlib import Path
be52af70   tangwang   first commit
11
  
86d8358b   tangwang   config optimize
12
  from config.loader import get_app_config
648cb4c2   tangwang   ES docs
13
  
325eec03   tangwang   1. 日志、配置基础设施,使用优化
14
15
  logger = logging.getLogger(__name__)
  
e4a39cc8   tangwang   索引隔离。 不同的tenant_i...
16
  # Default index name (deprecated, use get_tenant_index_name instead)
59b0a342   tangwang   创建手写 mapping JSON
17
18
19
20
21
22
  DEFAULT_INDEX_NAME = "search_products"
  
  # Default mapping file path
  DEFAULT_MAPPING_FILE = Path(__file__).parent.parent / "mappings" / "search_products.json"
  
  
e4a39cc8   tangwang   索引隔离。 不同的tenant_i...
23
24
25
  def get_tenant_index_name(tenant_id: str) -> str:
      """
      Generate index name for a tenant.
648cb4c2   tangwang   ES docs
26
27
28
29
30
31
  
      索引命名规则统一为:
        {ES_INDEX_NAMESPACE}search_products_tenant_{tenant_id}
  
      其中 ES_INDEX_NAMESPACE  config.env_config.ES_INDEX_NAMESPACE 控制,
      用于区分 prod/uat/test 等不同运行环境。
e4a39cc8   tangwang   索引隔离。 不同的tenant_i...
32
      """
86d8358b   tangwang   config optimize
33
      prefix = get_app_config().runtime.index_namespace or ""
648cb4c2   tangwang   ES docs
34
      return f"{prefix}search_products_tenant_{tenant_id}"
e4a39cc8   tangwang   索引隔离。 不同的tenant_i...
35
36
  
  
59b0a342   tangwang   创建手写 mapping JSON
37
  def load_mapping(mapping_file: str = None) -> Dict[str, Any]:
33839b37   tangwang   属性值参与搜索:
38
      """
59b0a342   tangwang   创建手写 mapping JSON
39
40
      Load Elasticsearch mapping from JSON file.
  
33839b37   tangwang   属性值参与搜索:
41
      Args:
59b0a342   tangwang   创建手写 mapping JSON
42
43
          mapping_file: Path to mapping JSON file. If None, uses default.
  
33839b37   tangwang   属性值参与搜索:
44
      Returns:
59b0a342   tangwang   创建手写 mapping JSON
45
46
47
48
49
50
51
52
53
54
55
56
          Dictionary containing index configuration (settings + mappings)
  
      Raises:
          FileNotFoundError: If mapping file doesn't exist
          json.JSONDecodeError: If mapping file is invalid JSON
      """
      if mapping_file is None:
          mapping_file = str(DEFAULT_MAPPING_FILE)
  
      mapping_path = Path(mapping_file)
      if not mapping_path.exists():
          raise FileNotFoundError(f"Mapping file not found: {mapping_path}")
be52af70   tangwang   first commit
57
  
59b0a342   tangwang   创建手写 mapping JSON
58
59
60
61
62
63
64
65
      with open(mapping_path, 'r', encoding='utf-8') as f:
          mapping = json.load(f)
  
      logger.info(f"Loaded mapping from {mapping_path}")
      return mapping
  
  
  def create_index_if_not_exists(es_client, index_name: str, mapping: Dict[str, Any] = None) -> bool:
be52af70   tangwang   first commit
66
67
68
69
      """
      Create Elasticsearch index if it doesn't exist.
  
      Args:
bf89b597   tangwang   feat(search): ada...
70
          es_client: ESClient instance
be52af70   tangwang   first commit
71
          index_name: Name of the index to create
59b0a342   tangwang   创建手写 mapping JSON
72
          mapping: Index mapping configuration. If None, loads from default file.
be52af70   tangwang   first commit
73
74
75
76
  
      Returns:
          True if index was created, False if it already exists
      """
bf89b597   tangwang   feat(search): ada...
77
      if es_client.index_exists(index_name):
325eec03   tangwang   1. 日志、配置基础设施,使用优化
78
          logger.info(f"Index '{index_name}' already exists")
be52af70   tangwang   first commit
79
80
          return False
  
59b0a342   tangwang   创建手写 mapping JSON
81
82
83
      if mapping is None:
          mapping = load_mapping()
  
bf89b597   tangwang   feat(search): ada...
84
      if es_client.create_index(index_name, mapping):
33839b37   tangwang   属性值参与搜索:
85
86
          logger.info(f"Index '{index_name}' created successfully")
          return True
bf89b597   tangwang   feat(search): ada...
87
88
89
      else:
          logger.error(f"Failed to create index '{index_name}'")
          return False
be52af70   tangwang   first commit
90
91
92
93
94
95
96
  
  
  def delete_index_if_exists(es_client, index_name: str) -> bool:
      """
      Delete Elasticsearch index if it exists.
  
      Args:
bf89b597   tangwang   feat(search): ada...
97
          es_client: ESClient instance
be52af70   tangwang   first commit
98
99
100
101
102
          index_name: Name of the index to delete
  
      Returns:
          True if index was deleted, False if it didn't exist
      """
bf89b597   tangwang   feat(search): ada...
103
      if not es_client.index_exists(index_name):
325eec03   tangwang   1. 日志、配置基础设施,使用优化
104
          logger.warning(f"Index '{index_name}' does not exist")
be52af70   tangwang   first commit
105
106
          return False
  
bf89b597   tangwang   feat(search): ada...
107
      if es_client.delete_index(index_name):
33839b37   tangwang   属性值参与搜索:
108
109
          logger.info(f"Index '{index_name}' deleted successfully")
          return True
bf89b597   tangwang   feat(search): ada...
110
111
112
      else:
          logger.error(f"Failed to delete index '{index_name}'")
          return False
be52af70   tangwang   first commit
113
114
115
116
117
118
119
  
  
  def update_mapping(es_client, index_name: str, new_fields: Dict[str, Any]) -> bool:
      """
      Update mapping for existing index (only adding new fields).
  
      Args:
bf89b597   tangwang   feat(search): ada...
120
          es_client: ESClient instance
be52af70   tangwang   first commit
121
122
123
124
125
126
          index_name: Name of the index
          new_fields: New field mappings to add
  
      Returns:
          True if successful
      """
bf89b597   tangwang   feat(search): ada...
127
      if not es_client.index_exists(index_name):
325eec03   tangwang   1. 日志、配置基础设施,使用优化
128
          logger.error(f"Index '{index_name}' does not exist")
be52af70   tangwang   first commit
129
130
131
          return False
  
      mapping = {"properties": new_fields}
bf89b597   tangwang   feat(search): ada...
132
      if es_client.update_mapping(index_name, mapping):
33839b37   tangwang   属性值参与搜索:
133
134
          logger.info(f"Mapping updated for index '{index_name}'")
          return True
bf89b597   tangwang   feat(search): ada...
135
136
137
      else:
          logger.error(f"Failed to update mapping for index '{index_name}'")
          return False