为何带尾随空格的match_bool_prefix查询会产生不同结果?
问题原因及分析
核心差异根源
虽然standard analyzer会修剪字段值的尾随空格,但match_bool_prefix查询对末尾带空格与不带空格的查询字符串,生成的查询逻辑完全不同,这直接导致了得分和结果的差异:
- 无尾随空格的
"island":查询被处理为前缀查询(orgName:island*),得分采用前缀查询的默认计算方式(固定1.0) - 带尾随空格的
"island ":查询被处理为精确term匹配查询,触发完整的TF-IDF得分计算(包含idf、tf、文档长度归一化等参数),因此得分远高于前缀查询。
具体信息验证
索引映射
"orgName": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }
查询语句片段
{ "bool": { "should": [ { "match_bool_prefix": { "orgName": { "query": "island", "operator": "AND", "prefix_length": 0, "max_expansions": 50, "fuzzy_transpositions": true, "boost": 1.0 } } } ], "adjust_pure_negative": true, "boost": 1.0 } }
无尾随空格查询得分详情
"value" : 1.0 "description": "orgName:island*", "details": []
带尾随空格查询得分详情
"value": 10.185921, "description": "weight(orgName:island in 45462) [PerFieldSimilarity], result of:", "details": [ { "value": 10.185921, "description": "score(freq=2.0), computed as boost * idf * tf from:", "details": [ { "value": 2.2, "description": "boost", "details": [] }, { "value": 6.9498363, "description": "idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:", "details": [ { "value": 23, "description": "n, number of documents containing term", "details": [] }, { "value": 24509, "description": "N, total number of documents with field", "details": [] } ] }, { "value": 0.6661975, "description": "tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:", "details": [ { "value": 2.0, "description": "freq, occurrences of term within document", "details": [] }, { "value": 1.2, "description": "k1, term saturation parameter", "details": [] }, { "value": 0.75, "description": "b, length normalization parameter", "details": [] }, { "value": 4.0, "description": "dl, length of field", "details": [] }, { "value": 5.127382, "description": "avgdl, average length of field", "details": [] } ] } ] } ]
内容的提问来源于stack exchange,提问作者bipotato911
相关产品推荐
相关产品推荐

