索引+搜索优化

This commit is contained in:
xiongxiaoyang 2020-05-20 21:23:59 +08:00
parent 8a628f081f
commit 002a0723f7
3 changed files with 177 additions and 168 deletions

View File

@ -11,17 +11,20 @@ PUT /novel
}, },
"authorName" : { "authorName" : {
"type" : "text", "type" : "text",
"analyzer": "ik_smart" "analyzer": "ik_smart",
"boost": 1
}, },
"bookName" : { "bookName" : {
"type" : "text", "type" : "text",
"analyzer": "ik_smart" "analyzer": "ik_smart",
"boost": 2
}, },
"bookDesc" : { "bookDesc" : {
"type" : "text", "type" : "text",
"analyzer": "ik_smart" "analyzer": "ik_smart",
"boost": 0.1
}, },
"bookStatus" : { "bookStatus" : {
@ -34,7 +37,8 @@ PUT /novel
"catName" : { "catName" : {
"type" : "text", "type" : "text",
"analyzer": "ik_smart" "analyzer": "ik_smart",
"boost": 0.5
}, },
"lastIndexId" : { "lastIndexId" : {
@ -43,7 +47,8 @@ PUT /novel
"lastIndexName" : { "lastIndexName" : {
"type" : "text", "type" : "text",
"analyzer": "ik_smart" "analyzer": "ik_smart",
"boost": 0.1
}, },
"lastIndexUpdateTime" : { "lastIndexUpdateTime" : {

View File

@ -64,7 +64,7 @@ public class BookServiceImpl implements BookService {
/** /**
* 本地图片保存路径 * 本地图片保存路径
* */ */
@Value("${pic.save.path}") @Value("${pic.save.path}")
private String picSavePath; private String picSavePath;
@ -111,7 +111,7 @@ public class BookServiceImpl implements BookService {
/** /**
* 初始化首页小说设置 * 初始化首页小说设置
* */ */
private List<BookSettingVO> initIndexBookSetting() { private List<BookSettingVO> initIndexBookSetting() {
Date currentDate = new Date(); Date currentDate = new Date();
List<Book> books = bookMapper.selectIdsByScoreAndRandom(Constants.INDEX_BOOK_SETTING_NUM); List<Book> books = bookMapper.selectIdsByScoreAndRandom(Constants.INDEX_BOOK_SETTING_NUM);
@ -185,7 +185,6 @@ public class BookServiceImpl implements BookService {
return result; return result;
} }
@SneakyThrows
@Override @Override
public PageInfo searchByPage(BookSP params, int page, int pageSize) { public PageInfo searchByPage(BookSP params, int page, int pageSize) {
@ -198,6 +197,9 @@ public class BookServiceImpl implements BookService {
} }
if (esEnable == 1) { if (esEnable == 1) {
try {
List<EsBookVO> bookList = new ArrayList<>(0); List<EsBookVO> bookList = new ArrayList<>(0);
//使用搜索引擎搜索 //使用搜索引擎搜索
@ -233,15 +235,17 @@ public class BookServiceImpl implements BookService {
boolQueryBuilder.filter(QueryBuilders.rangeQuery("lastIndexUpdateTime").gte(params.getUpdateTimeMin())); boolQueryBuilder.filter(QueryBuilders.rangeQuery("lastIndexUpdateTime").gte(params.getUpdateTimeMin()));
} }
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(boolQueryBuilder); searchSourceBuilder.query(boolQueryBuilder);
Count count = new Count.Builder().addIndex("novel").addType("book") Count count = new Count.Builder().addIndex("novel").addType("book")
.query(searchSourceBuilder.toString()).build(); .query(searchSourceBuilder.toString()).build();
CountResult results = jestClient.execute(count); CountResult results = jestClient.execute(count);
Double total = results.getCount(); Double total = results.getCount();
// 设置高亮字段
// 临时屏蔽小程序未处理的高亮字段等小程序处理后再放开 // 临时屏蔽小程序未处理的高亮字段等小程序处理后再放开
HighlightBuilder highlightBuilder = new HighlightBuilder(); HighlightBuilder highlightBuilder = new HighlightBuilder();
@ -251,9 +255,10 @@ public class BookServiceImpl implements BookService {
highlightBuilder.field("lastIndexName"); highlightBuilder.field("lastIndexName");
highlightBuilder.field("catName"); highlightBuilder.field("catName");
highlightBuilder.preTags("<span style='color:red'>").postTags("</span>"); highlightBuilder.preTags("<span style='color:red'>").postTags("</span>");
highlightBuilder.fragmentSize(200); highlightBuilder.fragmentSize(20000);
searchSourceBuilder.highlighter(highlightBuilder); searchSourceBuilder.highlighter(highlightBuilder);
//设置排序 //设置排序
if (params.getSort() != null) { if (params.getSort() != null) {
searchSourceBuilder.sort(StringUtil.camelName(params.getSort()), SortOrder.DESC); searchSourceBuilder.sort(StringUtil.camelName(params.getSort()), SortOrder.DESC);
@ -268,6 +273,7 @@ public class BookServiceImpl implements BookService {
log.debug(search.toString()); log.debug(search.toString());
SearchResult result; SearchResult result;
result = jestClient.execute(search); result = jestClient.execute(search);
if (result.isSucceeded()) {
log.debug(result.getJsonString()); log.debug(result.getJsonString());
Map resultMap = new ObjectMapper().readValue(result.getJsonString(), Map.class); Map resultMap = new ObjectMapper().readValue(result.getJsonString(), Map.class);
@ -316,7 +322,6 @@ public class BookServiceImpl implements BookService {
} }
} }
} }
} }
@ -327,19 +332,19 @@ public class BookServiceImpl implements BookService {
pageInfo.setPageNum(page); pageInfo.setPageNum(page);
pageInfo.setPageSize(pageSize); pageInfo.setPageSize(pageSize);
return pageInfo; return pageInfo;
}
}catch (Exception e){
log.error(e.getMessage(),e);
}
}
}else{
PageHelper.startPage(page, pageSize); PageHelper.startPage(page, pageSize);
if (StringUtils.isNotBlank(params.getSort())) { if (StringUtils.isNotBlank(params.getSort())) {
OrderByHelper.orderBy(params.getSort() + " desc"); OrderByHelper.orderBy(params.getSort() + " desc");
} }
return new PageInfo<>(bookMapper.searchByPage(params)); return new PageInfo<>(bookMapper.searchByPage(params));
}
} }
@ -567,7 +572,6 @@ public class BookServiceImpl implements BookService {
} }
@Override @Override
public Long queryIdByNameAndAuthor(String bookName, String author) { public Long queryIdByNameAndAuthor(String bookName, String author) {
//查询小说ID //查询小说ID
@ -637,7 +641,8 @@ public class BookServiceImpl implements BookService {
if (queryIdByNameAndAuthor(book.getBookName(), penName) != null) { if (queryIdByNameAndAuthor(book.getBookName(), penName) != null) {
//该作者发布过此书名的小说 //该作者发布过此书名的小说
throw new BusinessException(ResponseStatus.BOOKNAME_EXISTS); throw new BusinessException(ResponseStatus.BOOKNAME_EXISTS);
}; }
;
book.setAuthorName(penName); book.setAuthorName(penName);
book.setAuthorId(authorId); book.setAuthorId(authorId);
book.setVisitCount(0L); book.setVisitCount(0L);
@ -711,7 +716,6 @@ public class BookServiceImpl implements BookService {
bookContentMapper.insertSelective(bookContent); bookContentMapper.insertSelective(bookContent);
} }
@Override @Override

View File

@ -11,7 +11,7 @@ spring:
#是否开启搜索引擎1开启0不开启 #是否开启搜索引擎1开启0不开启
enable: 0 enable: 0
jest: jest:
uris: http://198.245.61.51:9200 uris: http://127.0.0.1:9200