lucene索引库优化二 其实提高索引查询的速度最简洁的方法技术将索引放到内存当中,减少IO,从而提高查询速度: public class MergePolicies { public static void main(String[] args) throws Exception { long start=new Date().getTime(); IOContext context=new IOContext(); Directory dir=FSDirectory.open(new File("E:/docData/indexDir")); /** * 把索引存储到内存中 */ Directory directory=new RAMDirectory(dir,context); IndexReader reader=DirectoryReader.open(directory); IndexSearcher searcher=new IndexSearcher(reader); /** * 多条件查询 */ String[] fields={"content"}; QueryParser parser=new MultiFieldQueryParser(Version.LUCENE_44, fields,new StandardAnalyzer(Version.LUCENE_44)); Query query = parser.parse("源码"); TopScoreDocCollector results=TopScoreDocCollector.create(10, false); searcher.search(query, results); ScoreDoc[] scoreDocs = results.topDocs().scoreDocs; System.out.println(scoreDocs.length); /** * 可以在此分页 * start 起始位置 * length 记录数 */ for(int i=0;i<scoreDocs.length;i++){ Document doc= searcher.doc(scoreDocs[i].doc); System.out.println(doc.getField("filename")+" "+scoreDocs[i].toString()); } long end=new Date().getTime(); System.out.println("took time:"+(end-start)); } } 用放荡不羁的心态过随遇而安的生活