日期:2014-05-20  浏览次数:20794 次

哎呀快急死了,我做的是一个搜索引擎,从一个界面中输入关键字,获取关键字后,中间用servlet处理查找索引,把搜索的结果显示在另外一个界面中,
哎呀快急死了,我做的是一个搜索引擎,从一个界面中输入关键字,获取关键字后,中间用servlet处理查找索引,把搜索的结果显示在另外一个界面中,下面是我servlet代码:
package search;

import java.io.*;
import org.apache.lucene.analysis.*;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.lucene.*;
import org.apache.lucene.search.*;
import org.apache.lucene.queryParser.*;
import org.apache.lucene.store.*;  
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import javax.servlet.Servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.lucene.analysis.standard.StandardAnalyzer;  
import org.apache.lucene.document.Document;  
import org.apache.lucene.search.BooleanClause;  
  import org.apache.lucene.util.Version;  
   
   
public class Searchxml extends HttpServlet
{  
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException  
{
this.doPost(req, resp);
}

  
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
resp.setContentType("text/html;charset=GB2312");
// PrintWriter out=resp.getWriter();
String path3="show.jsp";/*跳转路径*/
String str1=req.getParameter("txta");/*从前台获取的关键字*/
str1 = new String(str1.getBytes("ISO8859-1"), "utf-8");
//str1 = new String(str1.getBytes("ISO8859-1"), "utf-8");
String indexDir = "e:\\index"; /*索引文件存放路径*/
String message=" ";
  
  
int tiao;  
int all;
float time;
  
Directory dir = new SimpleFSDirectory(new File(indexDir));  
  
IndexSearcher indexSearch = new IndexSearcher(dir); /*打开索引文件*/
try{  
String[] fields = {"title","author","keywords","kind","date"};/*进行索引的域*/
  
//BooleanClause.Occur[] flags=new BooleanClause.Occur[]{BooleanClause.Occur.SHOULD,BooleanClause.Occur.SHOULD,BooleanClause.Occur.MUST,BooleanClause.Occur.SHOULD};
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);/*采用lucence标准分词器对获取的关键字进行分词*/
  
QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_30,fields,analyzer); /*进行多域搜索*/
// parser.setDefaultOperator(QueryParser.AND_OPERATOR);  
  
long startTime = new Date().getTime(); /*查询开始计时*/
  
Query query = parser.parse(str1);
  
all=indexSearch.maxDoc(); /*索引条数*/
  
TopDocs hits = indexSearch.search(query, all); /*从所有的索引中找出符合条件的资源*/
long endTime = new Date().getTime();
tiao=hits.totalHits;
time=endTime - startTime;
time=time/1000;
  
HttpSession session = req.getSession(true);  

  
  
if(tiao!=0)
  
{ message="总共"+all+"条资源"+"问源找到了"+tiao+"条资源"+" "+"总共用时"+time+"秒";
int i=0;
ArrayList list=new ArrayList();
for ( i = 0; i < hits.scoreDocs.length; i++)  
{  
ScoreDoc sdoc = hits.scoreDocs[i];  
Document doc = indexSearch.doc(sdoc.doc);
list.add(doc.get("title"));
list.add(doc.get("author"));
list.add(doc.get("keywords"));
list.add(doc.get("kind"));
list.add(doc.get("describe"));
list.add(doc.get("url"));
list.add(doc.get("date"));
list.add(doc.get("publisher"));