本人对Lucene挺感兴趣的,可是最新出的Lucene3.6 与以前的版本有很大的不同,改进了很多,使用方式也很多不同,通过多方查找,终于可以简单使用它了 创建索引和对它搜索了,
不过还好,有了它就可以对它进行扩展了,进了门,很多功能和方法,使用就好多了

,
现在我把简单的贴出来了,希望大家能够通过它,引申更多,
import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
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.analysis.cn.smart.SmartChineseAnalyzer;
import org.wltea.analyzer.dic.Hit;
import org.wltea.analyzer.lucene.IKAnalyzer;
public class LuceneUtil {
public static void main(String[] args)
{
searchRss("上海医药");
// createIndex();
}
/**
* 创建索引
*/
public static void createIndex()
{
Connection conn=null;
Statement st=null;
ResultSet rs=null;
String sql;
try
{
conn=SqlHelper.getConn();
st=conn.createStatement();
sql="Select * From rss";
rs=st.executeQuery(sql);
// 使用Lucene提供的分词器
// Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36); // 使用 商业分词器
Analyzer analyzer = new IKAnalyzer();
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_36, analyzer);
// 创建 索引文件位置
Directory dir =FSDirectory.open(new File("F:/RssIndex")); // 写入索引
IndexWriter index =new IndexWriter(dir,indexWriterConfig);
while(rs.next())
{
Document doc = new Document();
doc.add(new Field("id",rs.getString("id"),Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("title",rs.getString("title"),Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("link",rs.getString("link"),Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("pubdate",rs.getString("pubdate"),Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("author",rs.getString("author"),Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("source",rs.getString("source"),Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("body",rs.getString("body"),Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("addtime",rs.getString("addtime"),Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("rssfrom",rs.getString("rssfrom"),Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("tag",rs.getString("tag"),Field.Store.YES,Field.Index.NO));
doc.add(new Field("gegutag",rs.getString("gegutag"),Field.Store.YES,Field.Index.ANALYZED));
if(null!=rs.getString("gegulab"))
doc.add(new Field("gegulab",rs.getString("gegulab"),Field.Store.YES,Field.Index.ANALYZED));
index.addDocument(doc);
}
index.optimize();
index.close();
}catch(Exception ex)
{
ex.printStackTrace();
}finally
{
try{
SqlHelper.close(conn, st, rs);
}catch(Exception ex)
{
ex.printStackTrace();
}
}
}
/**
* 根据关键字搜索
* @param keyword
* @return
*/
public static List<Rss> searchRss(String keyword)
{
List<Rss> rsses = new ArrayList<Rss>();
try{
Directory dir = FSDirectory.open(new File("F:/RssIndex"));
IndexReader indexReader=IndexReader.open(dir);
IndexSearcher search = new IndexSearcher(indexReader);
Query query = null;
// Hit hits=null;
query = new TermQuery(new Term("title",keyword));
// Analyzer analyzer = new IKAnalyzer();
// QueryParser parser = new QueryParser(Version.LUCENE_36,"title", analyzer);
// query=parser.parse(keyword);
ScoreDoc[] hits = search.search(query, null, 1000).scoreDocs;
for(int i=0;i<hits.length;i++)
{
Document doc=search.doc(hits[i].doc);
Rss rss = new Rss();
rss.setId(Integer.parseInt(doc.get("id")));
rss.setTitle(doc.get("title"));
rss.setAuthor(doc.get("author"));
rss.setBody(doc.get("body"));
rss.setLink(doc.get("link"));
rss.setPubDate(doc.get("pubdate"));
rss.setRssFrom(doc.get("rssfrom"));
rss.setSource(doc.get("source"));
rsses.add(rss);
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return rsses;
}
}