1.demo
public static void main(String[] args) { Directory directory = null; try { directory = FSDirectory.open(Paths.get("E:\\document\\【进阶之路】\\技术\\lucene\\index")); DirectoryReader reader = DirectoryReader.open(directory); IndexSearcher searcher = new IndexSearcher(reader); // 与创建索引使用的分词器相同 Analyzer analyzer = new StandardAnalyzer(); // 指定搜索域 QueryParser parser = new QueryParser("content", analyzer); Query query = parser.parse("Lucene案例"); // 检索前10条 TopDocs docs = searcher.search(query, 10); if (docs != null) { System.out.println("符合检索条件的文档总数:" + docs.totalHits); for (int i = 0; i < docs.scoreDocs.length; i++) { Document doc = searcher.doc(docs.scoreDocs[i].doc); System.out.println("id = " + doc.get("id")); System.out.println("content = " + doc.get("content")); System.out.println("num = " + doc.get("num")); } } } catch (IOException | ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
2.运行结果
符合检索条件的文档总数:2
id = a
content = lucene学习案例
num = 1
id = c
content = Lucene案例开发
num = 2