Feat: [后台]:新增同步某人书签从mysql到es接口

This commit is contained in:
fanxb 2019-07-26 15:50:00 +08:00
parent b5d64078c3
commit b0b912dd0a
4 changed files with 69 additions and 2 deletions

View File

@ -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);
}
}

View File

@ -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<Integer> 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<com.fanxb.bookmark.business.bookmark.entity.BookmarkEs>
*/
List<BookmarkEs> selectBookmarkEsByUserIdAndType(@Param("userId") int userId, @Param("type") int type, @Param("start") int start, @Param("size") int size);
}

View File

@ -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<BookmarkEs> 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<EsEntity> 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);
}
}

View File

@ -110,5 +110,17 @@
where userId = #{userId} and bookmarkId = #{bookmarkId}
</update>
<select id="selectBookmarkEsByUserIdAndType" resultType="com.fanxb.bookmark.business.bookmark.entity.BookmarkEs">
select
userId,
bookmarkId,
name,
url
from bookmark
where userId = #{userId} and type = #{type}
order by bookmarkId
limit ${start}, ${size}
</select>
</mapper>