使用Lucene和IKAnalyzer做字符串分词的实例

2018-01-13 11:14:47来源:oschina作者:源哥L人点击

分享
// 添加搜索记录
@Transactional
private void addSearchLog(String content) throws IOException {
if(!StringUtils.isEmpty(content)){
// 分词
StringReader sr = new StringReader(content);
IKSegmenter ik = new IKSegmenter(sr, true);
Lexeme lex = null;
SearchLog searchLog = null;
while ((lex = ik.next()) != null) {
searchLog = searchLogDao.getSearchLog(lex.getLexemeText());
if(searchLog == null){
searchLog = new SearchLog();
searchLog.setLogCount(1);
searchLog.setName(lex.getLexemeText());
searchLogDao.save(searchLog);
}else{
searchLog.setLogCount(searchLog.getLogCount() + 1);
searchLogDao.save(searchLog);
}
} long count = searchLogDao.count(); // 删除多出总记录的记录数据
if (count > 1000) {
List lastLogList = searchLogDao.getSearchLog(new PageRequest(0, (int)count - 1000, Direction.ASC, "lastTime","logCount"));
List ids = new ArrayList();
for (int i = 0; i < lastLogList.size(); i++) {
ids.add(lastLogList.get(i).getId());
}
searchLogDao.deleteLastSearchLog(ids);
}
}
}
爱小板凳小编整理:
电影天堂

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台