?? baseindexingtestcase.java
字號:
import junit.framework.TestCase;
import org.apache.lucene.analysis.*;
import org.apache.lucene.analysis.standard.*;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.queryParser.*;
import org.apache.lucene.search.*;
import org.apache.lucene.search.function.*;
import org.apache.lucene.search.payloads.*;
import org.apache.lucene.search.spans.*;
import org.apache.lucene.store.*;
import org.apache.lucene.util.*;
import java.io.*;
public class BaseIndexingTestCase extends TestCase {
protected String[] keywords = {"1","2"};
protected String[] unIndexed = {"Netherlands","Italy"};
protected String[] unStored = {"Amsterdam has lots of bridges",
"Venice has lots of cannals"};
protected String[] text = {"Amsterdam","Venice"};
protected Directory dir;
protected void setUp() throws IOException {
String indexDir = System.getProperty("java.io.tmpdir","tmp") +
System.getProperty("file.separator") + "index-dir";
// deprecated: use
//dir = FSDirectory.getDirectory(indexDir,true);
dir = FSDirectory.getDirectory(indexDir);
addDocuments(dir);
}
protected void addDocuments(Directory dir) throws IOException {
IndexWriter iWriter = new IndexWriter(dir,getAnalyzer(),true);
iWriter.setUseCompoundFile(isCompound());
for(int i=0; i<keywords.length; i++){
Document doc = new Document();
doc.add(new Field("id",keywords[i],Field.Store.YES,Field.Index.UN_TOKENIZED));
doc.add(new Field("country",unIndexed[i],Field.Store.YES,Field.Index.NO));
doc.add(new Field("contents",unStored[i],Field.Store.NO,Field.Index.TOKENIZED));
doc.add(new Field("city",text[i],Field.Store.YES,Field.Index.TOKENIZED));
iWriter.addDocument(doc);
}
iWriter.optimize();
iWriter.close();
}
protected Analyzer getAnalyzer() {
return new SimpleAnalyzer();
}
protected boolean isCompound() {
return true;
}
public static void main(String[] args) {
BaseIndexingTestCase BITC = new BaseIndexingTestCase();
try {
BITC.setUp();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -