7fbca0d7
tangwang
启动脚本优化
|
1
|
from types import SimpleNamespace
|
a3d3fb11
tangwang
加phrase提权
|
2
|
from typing import Any, Dict
|
7fbca0d7
tangwang
启动脚本优化
|
3
4
5
6
7
|
import numpy as np
from search.es_query_builder import ESQueryBuilder
|
99b72698
tangwang
测试回归钩子梳理
|
8
9
10
11
|
import pytest
pytestmark = [pytest.mark.search, pytest.mark.regression]
|
7fbca0d7
tangwang
启动脚本优化
|
12
13
14
15
|
def _builder() -> ESQueryBuilder:
return ESQueryBuilder(
match_fields=["title.en^3.0", "brief.en^1.0"],
|
35da3813
tangwang
中英混写query的优化逻辑,不适...
|
16
17
18
|
multilingual_fields=["title", "brief"],
core_multilingual_fields=["title", "brief"],
shared_fields=[],
|
7fbca0d7
tangwang
启动脚本优化
|
19
|
text_embedding_field="title_embedding",
|
dc403578
tangwang
多模态搜索
|
20
|
image_embedding_field="image_embedding.vector",
|
7fbca0d7
tangwang
启动脚本优化
|
21
22
23
24
|
default_language="en",
)
|
dc403578
tangwang
多模态搜索
|
25
26
27
28
29
30
31
|
def _recall_root(es_body: Dict[str, Any]) -> Dict[str, Any]:
query_root = es_body["query"]
if "bool" in query_root and query_root["bool"].get("must"):
query_root = query_root["bool"]["must"][0]
if "function_score" in query_root:
query_root = query_root["function_score"]["query"]
return query_root
|
e756b18e
tangwang
重构了文本召回构建器,现在每个 b...
|
32
33
|
|
dc403578
tangwang
多模态搜索
|
34
35
36
37
38
39
40
41
|
def _recall_should_clauses(es_body: Dict[str, Any]) -> list[Dict[str, Any]]:
root = _recall_root(es_body)
should = root.get("bool", {}).get("should")
if should:
return should
return [root]
|
de98daa3
tangwang
多模态召回优化
|
42
43
44
45
46
47
48
49
50
51
|
def _recall_clause_name(clause: Dict[str, Any]) -> str | None:
if "bool" in clause:
return clause["bool"].get("_name")
if "knn" in clause:
return clause["knn"].get("_name")
if "nested" in clause:
return clause["nested"].get("_name")
return None
|
dc403578
tangwang
多模态搜索
|
52
|
def test_knn_clause_moves_under_query_should_and_uses_outer_filters():
|
7fbca0d7
tangwang
启动脚本优化
|
53
54
55
56
57
58
59
60
|
qb = _builder()
q = qb.build_query(
query_text="bags",
query_vector=np.array([0.1, 0.2, 0.3]),
range_filters={"min_price": {"gte": 50, "lt": 100}},
enable_knn=True,
)
|
dc403578
tangwang
多模态搜索
|
61
62
63
64
|
assert "knn" not in q
should = _recall_should_clauses(q)
assert any(clause.get("knn", {}).get("_name") == "knn_query" for clause in should)
assert q["query"]["bool"]["filter"] == [{"range": {"min_price": {"gte": 50, "lt": 100}}}]
|
7fbca0d7
tangwang
启动脚本优化
|
65
66
|
|
dc403578
tangwang
多模态搜索
|
67
|
def test_knn_clause_uses_outer_query_filter_when_disjunctive_filters_present():
|
7fbca0d7
tangwang
启动脚本优化
|
68
69
70
71
72
73
74
75
76
77
78
|
qb = _builder()
facets = [SimpleNamespace(field="category_name", disjunctive=True)]
q = qb.build_query(
query_text="bags",
query_vector=np.array([0.1, 0.2, 0.3]),
filters={"category_name": ["A", "B"], "vendor": "Nike"},
range_filters={"min_price": {"gte": 50, "lt": 100}},
facet_configs=facets,
enable_knn=True,
)
|
dc403578
tangwang
多模态搜索
|
79
80
81
82
83
|
assert "knn" not in q
assert q["query"]["bool"]["filter"] == [
{"term": {"vendor": "Nike"}},
{"range": {"min_price": {"gte": 50, "lt": 100}}},
]
|
7fbca0d7
tangwang
启动脚本优化
|
84
85
86
|
assert q["post_filter"] == {"terms": {"category_name": ["A", "B"]}}
|
dc403578
tangwang
多模态搜索
|
87
|
def test_knn_clause_has_name_and_no_embedded_filter():
|
7fbca0d7
tangwang
启动脚本优化
|
88
89
90
91
92
93
94
|
qb = _builder()
q = qb.build_query(
query_text="bags",
query_vector=np.array([0.1, 0.2, 0.3]),
enable_knn=True,
)
|
dc403578
tangwang
多模态搜索
|
95
96
97
98
|
should = _recall_should_clauses(q)
knn_clause = next(clause["knn"] for clause in should if clause.get("knn", {}).get("_name") == "knn_query")
assert "filter" not in knn_clause
assert knn_clause["_name"] == "knn_query"
|
c90f80ed
tangwang
相关性优化
|
99
100
|
|
ef5baa86
tangwang
混杂语言处理
|
101
|
def test_text_query_contains_only_base_and_translation_named_queries():
|
c90f80ed
tangwang
相关性优化
|
102
103
|
qb = _builder()
parsed_query = SimpleNamespace(
|
ef5baa86
tangwang
混杂语言处理
|
104
|
rewritten_query="dress",
|
c90f80ed
tangwang
相关性优化
|
105
|
detected_language="en",
|
ef5baa86
tangwang
混杂语言处理
|
106
|
translations={"en": "dress", "zh": "连衣裙"},
|
c90f80ed
tangwang
相关性优化
|
107
108
|
)
|
ef5baa86
tangwang
混杂语言处理
|
109
110
111
112
|
q = qb.build_query(
query_text="dress",
parsed_query=parsed_query,
enable_knn=False,
|
ef5baa86
tangwang
混杂语言处理
|
113
|
)
|
dc403578
tangwang
多模态搜索
|
114
|
should = _recall_should_clauses(q)
|
e756b18e
tangwang
重构了文本召回构建器,现在每个 b...
|
115
|
names = [clause["bool"]["_name"] for clause in should]
|
c90f80ed
tangwang
相关性优化
|
116
|
|
e756b18e
tangwang
重构了文本召回构建器,现在每个 b...
|
117
|
assert names == ["base_query", "base_query_trans_zh"]
|
dc403578
tangwang
多模态搜索
|
118
|
base_should = should[0]["bool"]["should"]
|
f8219b5e
tangwang
1.
|
119
120
|
mm_types = [c["multi_match"]["type"] for c in base_should if "multi_match" in c]
assert mm_types == ["best_fields", "phrase"]
|
ef5baa86
tangwang
混杂语言处理
|
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
def test_text_query_skips_duplicate_translation_same_as_base():
qb = _builder()
parsed_query = SimpleNamespace(
rewritten_query="dress",
detected_language="en",
translations={"en": "dress"},
)
q = qb.build_query(
query_text="dress",
parsed_query=parsed_query,
enable_knn=False,
|
ef5baa86
tangwang
混杂语言处理
|
135
136
|
)
|
ed13851c
tangwang
图片文本两个knn召回相关参数配置
|
137
138
139
140
141
|
query_root = q["query"]
if "function_score" in query_root:
query_root = query_root["function_score"]["query"]
base_bool = query_root["bool"]
assert base_bool["_name"] == "base_query"
|
f8219b5e
tangwang
1.
|
142
143
|
mm_types = [c["multi_match"]["type"] for c in base_bool["should"] if "multi_match" in c]
assert mm_types == ["best_fields", "phrase"]
|
74fdf9bd
tangwang
1.
|
144
145
|
|
dc403578
tangwang
多模态搜索
|
146
|
def test_product_title_exclusion_filter_is_applied_once_on_outer_query():
|
74fdf9bd
tangwang
1.
|
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
|
qb = _builder()
parsed_query = SimpleNamespace(
rewritten_query="fitted dress",
detected_language="en",
translations={"zh": "修身 连衣裙"},
product_title_exclusion_profile=SimpleNamespace(
is_active=True,
all_zh_title_exclusions=lambda: ["宽松"],
all_en_title_exclusions=lambda: ["loose", "relaxed"],
),
)
q = qb.build_query(
query_text="fitted dress",
query_vector=np.array([0.1, 0.2, 0.3]),
parsed_query=parsed_query,
enable_knn=True,
)
expected_filter = {
"bool": {
"must_not": [
{
"bool": {
"should": [
{"match_phrase": {"title.zh": {"query": "宽松"}}},
{"match_phrase": {"title.en": {"query": "loose"}}},
{"match_phrase": {"title.en": {"query": "relaxed"}}},
],
"minimum_should_match": 1,
}
}
]
}
}
assert expected_filter in q["query"]["bool"]["filter"]
|
dc403578
tangwang
多模态搜索
|
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
should = _recall_should_clauses(q)
knn_clause = next(clause["knn"] for clause in should if clause.get("knn", {}).get("_name") == "knn_query")
assert "filter" not in knn_clause
def test_image_knn_clause_is_added_alongside_base_translation_and_text_knn():
qb = _builder()
parsed_query = SimpleNamespace(
rewritten_query="street tee",
detected_language="en",
translations={"zh": "街头短袖"},
)
q = qb.build_query(
query_text="street tee",
query_vector=np.array([0.1, 0.2, 0.3]),
image_query_vector=np.array([0.4, 0.5, 0.6]),
parsed_query=parsed_query,
enable_knn=True,
)
should = _recall_should_clauses(q)
names = [
|
de98daa3
tangwang
多模态召回优化
|
207
|
_recall_clause_name(clause)
|
dc403578
tangwang
多模态搜索
|
208
209
210
|
for clause in should
]
assert names == ["base_query", "base_query_trans_zh", "knn_query", "image_knn_query"]
|
de98daa3
tangwang
多模态召回优化
|
211
212
213
214
|
image_knn = next(clause["nested"] for clause in should if clause.get("nested", {}).get("_name") == "image_knn_query")
assert image_knn["path"] == "image_embedding"
assert image_knn["score_mode"] == "max"
assert image_knn["query"]["knn"]["field"] == "image_embedding.vector"
|
47452e1d
tangwang
feat(search): 支持可...
|
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
|
def test_text_knn_plan_is_reused_for_ann_and_exact_rescore():
qb = _builder()
parsed_query = SimpleNamespace(query_tokens=["a", "b", "c", "d", "e"])
ann_clause = qb.build_text_knn_clause(
np.array([0.1, 0.2, 0.3]),
parsed_query=parsed_query,
)
exact_clause = qb.build_exact_text_knn_rescore_clause(
np.array([0.1, 0.2, 0.3]),
parsed_query=parsed_query,
)
assert ann_clause is not None
assert exact_clause is not None
assert ann_clause["knn"]["k"] == qb.knn_text_k_long
assert ann_clause["knn"]["num_candidates"] == qb.knn_text_num_candidates_long
assert ann_clause["knn"]["boost"] == qb.knn_text_boost * 1.4
assert exact_clause["script_score"]["script"]["params"]["boost"] == qb.knn_text_boost * 1.4
def test_image_knn_plan_is_reused_for_ann_and_exact_rescore():
qb = _builder()
ann_clause = qb.build_image_knn_clause(np.array([0.4, 0.5, 0.6]))
exact_clause = qb.build_exact_image_knn_rescore_clause(np.array([0.4, 0.5, 0.6]))
assert ann_clause is not None
assert exact_clause is not None
assert ann_clause["nested"]["query"]["knn"]["boost"] == qb.knn_image_boost
assert exact_clause["nested"]["query"]["script_score"]["script"]["params"]["boost"] == qb.knn_image_boost
|