搜索优化

This commit is contained in:
JenniferW 2025-12-22 13:45:58 +08:00
parent a6c0293205
commit f3b289d16c
1 changed files with 49 additions and 11 deletions

View File

@ -1114,6 +1114,15 @@ const triggerConditionSuggestions = (conditionText) => {
suggestions.value = uniqueSuggestions.slice(0, 8); suggestions.value = uniqueSuggestions.slice(0, 8);
} }
// 无论是字段还是值,只要光标移动到某个条件,都实时调用 searchHint,
// 使用当前这一段文本作为 keyword,且不依赖之前的本地缓存
if (conditionText && conditionText.trim()) {
// 允许本次 smart 接口返回覆盖本地字段/值缓存
disableLocalSuggestions.value = false;
fieldsFromApi.value = false;
scheduleSearchHint(conditionText.trim());
}
}; };
// 根据当前输入更新建议列表(不触发远程请求) // 根据当前输入更新建议列表(不触发远程请求)
@ -1490,8 +1499,36 @@ const fetchSearchHints = async (keyword) => {
// 始终基于最新的 currentInput.value 解析条件,确保使用最新的条件顺序 // 始终基于最新的 currentInput.value 解析条件,确保使用最新的条件顺序
parsedConditions.value = parseConditions(currentInput.value); parsedConditions.value = parseConditions(currentInput.value);
// 构建 fieldConditions,使用最新的条件顺序 // 检测当前正在编辑的条件索引
const fieldConditions = parsedConditions.value const cursorIndex = findCursorConditionIndex();
const parts = currentInput.value.split(/[;;]/);
const activeIndex =
cursorIndex >= 0 && cursorIndex < parts.length
? cursorIndex
: parts.length - 1;
// 如果正在编辑中间的条件,需要重新排序:将正在编辑的条件移到最后一个位置
let conditionsToProcess = [...parsedConditions.value];
let reorderedInputWord = currentInput.value.trim();
if (activeIndex >= 0 && activeIndex < conditionsToProcess.length - 1) {
// 正在编辑中间的条件,需要重新排序
const [editedCondition] = conditionsToProcess.splice(activeIndex, 1);
conditionsToProcess.push(editedCondition);
// 重新构建 inputWord,将被编辑的条件移到最后一个位置
const reorderedParts = parts
.map((p) => p.trim())
.filter((p) => p);
if (activeIndex < reorderedParts.length) {
const [editedPart] = reorderedParts.splice(activeIndex, 1);
reorderedParts.push(editedPart);
reorderedInputWord = reorderedParts.join(";");
}
}
// 构建 fieldConditions,使用重新排序后的条件顺序
const fieldConditions = conditionsToProcess
.filter((condition) => { .filter((condition) => {
// 保留有值的条件,或者有字段名但没有值的条件(如 "车型:") // 保留有值的条件,或者有字段名但没有值的条件(如 "车型:")
return condition.value || (condition.fieldLabel && condition.field); return condition.value || (condition.fieldLabel && condition.field);
@ -1552,7 +1589,7 @@ const fetchSearchHints = async (keyword) => {
const payload = { const payload = {
fieldConditions, fieldConditions,
inputWord: currentInput.value.trim(), inputWord: reorderedInputWord,
page: 0, page: 0,
size: pageSize.value, size: pageSize.value,
}; };
@ -1586,12 +1623,7 @@ const fetchSearchHints = async (keyword) => {
// 从返回的数据中提取字段信息用于"可能属于的字段"下拉框 // 从返回的数据中提取字段信息用于"可能属于的字段"下拉框
// 优先根据光标位置确定当前正在编辑的条件,再决定使用哪一段文本 // 优先根据光标位置确定当前正在编辑的条件,再决定使用哪一段文本
const parts = currentInput.value.split(/[;;]/); // 注意:parts、cursorIndex、activeIndex 已在函数开始处声明,这里直接使用
const cursorIndex = findCursorConditionIndex();
const activeIndex =
cursorIndex >= 0 && cursorIndex < parts.length
? cursorIndex
: parts.length - 1;
const currentPart = parts[activeIndex]?.trim() || ""; const currentPart = parts[activeIndex]?.trim() || "";
const isEditingExistingField = const isEditingExistingField =
cursorIndex >= 0 && cursorIndex < parts.length - 1; cursorIndex >= 0 && cursorIndex < parts.length - 1;
@ -1876,9 +1908,15 @@ const selectSuggestion = (item) => {
parts[targetIndex] = `${fieldPart}${item.label}`; parts[targetIndex] = `${fieldPart}${item.label}`;
} }
} else { } else {
// 无字段时直接处理值 // 无字段时,如果值建议项包含字段信息,自动补充字段名
if (item.fieldLabel) {
// 自动补充字段名和冒号
parts[targetIndex] = `${item.fieldLabel}:${item.label}`;
} else {
// 如果没有字段信息,直接使用值
parts[targetIndex] = item.label; parts[targetIndex] = item.label;
} }
}
// 核心:值选择后自动添加分号(避免重复添加) // 核心:值选择后自动添加分号(避免重复添加)
const lastChar = parts[targetIndex].slice(-1); const lastChar = parts[targetIndex].slice(-1);