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
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
|
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
# 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
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"
else:
mapping["analyzer"] = field_config.analyzer.value
if field_config.search_analyzer:
mapping["search_analyzer"] = field_config.search_analyzer.value
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": {
"type": "keyword"
}
}
}
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级别索引、统一索引架构...
|
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
|
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:
prop_mapping["analyzer"] = prop_config["analyzer"]
# 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
|
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
|
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"]
}
}
}
}
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,
"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,
"english": AnalyzerType.ENGLISH,
"arabic": AnalyzerType.ARABIC,
"spanish": AnalyzerType.SPANISH,
"russian": AnalyzerType.RUSSIAN,
"japanese": AnalyzerType.JAPANESE,
"standard": AnalyzerType.STANDARD,
"keyword": AnalyzerType.KEYWORD,
}
|