標籤:kivy qsub spel psu fifo lrm idts gpe orb
1.使用者介面(lucene不提供)
2.建立查詢
3.執行查詢
4.渲染結果:
5.過程分析
根據關鍵字查詢索引庫中的內容:
1) 建立IndexSearcher對象
2) 建立QueryParser對象
3) 建立Query對象來封裝關鍵字
4) 用IndexSearcher對象去索引庫中查詢合格前100條記錄,不足100條記錄的以實際為準
5) 擷取合格編號
6) 用indexSearcher對象去索引庫中查詢編號對應的Document對象
7) 將Document對象中的所有屬性取出,再封裝回JavaBean對象中去,並加入到集合中儲存,以備將之用
IndexSearcher對象搜尋方法:
6.代碼實現:
1 // 搜尋索引 2 @Test 3 public void testSearch() throws Exception { 4 // 第一步:建立一個Directory對象,也就是索引庫存放的位置。 5 Directory directory = FSDirectory.open(new File("E:\\lucene&solr\\index"));// 磁碟 6 // 第二步:建立一個indexReader對象,需要指定Directory對象。 7 IndexReader indexReader = DirectoryReader.open(directory); 8 // 第三步:建立一個indexsearcher對象,需要指定IndexReader對象 9 IndexSearcher indexSearcher = new IndexSearcher(indexReader);10 // 第四步:建立一個TermQuery對象,指定查詢的域和查詢的關鍵詞。11 Query query = new TermQuery(new Term("fileName", "java"));12 // 第五步:執行查詢。13 TopDocs topDocs = indexSearcher.search(query, 10);14 // 第六步:返回查詢結果。遍曆查詢結果並輸出。15 ScoreDoc[] scoreDocs = topDocs.scoreDocs;16 for (ScoreDoc scoreDoc : scoreDocs) {17 int doc = scoreDoc.doc;18 Document document = indexSearcher.doc(doc);19 // 檔案名稱20 String fileName = document.get("fileName");21 System.out.println(fileName);22 // 檔案內容23 String fileContent = document.get("fileContent");24 System.out.println(fileContent);25 // 檔案大小26 String fileSize = document.get("fileSize");27 System.out.println(fileSize);28 // 檔案路徑29 String filePath = document.get("filePath");30 System.out.println(filePath);31 System.out.println("------------");32 }33 // 第七步:關閉IndexReader對象34 indexReader.close();35 36 }
結果:
java struts.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren??t happy, you can smile, and then you will feel happy. Someone may say, ??But I don??t feel happy.?? Then I would say, ??Please smile as you do when you are happy or play wit309E:\lucene1\searchfiles\java struts.txt------------java struts - .txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren??t happy, you can smile, and then you will feel happy. Someone may say, ??But I don??t feel happy.?? Then I would say, ??Please smile as you do when you are happy or play wit309E:\lucene&solr\searchfiles\java struts - .txt------------java struts.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren??t happy, you can smile, and then you will feel happy. Someone may say, ??But I don??t feel happy.?? Then I would say, ??Please smile as you do when you are happy or play wit309E:\lucene&solr\searchfiles\java struts.txt------------java struts springmvc.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren??t happy, you can smile, and then you will feel happy. Someone may say, ??But I don??t feel happy.?? Then I would say, ??Please smile as you do when you are happy or play wit309E:\lucene&solr\searchfiles\java struts springmvc.txt------------java struts spring.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren??t happy, you can smile, and then you will feel happy. Someone may say, ??But I don??t feel happy.?? Then I would say, ??Please smile as you do when you are happy or play wit309E:\lucene&solr\searchfiles\java struts spring.txt------------
將上面的第五步的
TopDocs topDocs = indexSearcher.search(query, 10); 改為
TopDocs topDocs = indexSearcher.search(query, 2);
結果:只輸出查到的兩條記錄
java struts.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren??t happy, you can smile, and then you will feel happy. Someone may say, ??But I don??t feel happy.?? Then I would say, ??Please smile as you do when you are happy or play wit309E:\lucene1\searchfiles\java struts.txt------------java struts - .txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren??t happy, you can smile, and then you will feel happy. Someone may say, ??But I don??t feel happy.?? Then I would say, ??Please smile as you do when you are happy or play wit309E:\lucene&solr\searchfiles\java struts - .txt------------
lucene入門查詢索引——(三)