【Lucene實驗1】構建索引

來源:互聯網
上載者:User

標籤:Lucene   blog   http   io   os   ar   使用   java   for   

一、實驗名稱:構建索引

二、實驗日期:2013/9/21

三、實驗目的:

1)        能理解Lucene中的Document-Field結構的資料建模過程;

2)        能編針對特定資料產生索引檔案。

實驗用的儀器和材料

MyEclipse 10,JDK

 

實驗的步驟和方法

題目一:在指定目錄產生表示3本書的索引,要求建立3個document分別存放書名資料。把產生的索引檔案截好圖(複合索引與一般索引各產生一次)

 

圖1:一般索引的

圖2:複合索引的

題目二:修改題目一的代碼,使用多範圍在一個文檔中存放3本書的書名值。

題目三:針對題目一的三個文檔,分別做如下操作:根據書名在索引中刪除一個值、修改一個文檔的域值。

實驗過程:

題目一原始碼:

package lab02;import java.io.File;import java.io.IOException;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.Term;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.util.Version;public class GreatIndex {public static void main(String[] args) {GreatIndex GreateIndexobj=new GreatIndex();try {     GreateIndexobj.setUp();} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}private String indexDir="E:/Users/Administrator/Workspaces/MyEclipse 10/mylucene/src/lab02/index";private Directory directory; //表示索引存放的目錄public void setUp()throws Exception {//directory =new RAMDirectory(); //索引存放在記憶體的RAM中directory =FSDirectory.open((new File(indexDir))); //索引存放在物理硬碟的檔案系統內(就是存放指定路徑)IndexWriter writer=new IndexWriter(directory,new StandardAnalyzer(Version.LUCENE_30),true,IndexWriter.MaxFieldLength.UNLIMITED);//write.setUseCompoundFile(false);//設定false就是使用一般索引(有多種檔案的)//建立3本書的documentDocument doc1=new Document();Document doc2=new Document();Document doc3=new Document();//建立名字叫“bookname”的field並添加域值到文檔中,設定國域值儲存到索引中,不被分詞與加權doc1.add(new Field("bookname", "伐清",Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));doc2.add(new Field("bookname", "奧術神座",Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));doc1.add(new Field("bookname", "冰與火之歌",Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));writer.addDocument(doc1);writer.addDocument(doc2);writer.addDocument(doc3);writer.close(); }}

  

題目二原始碼:

package lab02;import java.io.File;import java.io.IOException;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.Term;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.util.Version;public class GreatIndex {public static void main(String[] args) {GreatIndex GreateIndexobj=new GreatIndex();try {//GreateIndexobj.setUp();     GreateIndexobj.setUp2();} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}private String indexDir="E:/Users/Administrator/Workspaces/MyEclipse 10/mylucene/src/lab02/index";private Directory directory; //表示索引存放的目錄private String[] booknames={"伐清","奧術神座","冰與火之歌"};public void setUp2() throws Exception{//directory =new RAMDirectory(); //索引存放在記憶體的RAM中directory =FSDirectory.open((new File(indexDir))); //索引存放在物理硬碟的檔案系統內(就是存放指定路徑)IndexWriter writer=new IndexWriter(directory,new StandardAnalyzer(Version.LUCENE_30),true,IndexWriter.MaxFieldLength.UNLIMITED);//writer.setUseCompoundFile(false);//設定false就是使用一般索引(有多種檔案的)//建立包含三個域值的documentDocument doc=new Document();for (String bookname:booknames) {doc.add(new Field("bookname",bookname,Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));}writer.addDocument(doc);writer.close();}}

 

題目三原始碼:

  //題目三public void DeleteDocument()throws IOException{IndexWriter writer=new IndexWriter(directory,new StandardAnalyzer(Version.LUCENE_30),true,IndexWriter.MaxFieldLength.UNLIMITED);writer.optimize();//使用最佳化策略刪除文檔(直接刪除,不能回複)writer.deleteDocuments(new Trem("bookname", "伐清"));writer.close();}public void UpdateDocument() throws IOException{IndexWriter writer=new IndexWriter(directory,new StandardAnalyzer(Version.LUCENE_30),true,IndexWriter.MaxFieldLength.UNLIMITED);//構建一個新的document用與替換Document doc=new Document();doc.add(new Field("bookname","Lucene實戰第二版",Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));writer.updateDocument(new Term("bookname","官仙"), doc);writer.close();}

  

六、資料記錄和計算

項目的結構圖:

七、實驗結果或結論

總結:通過這次的實驗,我基本理解Lucene中的Document-Field結構的資料建模過程, 能編針對特定資料產生索引檔案.在這次的實驗過程中,實驗不是很順利,這次實驗讓我感受到了Lucene的強大,增加我對Lucene的興趣!

 

 

八、備忘或說明

、引用參考文獻

http://lucene.apache.org

 

【Lucene實驗1】構建索引

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.