Jacry 2012-02-07
创建索引这里是以一张表中两个字段创建一个索引
<%@pagelanguage="java"pageEncoding="GBK"%>
<%@pageimport="org.apache.lucene.document.Document"%>
<%@pageimport="org.apache.lucene.document.Field"%>
<%@pageimport="org.apache.lucene.store.FSDirectory"%>
<%@pageimport="org.apache.lucene.store.Directory"%>
<%@pageimport="org.apache.lucene.index.IndexWriter"%>
<%@pageimport="java.io.File"%>
<%@pageimport="org.apache.lucene.analysis.standard.StandardAnalyzer"%>
<%@pageimport="org.apache.lucene.analysis.Analyzer"%>
<%@includefile="/common/common.jsp"%>
<%
GoodsServicegoodsService=GoodsService.instance();
List<Goods>list=goodsService.getLatestList(1,200);
Directorydirectory=FSDirectory.open(newFile("e:\\index"));
Analyzeranalyzer=newStandardAnalyzer();
IndexWriterindexWriter=newIndexWriter(directory,analyzer,true);
longbegin=System.currentTimeMillis();
for(inti=0;i<list.size();i++){
Goodsgoods=list.get(i);
Documentdoc=newDocument();
Fieldid=newField("id",goods.getId()+"",Field.Store.YES,Field.Index.NOT_ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS);
Fieldtitle=newField("title",goods.getTitle(),Field.Store.YES,Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS);
doc.add(id);
doc.add(title);
indexWriter.addDocument(doc);
}
longend=System.currentTimeMillis();
System.out.println("建索引需要的时间:"+(end-begin));
indexWriter.optimize();
indexWriter.close();
%>
实现搜索
<%@pagelanguage="java"pageEncoding="GBK"%>
<%@pageimport="org.apache.lucene.document.Document"%>
<%@pageimport="org.apache.lucene.search.IndexSearcher"%>
<%@pageimport="org.apache.lucene.analysis.standard.StandardAnalyzer"%>
<%@pageimport="org.apache.lucene.analysis.Analyzer"%>
<%@pageimport="java.io.File"%>
<%@pageimport="org.apache.lucene.queryParser.QueryParser"%>
<%@pageimport="org.apache.lucene.store.FSDirectory"%>
<%@pageimport="org.apache.lucene.store.Directory"%>
<%@pageimport="org.apache.lucene.search.Query"%>
<%@pageimport="org.apache.lucene.search.Hits"%>
<%@includefile="/common/common.jsp"%>
<%
Stringkeyword=T.stringValue(request.getParameter("k"),null);
GoodsServicegoodsService=GoodsService.instance();
Goodsgoods=null;
if(keyword!=null){
Hitshits=null;
Queryquery=null;
Directorydirectory=FSDirectory.open(newFile("e:\\index"));
IndexSearcherindexSearcher=newIndexSearcher(directory);
Analyzeranalyzer=newStandardAnalyzer();
try{
QueryParserqueryParser=newQueryParser("title",analyzer);
query=queryParser.parse(keyword);
}catch(Exceptione){
}
if(indexSearcher!=null){
hits=indexSearcher.search(query);
if(hits.length()>0){
out.println("搜索出来的结果为。。。。<br>");
if(hits.length()==1){
Documentdoc=hits.doc(0);
//其实这个只要根据doc也可以获取到title字段,只是考虑一张表很多个字段,而只是拿几个出来建索引,要想获取详细的信息还必须查找数据库找到相应的字段
goods=goodsService.find(Long.valueOf(doc.get("id")));
if(goods!=null){
out.println("{'id':"+goods.getId()+",'title':"+goods.getTitle()+"}<br>");
}
}else{
intlength=hits.length();
for(inti=0;i<length;i++){
Documentdoc=hits.doc(i);
if(doc!=null){
goods=goodsService.find(Long.valueOf(doc.get("id")));
if(goods!=null){
out.println("{'id':"+goods.getId()+",'title':"+goods.getTitle()+"}<br>");
}
}
}
}
}
}
}
%>