?? lucenetester.java
字號:
/*
* 版權所有: 摩網信息科技有限公司 2005
* 項目:DLOG4J_V3
* 所在包:com.liusoft.dlog4j.lucene
* 文件名:LuceneTester.java
* 創建時間:2005-10-24
* 創建者:Winter Lau
*/
package com.liusoft.dlog4j.search;
import java.io.File;
import java.io.IOException;
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.index.Term;
import org.apache.lucene.index.TermEnum;
/**
* useless, just for test
* @author Winter Lau
*/
public class LuceneTester {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
addIndex();
IndexReader reader = IndexReader.open(lucenePath);
System.out.println("文檔數:"+reader.numDocs());
TermEnum tes = reader.terms();
while(tes.next()){
Term t = tes.term();
System.out.println(t.toString());
}
//IndexSearcher searcher = new IndexSearcher(lucenePath);
}
/**
* 增加索引
*/
protected static int addIndex() throws IOException {
Document doc = new Document();
doc.add(new Field("id", "1", Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.add(new Field("author", "劉冬IBM OSI一一二二", Field.Store.YES, Field.Index.TOKENIZED));
doc.add(new Field("time", Long.toString(System.currentTimeMillis()), Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.add(new Field("content", "不知道從什么時候開始,在什么東西上面都有個日期,秋刀魚會過期,肉罐頭會過期,連保鮮紙都會過期,我開始懷疑,在這個世界上,還有什么東西是不會過期的?", Field.Store.NO, Field.Index.TOKENIZED));
IndexWriter writer = getWriter();
try {
writer.addDocument(doc);
writer.optimize();
}finally {
writer.close();
}
System.out.println("doc count = " + writer.docCount());
return 1;
}
/* (non-Javadoc)
* @see jdlog.search.SearchProxy#getWriter()
*/
protected static IndexWriter getWriter() throws IOException{
File rp = new File(lucenePath);
if(!rp.exists())
rp.mkdirs();
int wc = 0;
while(wc<10 && IndexReader.isLocked(lucenePath)){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
return null;
}
wc++;
}
File segments = new File(lucenePath + File.separator + SEGMENTS);
boolean bCreate = !segments.exists();
return new IndexWriter(lucenePath,new StandardAnalyzer(),bCreate);
}
public final static String SEGMENTS = "segments";
protected static String lucenePath = "D:\\lucene";
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -