基于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 = {"soukenan@qq.com","li@soukenan.com","804632564@qq.com","admin@qq.com","soukenan@kenan.org","123@df.com"}; 
  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.