鎴戠殑鐜鏄疞ucene2.0
鍐欎竴涓畝鍗曚嬌鐢↙ucene鐨勭ず渚嬨傛綾婚鍒涘緩绱㈠紩錛岀劧鍚庢樉紺虹儲寮曟枃妗g殑鎯呭喌錛屾渶鍚庢悳绱紙鍙湪content鎵撅紝鍜屽湪title鎴朿ontent閲屾壘錛夈?br />
package net.blogjava.chenlb.lucene;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
/**
* Lucene綆鍗曚嬌鐢?br />
* @author chenlb 2008-3-8 涓嬪崍11:42:55
*/
public class LuceneUse {
public static void main(String[] args) throws Exception {
LuceneUse liu = new LuceneUse();
//绱㈠紩
IndexWriter iw = new IndexWriter("index", new StandardAnalyzer(), true);
//娣誨姞瑕佺儲寮曠殑Lucene鏂囨。
Document doc = liu.createDoc("Lucene鍒涘緩绱㈠紩紺轟緥", "chenlb", "2008-03-08", "Lucene绱㈠紩鐨勫唴瀹瑰湪榪欓噷,榪欎簺鍐呭涓嶈瀛樺偍.");
iw.addDocument(doc);
doc = liu.createDoc("鏂囨。2", "bin", "2007-10-03", "榪欐槸绱㈠紩鐨勫彟涓涓枃妗?/span>");
iw.addDocument(doc);
doc = liu.createDoc("瀛︿範鍐呭", "chenlb", "2008-3-3", "瑕佸姫鍔涘鏂?紲濈綉鍙嬩滑澶╁ぉ蹇箰");
iw.addDocument(doc);
iw.optimize(); //浼樺寲
iw.close();
//璇?/span>
System.out.println("===========绱㈠紩鏂囨。鍐呭=============");
IndexReader reader = IndexReader.open("index");
for(int i=0; i<reader.numDocs(); i++) {
Document d = reader.document(i);
liu.printDoc(d);
}
System.out.println("===========浠ヤ笅鏄崟鍩熸煡鎵?澶╁ぉ'緇撴灉============");
//鍗曞煙鎼滅儲
IndexSearcher searcher = new IndexSearcher("index");
QueryParser parser = new QueryParser("content", new StandardAnalyzer());
Query q = parser.parse("澶╁ぉ");
long start = System.currentTimeMillis();
Hits hits = searcher.search(q);
long end = System.currentTimeMillis();
for(int i=0; i<hits.length(); i++) {
liu.printDoc(hits.doc(i));
}
System.out.println("鍏辨壘鍒? "+hits.length()+" 涓枃妗?鑺變簡:"+(end-start)+"ms");
//澶氬煙鎼滅儲
System.out.println("===========浠ヤ笅澶氬煙鏄煡鎵?鍐呭'緇撴灉============");
//浠巘itle鎴朿ontent鎵?/span>
q = MultiFieldQueryParser.parse("鍐呭", new String[] {"title", "content"}, new BooleanClause.Occur[] {BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD}, new StandardAnalyzer());
start = System.currentTimeMillis();
hits = searcher.search(q);
end = System.currentTimeMillis();
for(int i=0; i<hits.length(); i++) {
liu.printDoc(hits.doc(i));
}
System.out.println("鍏辨壘鍒? "+hits.length()+" 涓枃妗?鑺變簡:"+(end-start)+"ms");
}
/**
* 鏄劇ず鏂囨。鍐呭
*/
private void printDoc(Document d) {
System.out.println("鏍囬: "+d.get("title")+", 浣滆? "+d.get("author")+", 鏃ユ湡: "+d.get("date")+", 鍐呭: "+d.get("content"));
}
/**
* 鍒涘緩涓涓狶ucene鏂囨。
*/
private Document createDoc(String title, String author, String date, String content) {
Document doc = new Document();
doc.add(new Field("title", title, Field.Store.YES, Field.Index.TOKENIZED));
doc.add(new Field("author", author, Field.Store.YES, Field.Index.NO));
doc.add(new Field("date", date, Field.Store.YES, Field.Index.NO));
doc.add(new Field("content", content, Field.Store.YES, Field.Index.TOKENIZED));
return doc;
}
}

]]>