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
|
"""
Field type definitions for the search engine configuration system.
This module defines all supported field types, analyzers, and their
corresponding Elasticsearch mapping configurations.
"""
from enum import Enum
from typing import Dict, Any, Optional
from dataclasses import dataclass
class FieldType(Enum):
"""Supported field types in the search engine."""
TEXT = "text"
KEYWORD = "keyword"
TEXT_EMBEDDING = "text_embedding"
IMAGE_EMBEDDING = "image_embedding"
INT = "int"
LONG = "long"
FLOAT = "float"
DOUBLE = "double"
DATE = "date"
BOOLEAN = "boolean"
JSON = "json"
class AnalyzerType(Enum):
"""Supported analyzer types for text fields."""
# E-commerce general analysis - Chinese
CHINESE_ECOMMERCE = "index_ansj"
CHINESE_ECOMMERCE_QUERY = "query_ansj"
# Standard language analyzers
ENGLISH = "english"
ARABIC = "arabic"
SPANISH = "spanish"
RUSSIAN = "russian"
JAPANESE = "japanese"
# Standard analyzers
STANDARD = "standard"
KEYWORD = "keyword"
class SimilarityType(Enum):
"""Supported similarity algorithms for text fields."""
BM25 = "BM25"
BM25_CUSTOM = "BM25_custom" # Modified BM25 with b=0.0, k1=0.0
@dataclass
class FieldConfig:
"""Configuration for a single field."""
name: str
field_type: FieldType
|
be52af70
tangwang
first commit
|
57
58
59
60
61
62
63
64
|
analyzer: Optional[AnalyzerType] = None
search_analyzer: Optional[AnalyzerType] = None
required: bool = False
multi_language: bool = False # If true, field has language variants
languages: Optional[list] = None # ['zh', 'en', 'ru']
boost: float = 1.0
store: bool = False
index: bool = True
|
cd3799c6
tangwang
tenant2 1w测试数据 mo...
|
65
|
return_in_source: bool = True # Whether to include this field in search result _source
|
be52af70
tangwang
first commit
|
66
67
68
69
70
71
72
73
74
|
# For embedding fields
embedding_dims: int = 1024
embedding_similarity: str = "dot_product" # dot_product, cosine, l2_norm
# For nested fields (like image embeddings)
nested: bool = False
nested_properties: Optional[Dict[str, Any]] = None
|
cadc77b6
tangwang
索引字段名、变量名、API数据结构...
|
75
76
77
|
# Hybrid Keyword Text (HKText) support
keyword_subfield: bool = False
keyword_ignore_above: int = 256
|
5dcddc06
tangwang
索引重构
|
78
|
keyword_normalizer: Optional[str] = None # For keyword subfield normalizer (e.g., "lowercase")
|
cadc77b6
tangwang
索引字段名、变量名、API数据结构...
|
79
|
|
be52af70
tangwang
first commit
|
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
def get_es_mapping_for_field(field_config: FieldConfig) -> Dict[str, Any]:
"""
Generate Elasticsearch mapping configuration for a field.
Args:
field_config: Field configuration object
Returns:
Dictionary containing ES mapping for the field
"""
mapping = {}
if field_config.field_type == FieldType.TEXT:
mapping = {
"type": "text",
"store": field_config.store,
"index": field_config.index
}
if field_config.analyzer:
if field_config.analyzer == AnalyzerType.CHINESE_ECOMMERCE:
mapping["analyzer"] = "index_ansj"
mapping["search_analyzer"] = "query_ansj"
|
5dcddc06
tangwang
索引重构
|
104
105
106
107
|
elif field_config.analyzer == AnalyzerType.CHINESE_ECOMMERCE_QUERY:
# If search_analyzer is explicitly set to CHINESE_ECOMMERCE_QUERY
mapping["analyzer"] = "index_ansj"
mapping["search_analyzer"] = "query_ansj"
|
be52af70
tangwang
first commit
|
108
109
110
111
|
else:
mapping["analyzer"] = field_config.analyzer.value
if field_config.search_analyzer:
|
5dcddc06
tangwang
索引重构
|
112
113
114
115
|
if field_config.search_analyzer == AnalyzerType.CHINESE_ECOMMERCE_QUERY:
mapping["search_analyzer"] = "query_ansj"
else:
mapping["search_analyzer"] = field_config.search_analyzer.value
|
be52af70
tangwang
first commit
|
116
|
|
cadc77b6
tangwang
索引字段名、变量名、API数据结构...
|
117
118
|
if field_config.keyword_subfield:
mapping.setdefault("fields", {})
|
5dcddc06
tangwang
索引重构
|
119
|
keyword_field = {
|
cadc77b6
tangwang
索引字段名、变量名、API数据结构...
|
120
121
122
|
"type": "keyword",
"ignore_above": field_config.keyword_ignore_above
}
|
5dcddc06
tangwang
索引重构
|
123
124
125
|
if field_config.keyword_normalizer:
keyword_field["normalizer"] = field_config.keyword_normalizer
mapping["fields"]["keyword"] = keyword_field
|
cadc77b6
tangwang
索引字段名、变量名、API数据结构...
|
126
|
|
be52af70
tangwang
first commit
|
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
|
elif field_config.field_type == FieldType.KEYWORD:
mapping = {
"type": "keyword",
"store": field_config.store,
"index": field_config.index
}
elif field_config.field_type == FieldType.TEXT_EMBEDDING:
mapping = {
"type": "dense_vector",
"dims": field_config.embedding_dims,
"index": True,
"similarity": field_config.embedding_similarity
}
elif field_config.field_type == FieldType.IMAGE_EMBEDDING:
if field_config.nested:
mapping = {
"type": "nested",
"properties": {
"vector": {
"type": "dense_vector",
"dims": field_config.embedding_dims,
"index": True,
"similarity": field_config.embedding_similarity
},
"url": {
|
5dcddc06
tangwang
索引重构
|
154
|
"type": "text"
|
be52af70
tangwang
first commit
|
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
|
}
}
}
else:
# Simple vector field
mapping = {
"type": "dense_vector",
"dims": field_config.embedding_dims,
"index": True,
"similarity": field_config.embedding_similarity
}
elif field_config.field_type in [FieldType.INT, FieldType.LONG]:
mapping = {
"type": "long",
"store": field_config.store,
"index": field_config.index
}
elif field_config.field_type in [FieldType.FLOAT, FieldType.DOUBLE]:
mapping = {
"type": "float",
"store": field_config.store,
"index": field_config.index
}
elif field_config.field_type == FieldType.DATE:
mapping = {
"type": "date",
"store": field_config.store,
"index": field_config.index
}
elif field_config.field_type == FieldType.BOOLEAN:
mapping = {
"type": "boolean",
"store": field_config.store,
"index": field_config.index
}
elif field_config.field_type == FieldType.JSON:
|
1f6d15fa
tangwang
重构:SPU级别索引、统一索引架构...
|
196
197
198
199
200
201
202
203
204
205
206
207
208
|
if field_config.nested and field_config.nested_properties:
# Nested type with properties (e.g., variants)
mapping = {
"type": "nested",
"properties": {}
}
# Generate mappings for nested properties
for prop_name, prop_config in field_config.nested_properties.items():
prop_type = prop_config.get("type", "keyword")
prop_mapping = {"type": prop_type}
# Add analyzer for text fields
if prop_type == "text" and "analyzer" in prop_config:
|
41e1f8df
tangwang
店匠体系数据的搜索:mock da...
|
209
210
211
212
213
214
215
|
analyzer_str = prop_config["analyzer"]
# Convert chinese_ecommerce to index_ansj/query_ansj
if analyzer_str == "chinese_ecommerce":
prop_mapping["analyzer"] = "index_ansj"
prop_mapping["search_analyzer"] = "query_ansj"
else:
prop_mapping["analyzer"] = analyzer_str
|
1f6d15fa
tangwang
重构:SPU级别索引、统一索引架构...
|
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
# Add other properties
if "index" in prop_config:
prop_mapping["index"] = prop_config["index"]
if "store" in prop_config:
prop_mapping["store"] = prop_config["store"]
mapping["properties"][prop_name] = prop_mapping
else:
# Simple object type
mapping = {
"type": "object",
"enabled": True
}
|
be52af70
tangwang
first commit
|
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
return mapping
def get_default_analyzers() -> Dict[str, Any]:
"""
Get default analyzer definitions for the index.
Returns:
Dictionary of analyzer configurations
"""
return {
"analysis": {
"analyzer": {
"index_ansj": {
"type": "custom",
"tokenizer": "standard",
"filter": ["lowercase", "asciifolding"]
},
"query_ansj": {
"type": "custom",
"tokenizer": "standard",
"filter": ["lowercase", "asciifolding"]
|
5dcddc06
tangwang
索引重构
|
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
},
"hanlp_index": {
"type": "custom",
"tokenizer": "standard",
"filter": ["lowercase", "asciifolding"]
},
"hanlp_standard": {
"type": "custom",
"tokenizer": "standard",
"filter": ["lowercase", "asciifolding"]
}
},
"normalizer": {
"lowercase": {
"type": "custom",
"filter": ["lowercase"]
|
be52af70
tangwang
first commit
|
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
|
}
}
}
}
def get_default_similarity() -> Dict[str, Any]:
"""
Get default similarity configuration (modified BM25).
Returns:
Dictionary of similarity configurations
"""
return {
"similarity": {
"default": {
"type": "BM25",
"b": 0.0,
"k1": 0.0
}
}
}
# Mapping of field type strings to FieldType enum
FIELD_TYPE_MAP = {
"text": FieldType.TEXT,
"TEXT": FieldType.TEXT,
|
cadc77b6
tangwang
索引字段名、变量名、API数据结构...
|
297
298
299
|
"HKText": FieldType.TEXT,
"hktext": FieldType.TEXT,
"HKTEXT": FieldType.TEXT,
|
be52af70
tangwang
first commit
|
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
|
"keyword": FieldType.KEYWORD,
"KEYWORD": FieldType.KEYWORD,
"LITERAL": FieldType.KEYWORD,
"text_embedding": FieldType.TEXT_EMBEDDING,
"TEXT_EMBEDDING": FieldType.TEXT_EMBEDDING,
"EMBEDDING": FieldType.TEXT_EMBEDDING,
"image_embedding": FieldType.IMAGE_EMBEDDING,
"IMAGE_EMBEDDING": FieldType.IMAGE_EMBEDDING,
"int": FieldType.INT,
"INT": FieldType.INT,
"long": FieldType.LONG,
"LONG": FieldType.LONG,
"float": FieldType.FLOAT,
"FLOAT": FieldType.FLOAT,
"double": FieldType.DOUBLE,
"DOUBLE": FieldType.DOUBLE,
"date": FieldType.DATE,
"DATE": FieldType.DATE,
"boolean": FieldType.BOOLEAN,
"BOOLEAN": FieldType.BOOLEAN,
"json": FieldType.JSON,
"JSON": FieldType.JSON,
}
# Mapping of analyzer strings to AnalyzerType enum
ANALYZER_MAP = {
"chinese": AnalyzerType.CHINESE_ECOMMERCE,
"chinese_ecommerce": AnalyzerType.CHINESE_ECOMMERCE,
"index_ansj": AnalyzerType.CHINESE_ECOMMERCE,
|
5dcddc06
tangwang
索引重构
|
330
331
332
|
"hanlp_index": AnalyzerType.CHINESE_ECOMMERCE, # Alias for index_ansj
"hanlp_standard": AnalyzerType.CHINESE_ECOMMERCE_QUERY, # Alias for query_ansj
"query_ansj": AnalyzerType.CHINESE_ECOMMERCE_QUERY,
|
be52af70
tangwang
first commit
|
333
334
335
336
337
338
339
340
|
"english": AnalyzerType.ENGLISH,
"arabic": AnalyzerType.ARABIC,
"spanish": AnalyzerType.SPANISH,
"russian": AnalyzerType.RUSSIAN,
"japanese": AnalyzerType.JAPANESE,
"standard": AnalyzerType.STANDARD,
"keyword": AnalyzerType.KEYWORD,
}
|