作者:月舞B的啊 | 来源:互联网 | 2023-08-31 10:59
jar包:lucene-core-2.3.2.jar到相关官网下载建立线程通用类LuceneUtilimportjava.io.File;importjava.io.IOExcep
jar包:lucene-core-2.3.2.jar 到相关官网下载
//建立线程通用类LuceneUtil
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
public class LuceneUtil implements Runnable{
private List list;
private String path;
public LuceneUtil(List list,String path){
this.list=list;
this.path=path;
}
/**
* 对数据库插叙结果List以及软路径建立索引
* @param list
*/
public void searchBuild() throws Exception {
File indexDir = new File(path);
Analyzer luceneAnalyzer = new StandardAnalyzer();
IndexWriter indexWriter = new IndexWriter(indexDir, luceneAnalyzer, true );
if(list!=null){
for(int i=0;i Document document = new Document();
document.add(new Field("id", String.valueOf(list.get(i).getId()),
Field.Store.YES, Field.Index.NO));
document.add(new Field("title", list.get(i).getTitle(), Field.Store.YES,
Field.Index.TOKENIZED));
document.add(new Field("content", list.get(i).getContent(), Field.Store.YES,
Field.Index.TOKENIZED));
document.add(new Field("updateTime", list.get(i).getPublishTime().toString(), Field.Store.YES,
Field.Index.NO));
indexWriter.addDocument(document);
}
indexWriter.optimize();
indexWriter.close();
}
}
/**
* 通过传递的内容以及路径地址进行搜索
* @param search
* @return
*/
public List searchList(String search,String path)throws IOException, ParseException{
List idList=new ArrayList();
Hits hitsTitle = null ;
Hits hitsCOntent= null ;
IndexSearcher searcher = new IndexSearcher(path);
Analyzer analyzer = new StandardAnalyzer();
QueryParser qpTitle = new QueryParser("title", analyzer);
Query queryTitle = qpTitle.parse(search);
hitsTitle = searcher.search(queryTitle);
QueryParser qpCOntent= new QueryParser("content", analyzer);
Query queryCOntent= qpContent.parse(search);
hitsCOntent= searcher.search(queryContent);
for(int i=0;i Document doc = hitsTitle.doc(i);
String idi=doc.get("id" );
Integer id=Integer.parseInt(idi);
idList.add(id);
}
for(int i=0;i boolean flag=true;
Document doc = hitsContent.doc(i);
String idi=doc.get("id" );
Integer id=Integer.parseInt(idi);
for(int j=0;j Integer idd=(Integer) idList.get(j);
if(id==idd){
flag=false;
}
}
if(flag)
{
idList.add(id);
}
}
return idList;
}
/**
* 重写run()方法实现线程建立索引
*/
public void run(){
try {
searchBuild();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//service层方法
/**
* 新闻搜索
* @param search
* @return
* @throws Exception
*/
public List searchList(String search,String path) throws Exception{
File indexDir = new File(path);
List list=new ArrayList();
List listNews=new ArrayList();
list=newsDao.searchList();
LuceneUtil luceneUtil =new LuceneUtil(list,path);
if(!indexDir.exists())
{
luceneUtil.searchBuild();
}
List listId=luceneUtil.searchList(search,path);
for(int i=0;i int id=(Integer) listId.get(i);
NewsInfo news=newsDao.findOne(id);
listNews.add(news);
}
return listNews;
}
/**
*
* 查询已发表的新闻并建立索引
*/
public void buildSearch(String path){
List list=new ArrayList();
list=newsDao.searchList();
LuceneUtil luceneUtil =new LuceneUtil(list,path);
Thread thread=new Thread(luceneUtil);
thread.start();
}
//Controller层分页查询
package com.redwolfsoft.bxzk.controller.news;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class NewsSearch {
@Autowired
private NewsService newsService;
/**
* 搜索新闻
* @param search
* @param currentPageNo
* @return
*/
@RequestMapping(value="/{search}/search/{currentPageNo}/page")
public String searchNews(@PathVariable String search, @PathVariable String currentPageNo,Model model,HttpServletRequest request){
List newsList;
List pageList=new ArrayList();
StringBuffer root = new StringBuffer(request.getSession()
.getServletContext().getRealPath("/"));
root.append("indexFile");
int pageNo=1;
int current=1;
try {
newsList = newsService.searchList(search,root.toString());
int count=newsList.size();
if(count>10){
pageNo=count/10+1;
if(currentPageNo!=null && !"".equals(currentPageNo)){
current=Integer.parseInt(currentPageNo);
if(current<1){
current=1;
}
if(current>pageNo){
current=pageNo;
}
int start=(current-1)*10;
int end=current*10;
if(end>count){
end=count;
}
for(int i=start;i pageList.add(newsList.get(i));
}
model.addAttribute("searchList", pageList);
}
}else{
model.addAttribute("searchList", newsList);
}
model.addAttribute("search", search);
model.addAttribute("pageNo", pageNo);
model.addAttribute("current", current);
} catch (Exception e) {
e.printStackTrace();
}
return "web/search/search";
}
}
//JSP页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
int max=(Integer)request.getAttribute("pageNo");
String search=(String)request.getAttribute("search");
int current=(Integer)request.getAttribute("current");
%>
<%@include file="../common/footer.jsp" %>
实现效果,第一次搜索的时候进行索引建立,当后台对新闻进行相关操作的时候进行线程更新索引。
本文出自 “只争朝夕” 博客,谢绝转载!
利用lucene创建实现全站新闻搜索,布布扣,bubuko.com