一:建立索引. 1、创建IndexWriter对象 IndexWriter writer = new IndexWriter(FSDirectory.open(INDEX_DIR),new StandardAnalyzer(Version.LUCENE_CURRENT),true,IndexWriter.MaxFieldLength.LIMITED); 2.创建文档Document对象,并加入域(Field). Document doc = new Document(); doc.add(new Field("path",f.getPath,Field.Store.YES,Field.Index.NOT_ANALYZED)); doc.add(new Field("modified",DateTools.timeToString(f.lastModified(),DateTools.Resolution.MINUTE),Field.STORE.YES,Field.Index.NOT_ANALYZED)); doc.add(new Field("contents",new FileReader(f))); 3.将文档加入IndexWriter. writer.addDocument(doc); 4.关闭IndexWriter对象. writer.close(); 二:搜索 IndexSearcher searcher = new IndexSearcher(FSDirectory.open(INDEX_PATH)); QueryParser parser = new QueryParser(Version.LUCENE_CURRENT,"contents", new StandardAnalyzer(Version_LUCENE_CURRENT)); String queryString = "中国"; Query query = parser.parse(queryString); TopDocs topDocs = searcher.search(query,10000); for(ScoreDoc hits : topDocs.scoreDocs) { int docSn = hits.doc; Document d = searcher.doc(docSn); System.out.println(d.get("contents")); } searcher.close();