Commit 7bc756c50ff7e80b42486efa551465a732821bd1
1 parent
9a9b9ec5
优化 ES 查询构建
将 must 子句改为 should 子句的多查询策略 实现以下查询类型: base_query:主查询,使用 AND 操作符和 75% minimum_should_match 翻译查询:跨语言查询,boost=0.4 短语查询:短查询的精确短语匹配 关键词查询:基于提取名词的查询,boost=0.1 添加 _get_match_fields() 方法,支持中英文字段动态映射 4. 关键改进点 minimum_should_match 从 67% 提升到 75% 添加 operator: "AND" 确保所有词都匹配 使用 should 子句实现多策略融合 支持短语查询和关键词查询的智能触发
Showing
6 changed files
with
511 additions
and
51 deletions
Show diff stats
config/config.yaml
config/config_loader.py
| ... | ... | @@ -35,6 +35,7 @@ class QueryConfig: |
| 35 | 35 | enable_translation: bool = True |
| 36 | 36 | enable_text_embedding: bool = True |
| 37 | 37 | enable_query_rewrite: bool = True |
| 38 | + enable_multilang_search: bool = True # Enable multi-language search using translations | |
| 38 | 39 | |
| 39 | 40 | # Query rewrite dictionary (loaded from external file) |
| 40 | 41 | rewrite_dictionary: Dict[str, str] = field(default_factory=dict) | ... | ... |
| ... | ... | @@ -0,0 +1,218 @@ |
| 1 | +# 相关性检索优化说明 | |
| 2 | + | |
| 3 | +## 概述 | |
| 4 | + | |
| 5 | +本次优化将相关性检索从简单的 `must` 子句中的 `multi_match` 查询,改为使用 `should` 子句的多查询策略,参考了成熟的搜索实现,显著提升了检索效果。 | |
| 6 | + | |
| 7 | +## 主要改进 | |
| 8 | + | |
| 9 | +## 实现方式 | |
| 10 | + | |
| 11 | +本次优化采用精简实现,直接在 `QueryParser` 中集成必要的分析功能,不新增独立模块。 | |
| 12 | + | |
| 13 | +### 1. 查询结构优化 | |
| 14 | + | |
| 15 | +**之前的结构**(效果差): | |
| 16 | +```json | |
| 17 | +{ | |
| 18 | + "bool": { | |
| 19 | + "must": [ | |
| 20 | + { | |
| 21 | + "multi_match": { | |
| 22 | + "query": "戏水动物", | |
| 23 | + "fields": ["title_zh^3.0", "brief_zh^1.5", ...], | |
| 24 | + "minimum_should_match": "67%", | |
| 25 | + "tie_breaker": 0.9, | |
| 26 | + "boost": 1, | |
| 27 | + "_name": "base_query" | |
| 28 | + } | |
| 29 | + } | |
| 30 | + ] | |
| 31 | + } | |
| 32 | +} | |
| 33 | +``` | |
| 34 | + | |
| 35 | +**优化后的结构**(效果更好): | |
| 36 | +```json | |
| 37 | +{ | |
| 38 | + "bool": { | |
| 39 | + "should": [ | |
| 40 | + { | |
| 41 | + "multi_match": { | |
| 42 | + "_name": "base_query", | |
| 43 | + "fields": ["title_zh^3.0", "brief_zh^1.5", ...], | |
| 44 | + "minimum_should_match": "75%", | |
| 45 | + "operator": "AND", | |
| 46 | + "query": "戏水动物", | |
| 47 | + "tie_breaker": 0.9 | |
| 48 | + } | |
| 49 | + }, | |
| 50 | + { | |
| 51 | + "multi_match": { | |
| 52 | + "_name": "base_query_trans_en", | |
| 53 | + "boost": 0.4, | |
| 54 | + "fields": ["title_en^3.0", ...], | |
| 55 | + "minimum_should_match": "75%", | |
| 56 | + "operator": "AND", | |
| 57 | + "query": "water sports (e.g. animals playing with water)", | |
| 58 | + "tie_breaker": 0.9 | |
| 59 | + } | |
| 60 | + }, | |
| 61 | + { | |
| 62 | + "multi_match": { | |
| 63 | + "query": "戏水动物", | |
| 64 | + "fields": ["title_zh^3.0", "brief_zh^1.5", ...], | |
| 65 | + "type": "phrase", | |
| 66 | + "slop": 2, | |
| 67 | + "boost": 1.0, | |
| 68 | + "_name": "phrase_query" | |
| 69 | + } | |
| 70 | + }, | |
| 71 | + { | |
| 72 | + "multi_match": { | |
| 73 | + "query": "戏水 动物", | |
| 74 | + "fields": ["title_zh^3.0", "brief_zh^1.5", ...], | |
| 75 | + "operator": "AND", | |
| 76 | + "tie_breaker": 0.9, | |
| 77 | + "boost": 0.1, | |
| 78 | + "_name": "keywords_query" | |
| 79 | + } | |
| 80 | + } | |
| 81 | + ], | |
| 82 | + "minimum_should_match": 1 | |
| 83 | + } | |
| 84 | +} | |
| 85 | +``` | |
| 86 | + | |
| 87 | +### 2. 集成查询分析功能 | |
| 88 | + | |
| 89 | +在 `QueryParser` 中直接集成必要的分析功能: | |
| 90 | + | |
| 91 | +- **关键词提取**:使用 HanLP 提取查询中的名词(长度>1),用于关键词查询(可选,HanLP 不可用时降级) | |
| 92 | +- **查询类型判断**:区分短查询和长查询 | |
| 93 | +- **Token 计数**:用于判断查询长度 | |
| 94 | + | |
| 95 | +### 3. 多查询策略 | |
| 96 | + | |
| 97 | +#### 3.1 基础查询(base_query) | |
| 98 | +- 使用 `operator: "AND"` 确保所有词都必须匹配 | |
| 99 | +- `minimum_should_match: "75%"` 提高匹配精度 | |
| 100 | +- 使用 `tie_breaker: 0.9` 进行分数融合 | |
| 101 | + | |
| 102 | +#### 3.2 翻译查询(base_query_trans_zh/en) | |
| 103 | +- 当查询语言不是中文/英文时,添加翻译查询 | |
| 104 | +- 使用较低的 boost(0.4)避免过度影响 | |
| 105 | +- 支持跨语言检索 | |
| 106 | + | |
| 107 | +#### 3.3 短语查询(phrase_query) | |
| 108 | +- 针对短查询(token_count >= 2 且 is_short_query) | |
| 109 | +- 使用 `type: "phrase"` 进行精确短语匹配 | |
| 110 | +- 支持 slop(允许词序调整) | |
| 111 | + | |
| 112 | +#### 3.4 关键词查询(keywords_query) | |
| 113 | +- 使用 HanLP 提取的名词进行查询 | |
| 114 | +- 仅在关键词长度合理时启用(避免关键词占查询比例过高) | |
| 115 | +- 使用较低的 boost(0.1)作为补充 | |
| 116 | + | |
| 117 | +#### 3.5 长查询优化(long_query) | |
| 118 | +- 当前已禁用(参考实现中也是 False) | |
| 119 | +- 未来可根据需要启用 | |
| 120 | + | |
| 121 | +### 4. 字段映射优化 | |
| 122 | + | |
| 123 | +新增 `_get_match_fields()` 方法,支持: | |
| 124 | +- 根据语言动态获取匹配字段 | |
| 125 | +- 区分全部字段(all_fields)和核心字段(core_fields) | |
| 126 | +- 核心字段用于短语查询和关键词查询,提高精度 | |
| 127 | + | |
| 128 | +## 实现细节 | |
| 129 | + | |
| 130 | +### 文件修改清单 | |
| 131 | + | |
| 132 | +1. **修改文件**: | |
| 133 | + - `query/query_parser.py` - 添加关键词提取、查询类型判断等功能(HanLP 可选) | |
| 134 | + - `search/es_query_builder.py` - 实现 should 子句的多查询策略 | |
| 135 | + - `search/searcher.py` - 传递 parsed_query 给查询构建器 | |
| 136 | + | |
| 137 | +### 关键参数说明 | |
| 138 | + | |
| 139 | +- **minimum_should_match**: 从 "67%" 提升到 "75%",提高匹配精度 | |
| 140 | +- **operator**: 从默认改为 "AND",确保所有词都匹配 | |
| 141 | +- **tie_breaker**: 保持 0.9,用于分数融合 | |
| 142 | +- **boost 值**: | |
| 143 | + - base_query: 1.0(默认) | |
| 144 | + - translation queries: 0.4 | |
| 145 | + - phrase_query: 1.0 | |
| 146 | + - keywords_query: 0.1 | |
| 147 | + | |
| 148 | +### 依赖要求 | |
| 149 | + | |
| 150 | +- **HanLP**(可选):如果安装了 `hanlp` 包,会自动启用关键词提取功能 | |
| 151 | + ```bash | |
| 152 | + pip install hanlp | |
| 153 | + ``` | |
| 154 | + | |
| 155 | + 如果未安装,系统会自动降级到简单分析(基于空格分词),不影响基本功能。 | |
| 156 | + | |
| 157 | +- **HanLP 模型**:首次运行时会自动下载 | |
| 158 | + - Tokenizer: `CTB9_TOK_ELECTRA_BASE_CRF` | |
| 159 | + - POS Tagger: `CTB9_POS_ELECTRA_SMALL` | |
| 160 | + | |
| 161 | +### 配置说明 | |
| 162 | + | |
| 163 | +- **忽略关键词**:在 `_extract_keywords()` 方法中配置 | |
| 164 | + - 默认忽略:`['玩具']` | |
| 165 | + | |
| 166 | +## 使用示例 | |
| 167 | + | |
| 168 | +### 基本使用 | |
| 169 | + | |
| 170 | +查询会自动使用优化后的策略,无需额外配置: | |
| 171 | + | |
| 172 | +```python | |
| 173 | +# 在 searcher.py 中,查询会自动使用优化策略 | |
| 174 | +result = searcher.search( | |
| 175 | + query="戏水动物", | |
| 176 | + tenant_id="162", | |
| 177 | + size=10 | |
| 178 | +) | |
| 179 | +``` | |
| 180 | + | |
| 181 | +### 查看分析结果 | |
| 182 | + | |
| 183 | +可以直接从 `parsed_query` 查看分析结果: | |
| 184 | + | |
| 185 | +```python | |
| 186 | +parsed_query = query_parser.parse("戏水动物") | |
| 187 | +print(f"关键词: {parsed_query.keywords}") | |
| 188 | +print(f"Token数: {parsed_query.token_count}") | |
| 189 | +print(f"短查询: {parsed_query.is_short_query}") | |
| 190 | +print(f"长查询: {parsed_query.is_long_query}") | |
| 191 | +``` | |
| 192 | + | |
| 193 | +## 性能考虑 | |
| 194 | + | |
| 195 | +1. **HanLP 初始化**:采用懒加载,首次使用时才初始化 | |
| 196 | +2. **错误处理**:HanLP 初始化失败或未安装时,系统会降级到简单分析(基于空格分词),不影响服务 | |
| 197 | +3. **代码精简**:所有功能直接集成在 `QueryParser` 中,无额外模块依赖 | |
| 198 | + | |
| 199 | +## 后续优化方向 | |
| 200 | + | |
| 201 | +1. **长查询优化**:可以启用长查询的特殊处理 | |
| 202 | +2. **意图识别**:完善意图词典,提供更精准的意图识别 | |
| 203 | +3. **参数调优**:根据实际效果调整 boost 值和 minimum_should_match | |
| 204 | +4. **A/B 测试**:对比优化前后的检索效果 | |
| 205 | + | |
| 206 | +## 注意事项 | |
| 207 | + | |
| 208 | +1. **HanLP 依赖**:HanLP 是可选的,如果未安装或初始化失败,系统会自动降级到简单分析,不会影响基本功能 | |
| 209 | +2. **性能影响**:HanLP 分析会增加一定的处理时间,但采用懒加载机制 | |
| 210 | +3. **字段匹配**:确保 ES 索引中存在对应的中英文字段 | |
| 211 | +4. **代码精简**:所有功能都集成在现有模块中,保持代码结构简洁 | |
| 212 | + | |
| 213 | +## 参考 | |
| 214 | + | |
| 215 | +- 参考实现中的查询构建逻辑 | |
| 216 | +- HanLP 官方文档:https://hanlp.hankcs.com/ | |
| 217 | +- Elasticsearch multi_match 查询文档 | |
| 218 | + | ... | ... |
query/query_parser.py
| ... | ... | @@ -7,6 +7,8 @@ Handles query rewriting, translation, and embedding generation. |
| 7 | 7 | from typing import Dict, List, Optional, Any |
| 8 | 8 | import numpy as np |
| 9 | 9 | import logging |
| 10 | +import re | |
| 11 | +import hanlp | |
| 10 | 12 | |
| 11 | 13 | from embeddings import BgeEncoder |
| 12 | 14 | from config import SearchConfig |
| ... | ... | @@ -28,7 +30,11 @@ class ParsedQuery: |
| 28 | 30 | detected_language: str = "unknown", |
| 29 | 31 | translations: Dict[str, str] = None, |
| 30 | 32 | query_vector: Optional[np.ndarray] = None, |
| 31 | - domain: str = "default" | |
| 33 | + domain: str = "default", | |
| 34 | + keywords: str = "", | |
| 35 | + token_count: int = 0, | |
| 36 | + is_short_query: bool = False, | |
| 37 | + is_long_query: bool = False | |
| 32 | 38 | ): |
| 33 | 39 | self.original_query = original_query |
| 34 | 40 | self.normalized_query = normalized_query |
| ... | ... | @@ -37,6 +43,11 @@ class ParsedQuery: |
| 37 | 43 | self.translations = translations or {} |
| 38 | 44 | self.query_vector = query_vector |
| 39 | 45 | self.domain = domain |
| 46 | + # Query analysis fields | |
| 47 | + self.keywords = keywords | |
| 48 | + self.token_count = token_count | |
| 49 | + self.is_short_query = is_short_query | |
| 50 | + self.is_long_query = is_long_query | |
| 40 | 51 | |
| 41 | 52 | def to_dict(self) -> Dict[str, Any]: |
| 42 | 53 | """Convert to dictionary representation.""" |
| ... | ... | @@ -84,6 +95,13 @@ class QueryParser: |
| 84 | 95 | self.normalizer = QueryNormalizer() |
| 85 | 96 | self.language_detector = LanguageDetector() |
| 86 | 97 | self.rewriter = QueryRewriter(config.query_config.rewrite_dictionary) |
| 98 | + | |
| 99 | + # Initialize HanLP components at startup | |
| 100 | + logger.info("Initializing HanLP components...") | |
| 101 | + self._tok = hanlp.load(hanlp.pretrained.tok.CTB9_TOK_ELECTRA_BASE_CRF) | |
| 102 | + self._tok.config.output_spans = True | |
| 103 | + self._pos_tag = hanlp.load(hanlp.pretrained.pos.CTB9_POS_ELECTRA_SMALL) | |
| 104 | + logger.info("HanLP components initialized") | |
| 87 | 105 | |
| 88 | 106 | @property |
| 89 | 107 | def text_encoder(self) -> BgeEncoder: |
| ... | ... | @@ -105,6 +123,34 @@ class QueryParser: |
| 105 | 123 | translation_context=self.config.query_config.translation_context |
| 106 | 124 | ) |
| 107 | 125 | return self._translator |
| 126 | + | |
| 127 | + def _extract_keywords(self, query: str) -> str: | |
| 128 | + """Extract keywords (nouns with length > 1) from query.""" | |
| 129 | + tok_result = self._tok(query) | |
| 130 | + if not tok_result: | |
| 131 | + return "" | |
| 132 | + | |
| 133 | + words = [x[0] for x in tok_result] | |
| 134 | + pos_tags = self._pos_tag(words) | |
| 135 | + | |
| 136 | + keywords = [] | |
| 137 | + for word, pos in zip(words, pos_tags): | |
| 138 | + if len(word) > 1 and pos.startswith('N'): | |
| 139 | + keywords.append(word) | |
| 140 | + | |
| 141 | + return " ".join(keywords) | |
| 142 | + | |
| 143 | + def _get_token_count(self, query: str) -> int: | |
| 144 | + """Get token count using HanLP.""" | |
| 145 | + tok_result = self._tok(query) | |
| 146 | + return len(tok_result) if tok_result else 0 | |
| 147 | + | |
| 148 | + def _analyze_query_type(self, query: str, token_count: int) -> tuple: | |
| 149 | + """Analyze query type: (is_short_query, is_long_query).""" | |
| 150 | + is_quoted = query.startswith('"') and query.endswith('"') | |
| 151 | + is_short = is_quoted or ((token_count <= 2 or len(query) <= 4) and ' ' not in query) | |
| 152 | + is_long = token_count >= 4 | |
| 153 | + return is_short, is_long | |
| 108 | 154 | |
| 109 | 155 | def parse(self, query: str, generate_vector: bool = True, context: Optional[Any] = None) -> ParsedQuery: |
| 110 | 156 | """ |
| ... | ... | @@ -204,50 +250,40 @@ class QueryParser: |
| 204 | 250 | if context: |
| 205 | 251 | context.add_warning(error_msg) |
| 206 | 252 | |
| 207 | - # Stage 5: Text embedding | |
| 253 | + # Stage 5: Query analysis (keywords, token count, query type) | |
| 254 | + keywords = self._extract_keywords(query_text) | |
| 255 | + token_count = self._get_token_count(query_text) | |
| 256 | + is_short_query, is_long_query = self._analyze_query_type(query_text, token_count) | |
| 257 | + | |
| 258 | + log_debug(f"查询分析 | 关键词: {keywords} | token数: {token_count} | " | |
| 259 | + f"短查询: {is_short_query} | 长查询: {is_long_query}") | |
| 260 | + if context: | |
| 261 | + context.store_intermediate_result('keywords', keywords) | |
| 262 | + context.store_intermediate_result('token_count', token_count) | |
| 263 | + context.store_intermediate_result('is_short_query', is_short_query) | |
| 264 | + context.store_intermediate_result('is_long_query', is_long_query) | |
| 265 | + | |
| 266 | + # Stage 6: Text embedding (only for non-short queries) | |
| 208 | 267 | query_vector = None |
| 209 | - if (generate_vector and | |
| 268 | + should_generate_embedding = ( | |
| 269 | + generate_vector and | |
| 210 | 270 | self.config.query_config.enable_text_embedding and |
| 211 | - domain == "default"): # Only generate vector for default domain | |
| 212 | - # Get thresholds from config | |
| 213 | - chinese_limit = self.config.query_config.embedding_disable_chinese_char_limit | |
| 214 | - english_limit = self.config.query_config.embedding_disable_english_word_limit | |
| 215 | - | |
| 216 | - # Check if embedding should be disabled for short queries | |
| 217 | - should_disable_embedding = False | |
| 218 | - disable_reason = None | |
| 219 | - | |
| 220 | - if detected_lang == 'zh': | |
| 221 | - # For Chinese: disable embedding if character count <= threshold | |
| 222 | - char_count = len(query_text.strip()) | |
| 223 | - if char_count <= chinese_limit: | |
| 224 | - should_disable_embedding = True | |
| 225 | - disable_reason = f"中文查询字数({char_count}) <= {chinese_limit},禁用向量搜索" | |
| 226 | - log_info(disable_reason) | |
| 227 | - if context: | |
| 228 | - context.store_intermediate_result('embedding_disabled_reason', disable_reason) | |
| 229 | - else: | |
| 230 | - # For English: disable embedding if word count <= threshold | |
| 231 | - word_count = len(query_text.strip().split()) | |
| 232 | - if word_count <= english_limit: | |
| 233 | - should_disable_embedding = True | |
| 234 | - disable_reason = f"英文查询单词数({word_count}) <= {english_limit},禁用向量搜索" | |
| 235 | - log_info(disable_reason) | |
| 236 | - if context: | |
| 237 | - context.store_intermediate_result('embedding_disabled_reason', disable_reason) | |
| 238 | - | |
| 239 | - if not should_disable_embedding: | |
| 240 | - try: | |
| 241 | - log_debug("开始生成查询向量") | |
| 242 | - query_vector = self.text_encoder.encode([query_text])[0] | |
| 243 | - log_debug(f"查询向量生成完成 | 形状: {query_vector.shape}") | |
| 244 | - if context: | |
| 245 | - context.store_intermediate_result('query_vector_shape', query_vector.shape) | |
| 246 | - except Exception as e: | |
| 247 | - error_msg = f"查询向量生成失败 | 错误: {str(e)}" | |
| 248 | - log_info(error_msg) | |
| 249 | - if context: | |
| 250 | - context.add_warning(error_msg) | |
| 271 | + domain == "default" and | |
| 272 | + not is_short_query | |
| 273 | + ) | |
| 274 | + | |
| 275 | + if should_generate_embedding: | |
| 276 | + try: | |
| 277 | + log_debug("开始生成查询向量") | |
| 278 | + query_vector = self.text_encoder.encode([query_text])[0] | |
| 279 | + log_debug(f"查询向量生成完成 | 形状: {query_vector.shape}") | |
| 280 | + if context: | |
| 281 | + context.store_intermediate_result('query_vector_shape', query_vector.shape) | |
| 282 | + except Exception as e: | |
| 283 | + error_msg = f"查询向量生成失败 | 错误: {str(e)}" | |
| 284 | + log_info(error_msg) | |
| 285 | + if context: | |
| 286 | + context.add_warning(error_msg) | |
| 251 | 287 | |
| 252 | 288 | # Build result |
| 253 | 289 | result = ParsedQuery( |
| ... | ... | @@ -257,7 +293,11 @@ class QueryParser: |
| 257 | 293 | detected_language=detected_lang, |
| 258 | 294 | translations=translations, |
| 259 | 295 | query_vector=query_vector, |
| 260 | - domain=domain | |
| 296 | + domain=domain, | |
| 297 | + keywords=keywords, | |
| 298 | + token_count=token_count, | |
| 299 | + is_short_query=is_short_query, | |
| 300 | + is_long_query=is_long_query | |
| 261 | 301 | ) |
| 262 | 302 | |
| 263 | 303 | if context and hasattr(context, 'logger'): | ... | ... |
search/es_query_builder.py
| ... | ... | @@ -8,7 +8,7 @@ Simplified architecture: |
| 8 | 8 | - function_score wrapper for boosting fields |
| 9 | 9 | """ |
| 10 | 10 | |
| 11 | -from typing import Dict, Any, List, Optional, Union | |
| 11 | +from typing import Dict, Any, List, Optional, Union, Tuple | |
| 12 | 12 | import numpy as np |
| 13 | 13 | from .boolean_parser import QueryNode |
| 14 | 14 | from config import FunctionScoreConfig |
| ... | ... | @@ -24,7 +24,8 @@ class ESQueryBuilder: |
| 24 | 24 | text_embedding_field: Optional[str] = None, |
| 25 | 25 | image_embedding_field: Optional[str] = None, |
| 26 | 26 | source_fields: Optional[List[str]] = None, |
| 27 | - function_score_config: Optional[FunctionScoreConfig] = None | |
| 27 | + function_score_config: Optional[FunctionScoreConfig] = None, | |
| 28 | + enable_multilang_search: bool = True | |
| 28 | 29 | ): |
| 29 | 30 | """ |
| 30 | 31 | Initialize query builder. |
| ... | ... | @@ -36,6 +37,7 @@ class ESQueryBuilder: |
| 36 | 37 | image_embedding_field: Field name for image embeddings |
| 37 | 38 | source_fields: Fields to return in search results (_source includes) |
| 38 | 39 | function_score_config: Function score configuration |
| 40 | + enable_multilang_search: Enable multi-language search using translations | |
| 39 | 41 | """ |
| 40 | 42 | self.index_name = index_name |
| 41 | 43 | self.match_fields = match_fields |
| ... | ... | @@ -43,6 +45,7 @@ class ESQueryBuilder: |
| 43 | 45 | self.image_embedding_field = image_embedding_field |
| 44 | 46 | self.source_fields = source_fields |
| 45 | 47 | self.function_score_config = function_score_config |
| 48 | + self.enable_multilang_search = enable_multilang_search | |
| 46 | 49 | |
| 47 | 50 | def _split_filters_for_faceting( |
| 48 | 51 | self, |
| ... | ... | @@ -105,7 +108,8 @@ class ESQueryBuilder: |
| 105 | 108 | enable_knn: bool = True, |
| 106 | 109 | knn_k: int = 50, |
| 107 | 110 | knn_num_candidates: int = 200, |
| 108 | - min_score: Optional[float] = None | |
| 111 | + min_score: Optional[float] = None, | |
| 112 | + parsed_query: Optional[Any] = None | |
| 109 | 113 | ) -> Dict[str, Any]: |
| 110 | 114 | """ |
| 111 | 115 | Build complete ES query with post_filter support for multi-select faceting. |
| ... | ... | @@ -154,8 +158,8 @@ class ESQueryBuilder: |
| 154 | 158 | # Complex boolean query |
| 155 | 159 | text_query = self._build_boolean_query(query_node) |
| 156 | 160 | else: |
| 157 | - # Simple text query | |
| 158 | - text_query = self._build_text_query(query_text) | |
| 161 | + # Simple text query - use advanced should-based multi-query strategy | |
| 162 | + text_query = self._build_advanced_text_query(query_text, parsed_query) | |
| 159 | 163 | recall_clauses.append(text_query) |
| 160 | 164 | |
| 161 | 165 | # Embedding recall (KNN - separate from query, handled below) |
| ... | ... | @@ -326,6 +330,7 @@ class ESQueryBuilder: |
| 326 | 330 | def _build_text_query(self, query_text: str) -> Dict[str, Any]: |
| 327 | 331 | """ |
| 328 | 332 | Build simple text matching query (BM25). |
| 333 | + Legacy method - kept for backward compatibility. | |
| 329 | 334 | |
| 330 | 335 | Args: |
| 331 | 336 | query_text: Query text |
| ... | ... | @@ -343,6 +348,199 @@ class ESQueryBuilder: |
| 343 | 348 | "_name": "base_query" |
| 344 | 349 | } |
| 345 | 350 | } |
| 351 | + | |
| 352 | + def _get_match_fields(self, language: str) -> Tuple[List[str], List[str]]: | |
| 353 | + """ | |
| 354 | + Get match fields for a specific language. | |
| 355 | + | |
| 356 | + Args: | |
| 357 | + language: Language code ('zh' or 'en') | |
| 358 | + | |
| 359 | + Returns: | |
| 360 | + (all_fields, core_fields) - core_fields are for phrase/keyword queries | |
| 361 | + """ | |
| 362 | + if language == 'zh': | |
| 363 | + all_fields = [ | |
| 364 | + "title_zh^3.0", | |
| 365 | + "brief_zh^1.5", | |
| 366 | + "description_zh", | |
| 367 | + "vendor_zh^1.5", | |
| 368 | + "tags", | |
| 369 | + "category_path_zh^1.5", | |
| 370 | + "category_name_zh^1.5", | |
| 371 | + "option1_values^0.5" | |
| 372 | + ] | |
| 373 | + core_fields = [ | |
| 374 | + "title_zh^3.0", | |
| 375 | + "brief_zh^1.5", | |
| 376 | + "vendor_zh^1.5", | |
| 377 | + "category_name_zh^1.5" | |
| 378 | + ] | |
| 379 | + else: # en | |
| 380 | + all_fields = [ | |
| 381 | + "title_en^3.0", | |
| 382 | + "brief_en^1.5", | |
| 383 | + "description_en", | |
| 384 | + "vendor_en^1.5", | |
| 385 | + "tags", | |
| 386 | + "category_path_en^1.5", | |
| 387 | + "category_name_en^1.5", | |
| 388 | + "option1_values^0.5" | |
| 389 | + ] | |
| 390 | + core_fields = [ | |
| 391 | + "title_en^3.0", | |
| 392 | + "brief_en^1.5", | |
| 393 | + "vendor_en^1.5", | |
| 394 | + "category_name_en^1.5" | |
| 395 | + ] | |
| 396 | + return all_fields, core_fields | |
| 397 | + | |
| 398 | + def _get_embedding_field(self, language: str) -> str: | |
| 399 | + """Get embedding field name for a language.""" | |
| 400 | + # Currently using unified embedding field | |
| 401 | + return self.text_embedding_field or "title_embedding" | |
| 402 | + | |
| 403 | + def _build_advanced_text_query(self, query_text: str, parsed_query: Optional[Any] = None) -> Dict[str, Any]: | |
| 404 | + """ | |
| 405 | + Build advanced text query using should clauses with multiple query strategies. | |
| 406 | + | |
| 407 | + Reference implementation: | |
| 408 | + - base_query: main query with AND operator and 75% minimum_should_match | |
| 409 | + - translation queries: lower boost (0.4) for other languages | |
| 410 | + - phrase query: for short queries (2+ tokens) | |
| 411 | + - keywords query: extracted nouns from query | |
| 412 | + - KNN query: added separately in build_query | |
| 413 | + | |
| 414 | + Args: | |
| 415 | + query_text: Query text | |
| 416 | + parsed_query: ParsedQuery object with analysis results | |
| 417 | + | |
| 418 | + Returns: | |
| 419 | + ES bool query with should clauses | |
| 420 | + """ | |
| 421 | + should_clauses = [] | |
| 422 | + | |
| 423 | + # Get query analysis from parsed_query | |
| 424 | + translations = {} | |
| 425 | + language = 'zh' | |
| 426 | + keywords = "" | |
| 427 | + token_count = 0 | |
| 428 | + is_short_query = False | |
| 429 | + is_long_query = False | |
| 430 | + | |
| 431 | + if parsed_query: | |
| 432 | + translations = parsed_query.translations or {} | |
| 433 | + language = parsed_query.detected_language or 'zh' | |
| 434 | + keywords = getattr(parsed_query, 'keywords', '') or "" | |
| 435 | + token_count = getattr(parsed_query, 'token_count', 0) or 0 | |
| 436 | + is_short_query = getattr(parsed_query, 'is_short_query', False) | |
| 437 | + is_long_query = getattr(parsed_query, 'is_long_query', False) | |
| 438 | + | |
| 439 | + # Get match fields for the detected language | |
| 440 | + match_fields, core_fields = self._get_match_fields(language) | |
| 441 | + | |
| 442 | + # Tie breaker values | |
| 443 | + tie_breaker_base_query = 0.9 | |
| 444 | + tie_breaker_long_query = 0.9 | |
| 445 | + tie_breaker_keywords = 0.9 | |
| 446 | + | |
| 447 | + # 1. Base query - main query with AND operator | |
| 448 | + should_clauses.append({ | |
| 449 | + "multi_match": { | |
| 450 | + "_name": "base_query", | |
| 451 | + "fields": match_fields, | |
| 452 | + "minimum_should_match": "75%", | |
| 453 | + "operator": "AND", | |
| 454 | + "query": query_text, | |
| 455 | + "tie_breaker": tie_breaker_base_query | |
| 456 | + } | |
| 457 | + }) | |
| 458 | + | |
| 459 | + # 2. Translation queries - lower boost (0.4) for other languages | |
| 460 | + if self.enable_multilang_search: | |
| 461 | + if language != 'zh' and translations.get('zh') and translations['zh'] != query_text: | |
| 462 | + zh_fields, _ = self._get_match_fields('zh') | |
| 463 | + should_clauses.append({ | |
| 464 | + "multi_match": { | |
| 465 | + "query": translations['zh'], | |
| 466 | + "fields": zh_fields, | |
| 467 | + "operator": "AND", | |
| 468 | + "minimum_should_match": "75%", | |
| 469 | + "tie_breaker": tie_breaker_base_query, | |
| 470 | + "boost": 0.4, | |
| 471 | + "_name": "base_query_trans_zh" | |
| 472 | + } | |
| 473 | + }) | |
| 474 | + | |
| 475 | + if language != 'en' and translations.get('en') and translations['en'] != query_text: | |
| 476 | + en_fields, _ = self._get_match_fields('en') | |
| 477 | + should_clauses.append({ | |
| 478 | + "multi_match": { | |
| 479 | + "query": translations['en'], | |
| 480 | + "fields": en_fields, | |
| 481 | + "operator": "AND", | |
| 482 | + "minimum_should_match": "75%", | |
| 483 | + "tie_breaker": tie_breaker_base_query, | |
| 484 | + "boost": 0.4, | |
| 485 | + "_name": "base_query_trans_en" | |
| 486 | + } | |
| 487 | + }) | |
| 488 | + | |
| 489 | + # 3. Long query - add a query with lower minimum_should_match | |
| 490 | + # Currently disabled (False condition in reference) | |
| 491 | + if False and is_long_query: | |
| 492 | + boost = 0.5 * pow(min(1.0, token_count / 10.0), 0.9) | |
| 493 | + minimum_should_match = "70%" | |
| 494 | + should_clauses.append({ | |
| 495 | + "multi_match": { | |
| 496 | + "query": query_text, | |
| 497 | + "fields": match_fields, | |
| 498 | + "minimum_should_match": minimum_should_match, | |
| 499 | + "boost": boost, | |
| 500 | + "tie_breaker": tie_breaker_long_query, | |
| 501 | + "_name": "long_query" | |
| 502 | + } | |
| 503 | + }) | |
| 504 | + | |
| 505 | + # 4. Short query - add phrase query | |
| 506 | + ENABLE_PHRASE_QUERY = True | |
| 507 | + if ENABLE_PHRASE_QUERY and token_count >= 2 and is_short_query: | |
| 508 | + query_length = len(query_text) | |
| 509 | + slop = 0 if query_length < 3 else 1 if query_length < 5 else 2 | |
| 510 | + should_clauses.append({ | |
| 511 | + "multi_match": { | |
| 512 | + "query": query_text, | |
| 513 | + "fields": core_fields, | |
| 514 | + "type": "phrase", | |
| 515 | + "slop": slop, | |
| 516 | + "boost": 1.0, | |
| 517 | + "_name": "phrase_query" | |
| 518 | + } | |
| 519 | + }) | |
| 520 | + | |
| 521 | + # 5. Keywords query - extracted nouns from query | |
| 522 | + elif keywords and len(keywords.split()) <= 2 and 2 * len(keywords.replace(' ', '')) <= len(query_text): | |
| 523 | + should_clauses.append({ | |
| 524 | + "multi_match": { | |
| 525 | + "query": keywords, | |
| 526 | + "fields": core_fields, | |
| 527 | + "operator": "AND", | |
| 528 | + "tie_breaker": tie_breaker_keywords, | |
| 529 | + "boost": 0.1, | |
| 530 | + "_name": "keywords_query" | |
| 531 | + } | |
| 532 | + }) | |
| 533 | + | |
| 534 | + # Return bool query with should clauses | |
| 535 | + if len(should_clauses) == 1: | |
| 536 | + return should_clauses[0] | |
| 537 | + | |
| 538 | + return { | |
| 539 | + "bool": { | |
| 540 | + "should": should_clauses, | |
| 541 | + "minimum_should_match": 1 | |
| 542 | + } | |
| 543 | + } | |
| 346 | 544 | |
| 347 | 545 | def _build_boolean_query(self, node: QueryNode) -> Dict[str, Any]: |
| 348 | 546 | """ | ... | ... |
search/searcher.py
| ... | ... | @@ -112,7 +112,8 @@ class Searcher: |
| 112 | 112 | text_embedding_field=self.text_embedding_field, |
| 113 | 113 | image_embedding_field=self.image_embedding_field, |
| 114 | 114 | source_fields=self.source_fields, |
| 115 | - function_score_config=self.config.function_score | |
| 115 | + function_score_config=self.config.function_score, | |
| 116 | + enable_multilang_search=self.config.query_config.enable_multilang_search | |
| 116 | 117 | ) |
| 117 | 118 | |
| 118 | 119 | def search( |
| ... | ... | @@ -279,7 +280,8 @@ class Searcher: |
| 279 | 280 | size=size, |
| 280 | 281 | from_=from_, |
| 281 | 282 | enable_knn=enable_embedding and parsed_query.query_vector is not None, |
| 282 | - min_score=min_score | |
| 283 | + min_score=min_score, | |
| 284 | + parsed_query=parsed_query | |
| 283 | 285 | ) |
| 284 | 286 | |
| 285 | 287 | # Add facets for faceted search | ... | ... |