文章目录
pom.xml
导入redis的驱动包
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
public class BlogDao extends JsonBaseDao{
public IndexWriter setIndexWriter() throws Exception {
IndexWriterConfig conf = new IndexWriterConfig(new SmartChineseAnalyzer());
Directory d = FSDirectory.open(Paths.get(PropertiesUtil.getValue("indexPath")));
IndexWriter indexWriter = new IndexWriter(d , conf );
return indexWriter;
}
public List<Map<String,Object>> list(String title, PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql = "select * from t_lucene_crawler_blog where 1=1";
if(StringUtils.isNotBlank(title)) {
sql += " and title like '%"+title+"%'";
}
return super.executeQuery(sql, pageBean);
}
public int save(Map<String,String[]> paMap) throws InstantiationException, IllegalAccessException, SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IOException{
String sql = "insert into t_lucene_crawler_blog values(?,?,?,?,0)";
IndexWriter indexWriter = null;
try {
indexWriter = setIndexWriter();
// 为数据库中的所有数据构建索引
Document doc = new Document();
doc.add(new StringField("id", JsonUtils.getParamVal(paMap, "id"), Field.Store.YES));
doc.add(new TextField("title", JsonUtils.getParamVal(paMap, "title"), Field.Store.YES));
doc.add(new StringField("url", JsonUtils.getParamVal(paMap, "url"), Field.Store.YES));
indexWriter.addDocument(doc);
} catch (Exception e) {
e.printStackTrace();
}finally {
indexWriter.close();
}
return super.executeUpdate(sql, new String[] {"id","title","content","url"}, paMap);
}
public int del(Map<String,String[]> paMap) throws InstantiationException, IllegalAccessException, SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IOException{
String sql = "delete from t_lucene_crawler_blog where id = ?";
IndexWriter indexWriter = null;
try {
indexWriter = setIndexWriter();
System.out.println("最大文档数:"+indexWriter.maxDoc());
indexWriter.deleteDocuments(new Term("id", JsonUtils.getParamVal(paMap, "id")));
indexWriter.forceMergeDeletes(); //强制删除
indexWriter.commit();
} catch (Exception e) {
e.printStackTrace();
}finally {
indexWriter.close();
}
return super.executeUpdate(sql, new String[] {"id"}, paMap);
}
/**
* 刷新全局索引文件
* @return
* @throws IOException
*/
public String reflush() throws IOException {
//删除所有的索引文件
File[] flag = (new File(PropertiesUtil.getValue("indexPath"))).listFiles();
for (File file : flag) {
file.delete();
}
IndexWriter indexWriter = null;
try {
indexWriter = setIndexWriter();
List<Map<String, Object>> list = list(null, null);
for (Map<String, Object> map : list) {
Document doc = new Document();
doc.add(new StringField("id", (String) map.get("id"), Field.Store.YES));
// TextField用于对一句话分词处理 java培训机构
doc.add(new TextField("title", (String) map.get("title"), Field.Store.YES));
doc.add(new StringField("url", (String) map.get("url"), Field.Store.YES));
indexWriter.addDocument(doc);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
indexWriter.close();
}
return "刷新成功";
}
}
BlogAction
public class BlogAction {
private String title;
private BlogDao blogDao = new BlogDao();
private static Jedis jedis;
static {
jedis = new Jedis("192.168.239.128", 6379);
jedis.auth("123456");//权限认证
jedis.select(0);
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String list() {
try {
HttpServletRequest request = ServletActionContext.getRequest();
if (StringUtils.isBlank(title)) {
String blogList = jedis.get("blogList");
String blogstate = jedis.get("blogstate");
if(blogList!=null && blogstate==null ) {//判断redis里面是否有数据
//直接从缓存里拿值
blogList = jedis.get("blogList");
System.out.println("从缓存拿值");
}
else {//没有数据就创建一个blogList
List<Map<String, Object>> blogListall = blogDao.list(null, null);
//把数据库查来的值放到blogList里
jedis.set("blogList", JSON.toJSONString(blogListall));
//拿取数据
blogList = jedis.get("blogList");
//清除状态
jedis.del("blogstate");
System.out.println("从数据库拿值");
}
request.setAttribute("blogList", JSON.parse(blogList));
}else {
Directory directory = LuceneUtil.getDirectory(PropertiesUtil.getValue("indexPath"));
DirectoryReader reader = LuceneUtil.getDirectoryReader(directory);
IndexSearcher searcher = LuceneUtil.getIndexSearcher(reader);
SmartChineseAnalyzer analyzer = new SmartChineseAnalyzer();
Query query = new QueryParser("title", analyzer).parse(title);
Highlighter highlighter = LuceneUtil.getHighlighter(query, "title");
TopDocs topDocs = searcher.search(query , 100);
List<Map<String, Object>> blogList = new ArrayList<>();
Map<String, Object> map = null;
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
map = new HashMap<>();
Document doc = searcher.doc(scoreDoc.doc);
map.put("id", doc.get("id"));
String titleHighlighter = doc.get("title");
if(StringUtils.isNotBlank(titleHighlighter)) {
titleHighlighter = highlighter.getBestFragment(analyzer, "title", titleHighlighter);
}
map.put("title", titleHighlighter);
map.put("url", doc.get("url"));
blogList.add(map);
}
request.setAttribute("blogList", blogList);
}
} catch (Exception e) {
e.printStackTrace();
}
return "blogList";
}
public String add() {
HttpServletRequest request = ServletActionContext.getRequest();
try {
int save = blogDao.save(request.getParameterMap());
//改变state状态
jedis.set("blogstate", "1");
}catch (InstantiationException | IllegalAccessException | NoSuchFieldException | SecurityException
| IllegalArgumentException | SQLException | IOException e) {
e.printStackTrace();
}
return "toList";
}
public String del() {
HttpServletRequest request = ServletActionContext.getRequest();
try {
int save = blogDao.del(request.getParameterMap());
jedis.set("blogstate", "1");
} catch (InstantiationException | IllegalAccessException | NoSuchFieldException | SecurityException
| IllegalArgumentException | SQLException | IOException e) {
e.printStackTrace();
}
return "toList";
}
public String reflush() {
HttpServletResponse response = ServletActionContext.getResponse();
try {
String o= blogDao.reflush();
ObjectMapper om = new ObjectMapper();
response.setContentType("text/html;charset=utf-8");
PrintWriter out= response.getWriter();
out.println(om.writeValueAsString(o).toString());
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
运行
说明:第一次登陆进去是访问数据库,后面的一直是从缓存中获取数据,相比而言打开的速度要比第一次访问快的很多 。