Commit f2cff67c authored by 张辰's avatar 张辰

关键词搜索 后端筛选展示文本

parent 13ac331e
...@@ -23,7 +23,6 @@ public class ThemeQo implements Serializable { ...@@ -23,7 +23,6 @@ public class ThemeQo implements Serializable {
@ApiModelProperty(value = "主题ID") @ApiModelProperty(value = "主题ID")
public String themeId; public String themeId;
@ApiModelProperty(value = "被转发的主题") @ApiModelProperty(value = "被转发的主题")
public String formerThemeId; public String formerThemeId;
...@@ -116,4 +115,8 @@ public class ThemeQo implements Serializable { ...@@ -116,4 +115,8 @@ public class ThemeQo implements Serializable {
@ApiModelProperty("工作室简介") @ApiModelProperty("工作室简介")
private String workshopIntroduction;//工作室简介 private String workshopIntroduction;//工作室简介
// 搜索页用
// 使用关键词搜索时,在列表页展示的简要内容,内容带关键字.
public String briefContent4FullSearch;
} }
...@@ -94,7 +94,6 @@ public class ThemeManager { ...@@ -94,7 +94,6 @@ public class ThemeManager {
ThemeFullSearchResp resp = new ThemeFullSearchResp(); ThemeFullSearchResp resp = new ThemeFullSearchResp();
// 按时间倒叙查询 // 按时间倒叙查询
// todo redis
List<ESThemeQo> esIds = esService.queryThemeIdByContentAndTitle(keyword, from, pageSize * 5); List<ESThemeQo> esIds = esService.queryThemeIdByContentAndTitle(keyword, from, pageSize * 5);
if (esIds.isEmpty()) { if (esIds.isEmpty()) {
return resp; return resp;
...@@ -105,7 +104,14 @@ public class ThemeManager { ...@@ -105,7 +104,14 @@ public class ThemeManager {
return !excludeIds.contains(tId); return !excludeIds.contains(tId);
}).limit(pageSize).collect(Collectors.toList()); }).limit(pageSize).collect(Collectors.toList());
List<ThemeQo> themes = ConvertUtil.themeEntitiesToDTOs(themeService.queryByThemeIds(filterEsIds));
resp.themes = convertEntityToQo(themeService.queryByThemeIds(filterEsIds), userId); resp.themes = convertEntityToQo(themeService.queryByThemeIds(filterEsIds), userId);
// 截取关键词出现的那一部分段落
for (ThemeQo theme : resp.themes) {
theme.briefContent4FullSearch = BizUtils.getThemeContent(keyword, theme);
}
resp.excludeIds.addAll(filterEsIds); resp.excludeIds.addAll(filterEsIds);
return resp; return resp;
......
package com.tanpu.community.util; package com.tanpu.community.util;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.tanpu.community.api.beans.qo.ThemeContentQo;
import com.tanpu.community.api.beans.qo.ThemeQo;
import com.tanpu.community.api.enums.RelTypeEnum;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -15,4 +18,23 @@ public class BizUtils { ...@@ -15,4 +18,23 @@ public class BizUtils {
int realEnd = Math.min(start + size, list.size()); int realEnd = Math.min(start + size, list.size());
return list.subList(start, realEnd); return list.subList(start, realEnd);
} }
public static String getThemeContent(String keyword, ThemeQo theme) {
for (ThemeContentQo paragraph : theme.content) {
if (paragraph.getType().equals(RelTypeEnum.TEXT.type)) {
int idx = paragraph.getValue().indexOf(keyword);
if (idx == -1) {
continue;
} else if (idx < 30) {
// 如果关键词在段落偏头部的部分,则全部返回给前端,前端自由展示
return paragraph.getValue();
} else {
// 否则,保留关键词 向前20个字符
return paragraph.getValue().substring(idx - 20);
}
}
}
return "";
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment