package org.itat.text1;
import java.io.IOException;
public class IndexText {
/**
* @param args
*/
package org.itat.text1;
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.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
class IndexUtil_11 {
private String [] ids={"1","2","3","4"};
private String [] emails={"[email protected]","[email protected]","[email protected]","[email protected]"};
private String [] contents={"Hello world!","lele is so cool!","[email protected]","[email protected]"};
private String [] names={"lele!","Tom","zhangsan ","zhaosi"};
private int [] attachs={1,2,3,5};
private Directory directory=null;
public void index()
{
//建立Derectory
try {
directory=FSDirectory.open(new File ("G:/Lucene/indext2"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 建立Writer
IndexWriter writer=null;
try {
writer=new IndexWriter (directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
Document doc=null;
for (int i=0;i<ids.length;i++)
{
//建立Decoment并添加Field
doc=new Document();
doc.add(new Field("id",ids[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
doc.add(new Field("name",names[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
doc.add(new Field("emails",emails[i],Field.Store.YES,Field.Index.NOT_ANALYZED));
doc.add(new Field("content",contents[i],Field.Store.NO,Field.Index.ANALYZED));
//将文檔寫到索引中
writer.addDocument(doc);
}
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LockObtainFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if (writer!=null)
{
try {
writer.close();
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//throw new Exception ();
}
}
}
}
public void query() throws IOException
{
try {
//通過IndexReader進行查詢
IndexReader reader=IndexReader.open(directory);
//通過reader可以有效的擷取文檔的數量
System.out.println(reader.maxDoc());
System.out.println(reader.numDocs());
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) { // TODO Auto-generated method stub IndexUtil_11 indextUtil=new IndexUtil_11(); indextUtil.index(); //indextUtil.IndexUtil(); try { indextUtil.query(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace();
} }}