import java.io.File; import java.util.Date; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; public class LuceneIndexText { private static String IndexDir = "D://test//index//"; private static String[] keywords = { "001", "002", "003" }; private static String[] textdetail = { "content1", "content2", "content3" }; public static void main(String args[]) { try { Date s = new Date(); IndexWriter iw = new IndexWriter(FSDirectory .open(new File(IndexDir)), new StandardAnalyzer( Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED); for (int i = 0; i < 3; i++) { Document doc = new Document(); doc.add(new Field("filename", keywords[i], Field.Store.YES, Field.Index.ANALYZED)); doc.add(new Field("contents", textdetail[i], Field.Store.YES, Field.Index.ANALYZED)); iw.addDocument(doc); } iw.optimize(); iw.close(); Date l = new Date(); System.out.println("Total process time is :"+(l.getTime()-s.getTime())); } catch (Exception e) { // TODO: handle exception } } } 建立索引的文档是两个数组,索引的目录是D://test//index//"