首页 > 代码库 > 利用lucene创建实现全站新闻搜索

利用lucene创建实现全站新闻搜索

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<NewsInfo> list;

private String path;

public LuceneUtil(List<NewsInfo> 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<list.size();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<hitsTitle.length();i++){

Document doc =  hitsTitle.doc(i);

       String idi=doc.get("id" );

       Integer id=Integer.parseInt(idi);

       idList.add(id);

}

for(int i=0;i<hitsContent.length();i++){

boolean flag=true;

Document doc =  hitsContent.doc(i);

       String idi=doc.get("id" );

       Integer id=Integer.parseInt(idi);

       for(int j=0;j<idList.size();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<NewsInfo> searchList(String search,String path) throws Exception{

File indexDir = new File(path);

List<NewsInfo> list=new ArrayList<NewsInfo>();

List<NewsInfo> listNews=new ArrayList<NewsInfo>();

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<listId.size();i++){

int id=(Integer) listId.get(i);

NewsInfo news=newsDao.findOne(id);

listNews.add(news);

}

return listNews;

}

/**

* 查询已发表的新闻并建立索引

*/

public void buildSearch(String path){

List<NewsInfo> list=new ArrayList<NewsInfo>();

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="http://www.mamicode.com/{search}/search/{currentPageNo}/page")

public String searchNews(@PathVariable String search, @PathVariable String currentPageNo,Model model,HttpServletRequest request){

List<NewsInfo> newsList;

List<NewsInfo> pageList=new ArrayList<NewsInfo>();

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<end;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");

%>

<!DOCTYPE HTML>

<html>

  <head>

    <base href="http://www.mamicode.com/">

    <title></title>       

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<script type="text/javascript" src="http://www.mamicode.com/include/common.js.jsp"></script>

  </head>

<body>

<div id="header">

<%@include file="../common/header.jsp" %>

</div>

<div class="wra">

  <div class="now_position">目前位置:<a href="http://www.mamicode.com/index">首页</a> - 搜索</div>

  <div class="content">

    <div class="content_L">

      <div class="news_list" >

        <ul>

        <c:forEach var="news" items="${searchList}" varStatus="status"> 

        <c:choose>

        <c:when test="${news.image==null||news.image==‘‘}">

         <li>

           <div class="tit"> <a href="http://www.mamicode.com/newsInfo/${news.id}" target="_blank">${news.title}</a> </div>

           <div class="con"> <a href="http://www.mamicode.com/newsInfo/${news.id}" target="_blank">${news.content}</a> </div>

         </li>

         </c:when>

         <c:otherwise>

          <li> 

          <a href="http://www.mamicode.com/newsInfo/${news.id}" target="_blank"> <img src="http://www.mamicode.com/${news.image}" /></a>

           <dl>

             <dt> <a href="http://www.mamicode.com/newsInfo/${news.id}" target="_blank">${news.title}</a> </dt>

             <dd> <a href="http://www.mamicode.com/newsInfo/${news.id}" target="_blank"> ${news.content}</a></dd>

           </dl>

          </li>

         </c:otherwise>

        </c:choose>

          </c:forEach>

        </ul>

         <div class="paging"> 

         <a href="http://www.mamicode.com//search//page"><上一页</a> 

          <%for(int z=1;z<=max;z++){ %>

<a href="http://www.mamicode.com//search//page"><%=z %></a>&nbsp;&nbsp;

 <% }%>

         <a href="http://www.mamicode.com//search//page">下一页 ></a> </div>

      </div>

    </div>

    <div class="content_R">

      <div class="hot_tag_1">

        <strong>热门标签</strong>

        <p>

          <a href="http://www.mamicode.com/guide/1" target="_blank" class="hot_co_1">童年阶段</a>

          <a href="http://www.mamicode.com/guide/2" target="_blank" class="hot_co_2">学生时代</a>

          <a href="http://www.mamicode.com/guide/3" target="_blank" class="hot_co_3">单身贵族</a>

          <a href="http://www.mamicode.com/guide/4" target="_blank" class="hot_co_3">踏入社会</a>

          <a href="http://www.mamicode.com/guide/5" target="_blank" class="hot_co_4">新婚燕尔</a>

          <a href="http://www.mamicode.com/guide/6" target="_blank" class="hot_co_5">三口之家</a>

          <a href="http://www.mamicode.com/guide/7" target="_blank" class="hot_co_6">财富传承</a>

          <a href="http://www.mamicode.com/guide/8" target="_blank" class="hot_co_7">企业团体</a>

          <a href="http://www.mamicode.com/guide/9" target="_blank" class="hot_co_3">车险攻略</a>

          <a href="http://www.mamicode.com/guide/10" target="_blank" class="hot_co_4">走进社保</a>

        </p>

      </div>

      <div class="s_ad_1" id="s_ad_1"></div>

    </div>

  </div>

</div>

<%@include file="../common/footer.jsp" %>

  </body>

  <script type="text/javascript" src="http://www.mamicode.com/js/view/web/common/right_picture.js"></script>

</html>


实现效果,第一次搜索的时候进行索引建立,当后台对新闻进行相关操作的时候进行线程更新索引。

本文出自 “只争朝夕” 博客,谢绝转载!