generate_search_products_mapping.py
8.94 KB
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
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import json
from pathlib import Path
from typing import Any
ALL_LANGUAGE_CODES = [
"zh",
"en",
"ar",
"hy",
"eu",
"pt_br",
"bg",
"ca",
"cjk",
"cs",
"da",
"nl",
"fi",
"fr",
"gl",
"de",
"el",
"hi",
"hu",
"id",
"it",
"no",
"fa",
"pt",
"ro",
"ru",
"es",
"sv",
"tr",
"th",
]
CORE_INDEX_LANGUAGES = ["zh", "en"]
LANGUAGE_GROUPS = {
"all": ALL_LANGUAGE_CODES,
"core": CORE_INDEX_LANGUAGES,
}
ANALYZERS = {
"zh": "index_ik",
"en": "english",
"ar": "arabic",
"hy": "armenian",
"eu": "basque",
"pt_br": "brazilian",
"bg": "bulgarian",
"ca": "catalan",
"cjk": "cjk",
"cs": "czech",
"da": "danish",
"nl": "dutch",
"fi": "finnish",
"fr": "french",
"gl": "galician",
"de": "german",
"el": "greek",
"hi": "hindi",
"hu": "hungarian",
"id": "indonesian",
"it": "italian",
"no": "norwegian",
"fa": "persian",
"pt": "portuguese",
"ro": "romanian",
"ru": "russian",
"es": "spanish",
"sv": "swedish",
"tr": "turkish",
"th": "thai",
}
SETTINGS = {
"number_of_shards": 4,
"number_of_replicas": 0,
"refresh_interval": "30s",
"analysis": {
"analyzer": {
"index_ik": {
"type": "custom",
"tokenizer": "ik_max_word",
"filter": ["lowercase", "asciifolding"],
},
"query_ik": {
"type": "custom",
"tokenizer": "ik_smart",
"filter": ["lowercase", "asciifolding"],
},
},
"normalizer": {
"lowercase": {
"type": "custom",
"filter": ["lowercase"],
}
},
},
"similarity": {
"default": {
"type": "BM25",
"b": 0.0,
"k1": 0.0,
}
},
}
TEXT_FIELD_TEMPLATES = {
"all_language_text": {
"language_group": "all",
"with_keyword": False,
},
"all_language_text_with_keyword": {
"language_group": "all",
"with_keyword": True,
},
"core_language_text": {
"language_group": "core",
"with_keyword": False,
},
"core_language_text_with_keyword": {
"language_group": "core",
"with_keyword": True,
},
}
def scalar_field(name: str, field_type: str, **extra: Any) -> dict[str, Any]:
spec = {
"name": name,
"kind": "scalar",
"type": field_type,
}
if extra:
spec["extra"] = extra
return spec
def text_field(name: str, template: str) -> dict[str, Any]:
return {
"name": name,
"kind": "text",
"template": template,
}
def nested_field(name: str, *fields: dict[str, Any]) -> dict[str, Any]:
return {
"name": name,
"kind": "nested",
"fields": list(fields),
}
TEXT_EMBEDDING_SIZE = 1024
IMAGE_EMBEDDING_SIZE = 768
FIELD_SPECS = [
scalar_field("spu_id", "keyword"),
scalar_field("create_time", "date"),
scalar_field("update_time", "date"),
text_field("title", "all_language_text"),
text_field("keywords", "all_language_text_with_keyword"),
text_field("brief", "all_language_text"),
text_field("description", "all_language_text"),
text_field("vendor", "all_language_text_with_keyword"),
scalar_field("image_url", "keyword", index=False),
scalar_field(
"title_embedding",
"dense_vector",
dims=TEXT_EMBEDDING_SIZE,
index=True,
similarity="dot_product",
element_type="bfloat16",
),
nested_field(
"image_embedding",
scalar_field(
"vector",
"dense_vector",
dims=IMAGE_EMBEDDING_SIZE,
index=True,
similarity="dot_product",
element_type="bfloat16",
),
scalar_field("url", "text"),
),
text_field("category_path", "all_language_text_with_keyword"),
text_field("category_name_text", "all_language_text_with_keyword"),
text_field("tags", "all_language_text_with_keyword"),
scalar_field("category_id", "keyword"),
scalar_field("category_name", "keyword"),
scalar_field("category_level", "integer"),
scalar_field("category1_name", "keyword"),
scalar_field("category2_name", "keyword"),
scalar_field("category3_name", "keyword"),
nested_field(
"specifications",
scalar_field("sku_id", "keyword"),
scalar_field("name", "keyword"),
scalar_field("value_keyword", "keyword"),
text_field("value_text", "core_language_text_with_keyword"),
),
text_field("qanchors", "core_language_text"),
text_field("enriched_tags", "core_language_text_with_keyword"),
nested_field(
"enriched_attributes",
scalar_field("name", "keyword"),
text_field("value", "core_language_text_with_keyword"),
),
scalar_field("option1_name", "keyword"),
scalar_field("option2_name", "keyword"),
scalar_field("option3_name", "keyword"),
text_field("option1_values", "core_language_text_with_keyword"),
text_field("option2_values", "core_language_text_with_keyword"),
text_field("option3_values", "core_language_text_with_keyword"),
scalar_field("min_price", "float"),
scalar_field("max_price", "float"),
scalar_field("compare_at_price", "float"),
scalar_field("sku_prices", "float"),
scalar_field("sku_weights", "long"),
scalar_field("sku_weight_units", "keyword"),
scalar_field("total_inventory", "long"),
scalar_field("sales", "long"),
nested_field(
"skus",
scalar_field("sku_id", "keyword"),
scalar_field("price", "float"),
scalar_field("compare_at_price", "float"),
scalar_field("sku_code", "keyword"),
scalar_field("stock", "long"),
scalar_field("weight", "float"),
scalar_field("weight_unit", "keyword"),
scalar_field("option1_value", "keyword"),
scalar_field("option2_value", "keyword"),
scalar_field("option3_value", "keyword"),
scalar_field("image_src", "keyword", index=False),
),
]
def build_keyword_fields() -> dict[str, Any]:
return {
"keyword": {
"type": "keyword",
"normalizer": "lowercase",
}
}
def build_text_field(language: str, *, add_keyword: bool) -> dict[str, Any]:
field = {
"type": "text",
"analyzer": ANALYZERS[language],
}
if language == "zh":
field["search_analyzer"] = "query_ik"
if add_keyword:
field["fields"] = build_keyword_fields()
return field
def render_field(spec: dict[str, Any]) -> dict[str, Any]:
kind = spec["kind"]
if kind == "scalar":
rendered = {"type": spec["type"]}
rendered.update(spec.get("extra", {}))
return rendered
if kind == "text":
template = TEXT_FIELD_TEMPLATES[spec["template"]]
languages = LANGUAGE_GROUPS[template["language_group"]]
properties = {}
for language in languages:
properties[language] = build_text_field(
language,
add_keyword=template["with_keyword"],
)
return {
"type": "object",
"properties": properties,
}
if kind == "nested":
properties = {}
for child in spec["fields"]:
properties[child["name"]] = render_field(child)
return {
"type": "nested",
"properties": properties,
}
raise ValueError(f"Unknown field kind: {kind}")
def build_mapping() -> dict[str, Any]:
properties = {}
for spec in FIELD_SPECS:
properties[spec["name"]] = render_field(spec)
return {
"settings": SETTINGS,
"mappings": {
"properties": properties,
},
}
def render_mapping() -> str:
return json.dumps(build_mapping(), indent=2, ensure_ascii=False)
def main() -> int:
parser = argparse.ArgumentParser(
description="Generate mappings/search_products.json from a compact Python spec.",
)
parser.add_argument(
"-o",
"--output",
type=Path,
help="Write the generated mapping to this file. Defaults to stdout.",
)
parser.add_argument(
"--check",
type=Path,
help="Fail if the generated output does not exactly match this file.",
)
args = parser.parse_args()
rendered = render_mapping()
if args.check is not None:
existing = args.check.read_text(encoding="utf-8")
if existing != rendered:
print(f"Generated mapping does not match {args.check}")
return 1
print(f"Generated mapping matches {args.check}")
if args.output is not None:
args.output.write_text(rendered, encoding="utf-8")
elif args.check is None:
print(rendered, end="")
return 0
if __name__ == "__main__":
raise SystemExit(main())