lucene入門查詢索引——(三)

來源:互聯網
上載者:User

標籤: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入門查詢索引——(三)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.