Skip to content

Commit 1ace25a

Browse files
Johnson-zsdeepin-bot[bot]
authored andcommitted
feat: add configurable keyword positioning in search results
1. Added positioningMaxLength parameter to HighlightOptions structure to allow configuration of keyword positioning window size 2. Modified customHighlight function to accept and use this parameter for better keyword positioning in search results 3. Enforced minimum window size of 30 characters for proper keyword visibility 4. Updated all function calls to pass the new parameter 5. Improved documentation for the customHighlight function parameters Log: Added configurable keyword positioning window for better search result snippets Influence: 1. Test search results with different positioningMaxLength values (below 30 and above 30) 2. Verify keyword visibility and snippet positioning in results 3. Check HTML highlighting still works when enabled 4. Test with both short and long content samples 5. Verify behavior with empty content or keyword lists feat: 添加可配置的关键词定位功能 1. 在HighlightOptions结构中添加positioningMaxLength参数,用于配置关键词 定位窗口大小 2. 修改customHighlight函数以接受并使用此参数,改进搜索结果中的关键词定位 3. 强制30个字符的最小窗口尺寸以确保关键词可见性 4. 更新所有函数调用以传递新参数 5. 改进了customHighlight函数的参数文档 Log: 新增可配置关键词定位窗口,改进搜索结果片段显示 Influence: 1. 使用不同的positioningMaxLength值测试搜索结果(小于30和大于30) 2. 验证结果中的关键词可见性和片段定位 3. 检查启用HTML高亮时是否仍正常工作 4. 测试短内容和长内容样本 5. 验证空内容或关键词列表时的行为 Fixes: #365083
1 parent ad74c48 commit 1ace25a

4 files changed

Lines changed: 16 additions & 12 deletions

File tree

include/dfm-search/dfm-search/contentretriever.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ DFM_SEARCH_BEGIN_NS
1818
*/
1919
struct HighlightOptions
2020
{
21-
int maxPreviewLength = 200; ///< Maximum snippet length in characters
22-
bool enableHtml = false; ///< Wrap matched keywords with <b> tags
21+
int maxPreviewLength = 200; ///< Maximum snippet length in characters
22+
int positioningMaxLength = 30; ///< Keyword positioning window size (min 30)
23+
bool enableHtml = false; ///< Wrap matched keywords with <b> tags
2324
};
2425

2526
/**

src/dfm-search/dfm-search-lib/utils/contenthighlighter.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ KeywordMatch findFirstKeywordMatch(const QString &content, const QStringList &ke
155155
}
156156
} // namespace
157157

158-
QString customHighlight(const QStringList &keywords, const QString &content, int maxLength, bool enableHtml)
158+
QString customHighlight(const QStringList &keywords, const QString &content, int maxLength, bool enableHtml, int positioningMaxLength)
159159
{
160160
if (content.isEmpty() || keywords.isEmpty()) {
161161
return QString();
@@ -191,13 +191,13 @@ QString customHighlight(const QStringList &keywords, const QString &content, int
191191
return match.keyword; // Return the keyword as is (original behavior)
192192
}
193193

194-
// This is the "30 characters" from the requirement, used for positioning the keyword.
195-
const int positioningMaxLength = 30;
194+
// Enforce minimum of 30 for the positioning window
195+
const int effectivePositioningLength = qMax(30, positioningMaxLength);
196196

197197
// 1. Calculate the optimal start position.
198198
// This start position is determined based on making the keyword visible
199-
// and well-positioned within a `positioningMaxLength` (e.g., 80 char) window.
200-
int optimalStart = findOptimalStartPosition(content, match.position, positioningMaxLength);
199+
// and well-positioned within the positioning window.
200+
int optimalStart = findOptimalStartPosition(content, match.position, effectivePositioningLength);
201201

202202
// 2. Calculate the optimal end position.
203203
// This uses the `optimalStart` calculated above and extends the snippet

src/dfm-search/dfm-search-lib/utils/contenthighlighter.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ namespace ContentHighlighter {
3333
*
3434
* @param keywords A list of search keywords (supports wildcards *, ?).
3535
* @param content The original document content to be searched.
36-
* @param maxLength The maximum length of the returned snippet (used when no line breaks are present).
37-
* @param enableHtml Whether to wrap matched keywords with <b></b> tags for highlighting.
36+
* @param maxLength The maximum length of the returned snippet (used when no line breaks are present).
37+
* @param enableHtml Whether to wrap matched keywords with <b></b> tags for highlighting.
38+
* @param positioningMaxLength Keyword positioning window size for finding the optimal snippet start/end (min 30).
3839
* @return A snippet of content with matched keywords highlighted, or an empty string if no match is found.
3940
*/
40-
QString customHighlight(const QStringList &keywords, const QString &content, int maxLength, bool enableHtml);
41+
QString customHighlight(const QStringList &keywords, const QString &content, int maxLength, bool enableHtml, int positioningMaxLength = 30);
4142

4243
} // namespace ContentHighlighter
4344

src/dfm-search/dfm-search-lib/utils/contentretriever.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ QString ContentRetriever::fetchHighlight(const QString &path,
219219
}
220220

221221
return ContentHighlighter::customHighlight(
222-
keywords, content, options.maxPreviewLength, options.enableHtml);
222+
keywords, content, options.maxPreviewLength, options.enableHtml,
223+
options.positioningMaxLength);
223224
} catch (const LuceneException &e) {
224225
qWarning() << "ContentRetriever: error fetching highlight for" << path
225226
<< QString::fromStdWString(e.getError());
@@ -260,7 +261,8 @@ QMap<QString, QString> ContentRetriever::fetchHighlights(const QStringList &path
260261
}
261262

262263
results.insert(path, ContentHighlighter::customHighlight(
263-
keywords, content, options.maxPreviewLength, options.enableHtml));
264+
keywords, content, options.maxPreviewLength, options.enableHtml,
265+
options.positioningMaxLength));
264266
} catch (const LuceneException &e) {
265267
qWarning() << "ContentRetriever: error for" << path
266268
<< QString::fromStdWString(e.getError());

0 commit comments

Comments
 (0)