使用Lucene跟IKAnalyzer做字符串分词的实例
使用Lucene和IKAnalyzer做字符串分词的实例
// 添加搜索记录 @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<SearchLog> lastLogList = searchLogDao.getSearchLog(new PageRequest(0, (int)count - 1000, Direction.ASC, "lastTime","logCount")); List<Long> ids = new ArrayList<Long>(); for (int i = 0; i < lastLogList.size(); i++) { ids.add(lastLogList.get(i).getId()); } searchLogDao.deleteLastSearchLog(ids); } } }