Commit ab13575d authored by 张辰's avatar 张辰

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

parent ca40b021
...@@ -103,8 +103,10 @@ public class ThemeManager { ...@@ -103,8 +103,10 @@ public class ThemeManager {
Integer from = (pageNo - 1) * pageSize; Integer from = (pageNo - 1) * pageSize;
ThemeFullSearchResp resp = new ThemeFullSearchResp(); 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()) { if (esIds.isEmpty()) {
return resp; return resp;
} }
...@@ -123,7 +125,7 @@ public class ThemeManager { ...@@ -123,7 +125,7 @@ public class ThemeManager {
}); });
// 截取关键词出现的那一部分段落 // 截取关键词出现的那一部分段落
for (ThemeQo theme : resp.themes) { 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())); excludeIds.addAll(resp.themes.stream().map(ThemeQo::getThemeId).collect(Collectors.toList()));
......
...@@ -42,14 +42,13 @@ public class ESService { ...@@ -42,14 +42,13 @@ public class ESService {
helper.insert(String.valueOf(qo.themeType), qo.themeId, JSON.toJSONString(qo)); 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(); SearchSourceBuilder search = new SearchSourceBuilder();
BoolQueryBuilder boolQb = QueryBuilders.boolQuery(); BoolQueryBuilder boolQb = QueryBuilders.boolQuery();
// 如果关键词带空格,则拆分 // 如果关键词带空格,则拆分
String[] ks = StringUtils.split(keyword, ' '); for (String k : keywords) {
for (String k : ks) {
MatchPhraseQueryBuilder contentQb = QueryBuilders.matchPhraseQuery("textContent", k); MatchPhraseQueryBuilder contentQb = QueryBuilders.matchPhraseQuery("textContent", k);
MatchPhraseQueryBuilder titleQb = QueryBuilders.matchPhraseQuery("title", k); MatchPhraseQueryBuilder titleQb = QueryBuilders.matchPhraseQuery("title", k);
boolQb.should(contentQb); boolQb.should(contentQb);
......
...@@ -18,19 +18,21 @@ public class BizUtils { ...@@ -18,19 +18,21 @@ public class BizUtils {
return list.subList(start, realEnd); 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) { for (ThemeContentQo paragraph : theme.content) {
if (paragraph.getType().equals(RelTypeEnum.TEXT.type)) { if (paragraph.getType().equals(RelTypeEnum.TEXT.type)) {
int idx = paragraph.getValue().indexOf(keyword); for (String keyword : keywords) {
int idx = paragraph.getValue().indexOf(keyword);
if (idx == -1) {
continue; if (idx == -1) {
} else if (idx < 30) { continue;
// 如果关键词在段落偏头部的部分,则全部返回给前端,前端自由展示 } else if (idx < 30) {
return paragraph.getValue(); // 如果关键词在段落偏头部的部分,则全部返回给前端,前端自由展示
} else { return paragraph.getValue();
// 否则,保留关键词 向前20个字符 } else {
return "..." + paragraph.getValue().substring(idx - 20); // 否则,保留关键词 向前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