Lucene索引創建之域選項介紹,Field.Store和Fiele.Index

基於jar lucene3.6.2

Field.Store.YES / NO  ---  存儲選項
設置爲YES表示把這個域中的內容完全存儲到文件中,方便進行文本的還原
設置爲NO表示把這個域的內容不存儲到文件中,但是可以被索引,此時內容無法完全還原

Field.Index.  --- 索引選項
ANALYZED  進行分詞和索引,適用於標題,內容等
NOT_ANALYZED  進行索引,但不進行分詞,適用於精確搜索
ANALYZED_NOT_NORMS 進行分詞但是不存儲norms信息,這個norms中包括了創建索引的時間和全職等信息
NOT_ANALYZED_NOT_NORMS 既不進行分詞也不存儲norms信息
NO 不進行索引

下面直接通過例子說明問題

  1. IndexUtil.java 
  2. import java.io.File; 
  3. import java.io.IOException; 
  4.  
  5. import org.apache.lucene.analysis.standard.StandardAnalyzer; 
  6. import org.apache.lucene.document.Document; 
  7. import org.apache.lucene.document.Field; 
  8. import org.apache.lucene.index.CorruptIndexException; 
  9. import org.apache.lucene.index.IndexReader; 
  10. import org.apache.lucene.index.IndexWriter; 
  11. import org.apache.lucene.index.IndexWriterConfig; 
  12. import org.apache.lucene.store.Directory; 
  13. import org.apache.lucene.store.FSDirectory; 
  14. import org.apache.lucene.store.LockObtainFailedException; 
  15. import org.apache.lucene.util.Version; 
  16.  
  17.  
  18. public class IndexUtil { 
  19.     private String[] ids = {"1","2","3","4","5","6"}; 
  20.     private String[] emails = {"[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]"}; 
  21.     private String[] content ={ 
  22.             "Welcome to my home"
  23.             "Hello Kenan"
  24.             "Good morning"
  25.             "Are you OK?"
  26.             "Yeah hahahahahahaha"
  27.             "I like foot ball" 
  28.     }; 
  29.     private int[] attachs = {1,4,6,2,3,8}; 
  30.     private String[] names = {"zhangsan","kenan","soukenan","Micheal","Liangchengpeng","Jah"}; 
  31.     //詞典  
  32.     private Directory directory = null
  33.     //寫入筆 
  34.     private IndexWriter writer = null
  35.     //文檔對象 
  36.     private Document doc = null
  37.     //讀取對象 
  38.     private IndexReader reader = null
  39.     public IndexUtil(){ 
  40.         try { 
  41.             this.directory = FSDirectory.open(new File("d:/lucene/index02")); 
  42.         } catch (IOException e) { 
  43.             // TODO Auto-generated catch block 
  44.             e.printStackTrace(); 
  45.         } 
  46.     } 
  47.     /** 
  48.      * 構建索引 
  49.      */ 
  50.     public void buildIndex(){ 
  51.         try { 
  52.             writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_36))); 
  53.             for(int i=0;i<6;i++){ 
  54.                 doc = new Document(); 
  55.                 doc.add(new Field ("id",ids[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS)); 
  56.                 doc.add(new Field("email",emails[i],Field.Store.YES,Field.Index.NOT_ANALYZED)); 
  57.                 doc.add(new Field("content",this.content[i],Field.Store.NO,Field.Index.ANALYZED)); 
  58.                 doc.add(new Field("name",this.names[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS)); 
  59.                 writer.addDocument(doc); 
  60.             } 
  61.         } catch (CorruptIndexException e) { 
  62.             // TODO Auto-generated catch block 
  63.             e.printStackTrace(); 
  64.         } catch (LockObtainFailedException e) { 
  65.             // TODO Auto-generated catch block 
  66.             e.printStackTrace(); 
  67.         } catch (IOException e) { 
  68.             // TODO Auto-generated catch block 
  69.             e.printStackTrace(); 
  70.         }finally
  71.             if(writer != null ){ 
  72.                 try { 
  73.                     writer.close(); 
  74.                 } catch (CorruptIndexException e) { 
  75.                     // TODO Auto-generated catch block 
  76.                     e.printStackTrace(); 
  77.                 } catch (IOException e) { 
  78.                     // TODO Auto-generated catch block 
  79.                     e.printStackTrace(); 
  80.                 } 
  81.             } 
  82.         } 
  83.          
  84.     } 
  85.     public void query(){ 
  86.         try { 
  87.             this.reader = IndexReader.open(directory); 
  88.             System.out.println("maxDoc:"+reader.maxDoc()); 
  89.             System.out.println("numDocs:"+reader.numDocs()); 
  90.              
  91.         } catch (CorruptIndexException e) { 
  92.             // TODO Auto-generated catch block 
  93.             e.printStackTrace(); 
  94.         } catch (IOException e) { 
  95.             // TODO Auto-generated catch block 
  96.             e.printStackTrace(); 
  97.         } 
  98.     } 
  99. 測試類 TestCase.java 
  100. import static org.junit.Assert.*; 
  101.  
  102. import org.junit.BeforeClass; 
  103. import org.junit.Test; 
  104.  
  105.  
  106. public class TestCase { 
  107.  
  108.     @BeforeClass 
  109.     public static void setUpBeforeClass() throws Exception { 
  110.     } 
  111.  
  112.     @Test 
  113.     public void testBuildIndex() { 
  114.         new IndexUtil().buildIndex(); 
  115.     } 
  116.     @Test 
  117.     public void testQuery(){ 
  118.         new IndexUtil().query(); 
  119.     } 
  120.  

 

本文出自 “Kenan_ITBlog” 博客,請務必保留此出處http://soukenan.blog.51cto.com/5130995/1119923

發佈了115 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章