public List<Document> prefixQuery(Term term) { List<Document> docList = new ArrayList<Document>(); try { directory = FSDirectory.open(new File(LuceneConstant.INDEX_PATH));//打开索引文件夹 IndexReader reader = DirectoryReader.open(directory);//读取目录 IndexSearcher search = new IndexSearcher(reader);//初始化查询组件 Query query=new PrefixQuery(term);//查询 TopDocs td = search.search(query, 10000);//获取匹配上元素的一个docid for (ScoreDoc doc : td.scoreDocs) { docList.add(search.doc(doc.doc)); } reader.close();//关闭资源 directory.close();//关闭连接 } catch (Exception e) { e.printStackTrace(); } return docList; }