Commit ab13575d authored by 张辰's avatar 张辰

关键词full search 增加对多关键词搜索支持

parent ca40b021
......@@ -103,8 +103,10 @@ public class ThemeManager {
Integer from = (pageNo - 1) * pageSize;
ThemeFullSearchResp resp = new ThemeFullSearchResp();
String[] keywords = StringUtils.split(keyword, " ");
// 按时间倒叙查询
List<ESThemeQo> esIds = esService.queryThemeIdByContentAndTitle(keyword, from, pageSize * 5);
List<ESThemeQo> esIds = esService.queryThemeIdByContentAndTitle(keywords, from, pageSize * 5);
if (esIds.isEmpty()) {
return resp;
}
......@@ -123,7 +125,7 @@ public class ThemeManager {
});
// 截取关键词出现的那一部分段落
for (ThemeQo theme : resp.themes) {
theme.briefContent4FullSearch = BizUtils.getThemeContent(keyword, theme);
theme.briefContent4FullSearch = BizUtils.getThemeContent(keywords, theme);
}
excludeIds.addAll(resp.themes.stream().map(ThemeQo::getThemeId).collect(Collectors.toList()));
......
......@@ -42,14 +42,13 @@ public class ESService {
helper.insert(String.valueOf(qo.themeType), qo.themeId, JSON.toJSONString(qo));
}
public List<ESThemeQo> queryThemeIdByContentAndTitle(String keyword, int from, int size) {
public List<ESThemeQo> queryThemeIdByContentAndTitle(String[] keywords, int from, int size) {
SearchSourceBuilder search = new SearchSourceBuilder();
BoolQueryBuilder boolQb = QueryBuilders.boolQuery();
// 如果关键词带空格,则拆分
String[] ks = StringUtils.split(keyword, ' ');
for (String k : ks) {
for (String k : keywords) {
MatchPhraseQueryBuilder contentQb = QueryBuilders.matchPhraseQuery("textContent", k);
MatchPhraseQueryBuilder titleQb = QueryBuilders.matchPhraseQuery("title", k);
boolQb.should(contentQb);
......
......@@ -18,19 +18,21 @@ public class BizUtils {
return list.subList(start, realEnd);
}
public static String getThemeContent(String keyword, ThemeQo theme) {
public static String getThemeContent(String[] keywords, 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);
for (String keyword : keywords) {
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);
}
}
}
}
......
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