From b0b912dd0a25d923718f8d97480eaad38a079edf Mon Sep 17 00:00:00 2001 From: fanxb Date: Fri, 26 Jul 2019 15:50:00 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Feat:=20[=E5=90=8E=E5=8F=B0]:?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=90=8C=E6=AD=A5=E6=9F=90=E4=BA=BA=E4=B9=A6?= =?UTF-8?q?=E7=AD=BE=E4=BB=8Emysql=E5=88=B0es=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BookmarkController.java | 13 ++++++++ .../business/bookmark/dao/BookmarkDao.java | 14 ++++++++ .../bookmark/service/BookmarkService.java | 32 +++++++++++++++++-- .../mapper/bookmark-bookmarkMapper.xml | 12 +++++++ 4 files changed, 69 insertions(+), 2 deletions(-) diff --git a/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/controller/BookmarkController.java b/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/controller/BookmarkController.java index f558cb1..2f19688 100644 --- a/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/controller/BookmarkController.java +++ b/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/controller/BookmarkController.java @@ -118,4 +118,17 @@ public class BookmarkController { return Result.success(res); } + /** + * Description: 同步当前用户的书签到es中 + * + * @return com.fanxb.bookmark.common.entity.Result + * @author fanxb + * @date 2019/7/26 15:33 + */ + @PostMapping("/syncBookmark") + public Result syncBookmark() { + bookmarkService.syncUserBookmark(UserContextHolder.get().getUserId()); + return Result.success(null); + } + } diff --git a/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/dao/BookmarkDao.java b/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/dao/BookmarkDao.java index 6259c48..2fe8fa3 100644 --- a/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/dao/BookmarkDao.java +++ b/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/dao/BookmarkDao.java @@ -1,5 +1,6 @@ package com.fanxb.bookmark.business.bookmark.dao; +import com.fanxb.bookmark.business.bookmark.entity.BookmarkEs; import com.fanxb.bookmark.common.entity.Bookmark; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Component; @@ -144,4 +145,17 @@ public interface BookmarkDao { */ List getChildrenBookmarkId(@Param("userId") int userId, @Param("folderId") int folderId); + /** + * Description: 根据用户id,类别,分页查找书签 + * + * @author fanxb + * @date 2019/7/26 15:23 + * @param userId userId + * @param type type + * @param start start + * @param size size + * @return java.util.List + */ + List selectBookmarkEsByUserIdAndType(@Param("userId") int userId, @Param("type") int type, @Param("start") int start, @Param("size") int size); + } diff --git a/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/service/BookmarkService.java b/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/service/BookmarkService.java index 6fbca3b..c2ff064 100644 --- a/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/service/BookmarkService.java +++ b/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/service/BookmarkService.java @@ -9,8 +9,10 @@ import com.fanxb.bookmark.common.entity.EsEntity; import com.fanxb.bookmark.common.exception.CustomException; import com.fanxb.bookmark.common.util.EsUtil; import com.fanxb.bookmark.common.util.UserContextHolder; +import lombok.extern.slf4j.Slf4j; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; @@ -35,6 +37,7 @@ import java.util.Set; * @date 2019/7/8 15:00 */ @Service +@Slf4j public class BookmarkService { /** * chrome导出书签tag @@ -245,6 +248,7 @@ public class BookmarkService { } //更新被移动节点的path和sort bookmarkDao.updatePathAndSort(userId, body.getBookmarkId(), body.getTargetPath(), body.getSort()); + log.info("{},从{}移动到{},sort:{}", userId, body.getSourcePath(), body.getTargetPath(), body.getSort()); } /** @@ -262,8 +266,32 @@ public class BookmarkService { SearchSourceBuilder builder = new SearchSourceBuilder(); builder.size(5); builder.query(boolQueryBuilder); - List list = esUtil.search(EsConstant.BOOKMARK_INDEX, builder, BookmarkEs.class); - return list; + return esUtil.search(EsConstant.BOOKMARK_INDEX, builder, BookmarkEs.class); } + /** + * Description: 将某个用户的书签数据mysql同步到es中 + * + * @author fanxb + * @date 2019/7/26 11:27 + */ + public void syncUserBookmark(int userId) { + //删除旧的数据 + esUtil.deleteByQuery(EsConstant.BOOKMARK_INDEX, new TermQueryBuilder("userId", userId)); + int index = 0; + int size = 500; + List res = new ArrayList<>(); + do { + res.clear(); + bookmarkDao.selectBookmarkEsByUserIdAndType(userId, 0, index, size) + .forEach(item -> res.add(new EsEntity<>(item.getBookmarkId().toString(), item))); + if (res.size() > 0) { + esUtil.insertBatch(EsConstant.BOOKMARK_INDEX, res); + } + index += size; + } while (res.size() == 500); + + } + + } diff --git a/bookMarkService/business/bookmark/src/main/resources/mapper/bookmark-bookmarkMapper.xml b/bookMarkService/business/bookmark/src/main/resources/mapper/bookmark-bookmarkMapper.xml index fd0a434..b4344e5 100644 --- a/bookMarkService/business/bookmark/src/main/resources/mapper/bookmark-bookmarkMapper.xml +++ b/bookMarkService/business/bookmark/src/main/resources/mapper/bookmark-bookmarkMapper.xml @@ -110,5 +110,17 @@ where userId = #{userId} and bookmarkId = #{bookmarkId} + + \ No newline at end of file