Lucene对index操作

  1 package org.lucene.index;
  2 
  3 import java.io.File;
  4 import java.io.IOException;
  5 
  6 import org.apache.lucene.analysis.standard.StandardAnalyzer;
  7 import org.apache.lucene.document.Document;
  8 import org.apache.lucene.document.Field;
  9 import org.apache.lucene.index.CorruptIndexException;
 10 import org.apache.lucene.index.IndexReader;
 11 import org.apache.lucene.index.IndexWriter;
 12 import org.apache.lucene.index.IndexWriterConfig;
 13 import org.apache.lucene.index.Term;
 14 import org.apache.lucene.store.Directory;
 15 import org.apache.lucene.store.FSDirectory;
 16 import org.apache.lucene.store.LockObtainFailedException;
 17 import org.apache.lucene.util.Version;
 18 
 19 public class IndexUtil {
 20     String[] ids = { "aa", "bb", "cc", "dd", "ee", "ff", "gg" };
 21     String[] emails = { "aaa@lucenes.com", "bbb@lucenes.com",
 22             "ccc@lucenes.com", "ddd@lucenes.com", "eee@lucenes.com",
 23             "fff@lucenes.com", "ggg@lucenes.com" };
 24     String[] contents = { "aaaaaaaaaaaaaa", "bbbbbbbbbbbbbb",
 25             "ccccccccccccccc", "dddddddddddddd", "eeeeeeeeeeeeee",
 26             "fffffffffffffff", "gggggggggggggg" };
 27     String[] names = { "zhangsan", "lisi", "wangwu", "zhaoliu", "tianqi",
 28             "zhaoba", "chenjiu" };
 29     Integer[] attachs = { 1, 2, 5, 4, 6, 2, 3 };
 30 
 31     Directory directory = null;
 32 
 33     public IndexUtil() {
 34         try {
 35             directory = FSDirectory.open(new File(
 36                     "E:/luceneworkspace/Lucene2/path"));
 37         } catch (IOException e) {
 38             e.printStackTrace();
 39         }
 40     }
 41 
 42     /**
 43      * 创建索引
 44      */
 45     public void index() {
 46         IndexWriter indexWriter = null;
 47         try {
 48             indexWriter = new IndexWriter(directory, new IndexWriterConfig(
 49                     Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
 50             indexWriter.deleteAll();
 51             Document document = null;
 52             for (int i = 0; i < ids.length; i++) {
 53                 document = new Document();
 54                 document.add(new Field("id", ids[i], Field.Store.YES,
 55                         Field.Index.NOT_ANALYZED_NO_NORMS));
 56                 document.add(new Field("email", emails[i], Field.Store.YES,
 57                         Field.Index.NOT_ANALYZED));
 58                 document.add(new Field("content", contents[i], Field.Store.NO,
 59                         Field.Index.ANALYZED));
 60                 document.add(new Field("name", names[i], Field.Store.YES,
 61                         Field.Index.NOT_ANALYZED));
 62                 indexWriter.addDocument(document);
 63             }
 64         } catch (CorruptIndexException e) {
 65             e.printStackTrace();
 66         } catch (LockObtainFailedException e) {
 67             e.printStackTrace();
 68         } catch (IOException e) {
 69             e.printStackTrace();
 70         } finally {
 71             try {
 72                 if (indexWriter != null)
 73                     indexWriter.close();
 74             } catch (CorruptIndexException e) {
 75                 e.printStackTrace();
 76             } catch (IOException e) {
 77                 e.printStackTrace();
 78             }
 79         }
 80     }
 81 
 82     /**
 83      * 删除索引
 84      */
 85     public void delete() {
 86         IndexWriter indexWriter = null;
 87         try {
 88             indexWriter = new IndexWriter(directory, new IndexWriterConfig(
 89                     Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
 90             indexWriter.deleteDocuments(new Term("id", "aa"));
 91         } catch (CorruptIndexException e) {
 92             e.printStackTrace();
 93         } catch (LockObtainFailedException e) {
 94             e.printStackTrace();
 95         } catch (IOException e) {
 96             e.printStackTrace();
 97         } finally {
 98             try {
 99                 if (indexWriter != null)
100                     indexWriter.close();
101             } catch (CorruptIndexException e) {
102                 e.printStackTrace();
103             } catch (IOException e) {
104                 e.printStackTrace();
105             }
106         }
107     }
108 
109     /**
110      * 恢复索引
111      */
112     public void undelete() {
113         IndexReader indexReader = null;
114         try {
115             indexReader = IndexReader.open(directory, false);
116             indexReader.undeleteAll();
117         } catch (CorruptIndexException e) {
118             e.printStackTrace();
119         } catch (LockObtainFailedException e) {
120             e.printStackTrace();
121         } catch (IOException e) {
122             e.printStackTrace();
123         } finally {
124             try {
125                 if (indexReader != null)
126                     indexReader.close();
127             } catch (IOException e) {
128                 e.printStackTrace();
129             }
130         }
131     }
132 
133     /**
134      *  清空回收站索引
135      */
136     public void clear(){
137         IndexWriter indexWriter = null;
138         try {
139             indexWriter = new IndexWriter(directory, new IndexWriterConfig(
140                     Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
141             indexWriter.forceMergeDeletes();
142         } catch (CorruptIndexException e) {
143             e.printStackTrace();
144         } catch (LockObtainFailedException e) {
145             e.printStackTrace();
146         } catch (IOException e) {
147             e.printStackTrace();
148         } finally {
149             try {
150                 if (indexWriter != null)
151                     indexWriter.close();
152             } catch (CorruptIndexException e) {
153                 e.printStackTrace();
154             } catch (IOException e) {
155                 e.printStackTrace();
156             }
157         }
158     }
159     
160     /**
161      * 更新索引
162      */
163     public void update(){
164         IndexWriter indexWriter = null;
165         try {
166             indexWriter = new IndexWriter(directory, new IndexWriterConfig(
167                     Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
168             Document doc=new Document();
169             doc.add(new Field("id", "hh", Field.Store.YES,
170                     Field.Index.NOT_ANALYZED_NO_NORMS));
171             doc.add(new Field("email", emails[2], Field.Store.YES,
172                     Field.Index.NOT_ANALYZED));
173             doc.add(new Field("content", contents[3], Field.Store.NO,
174                     Field.Index.ANALYZED));
175             doc.add(new Field("name", names[4], Field.Store.YES,
176                     Field.Index.NOT_ANALYZED));
177             indexWriter.updateDocument(new Term("id","aa"), doc);
178         } catch (CorruptIndexException e) {
179             e.printStackTrace();
180         } catch (LockObtainFailedException e) {
181             e.printStackTrace();
182         } catch (IOException e) {
183             e.printStackTrace();
184         } finally {
185             try {
186                 if (indexWriter != null)
187                     indexWriter.close();
188             } catch (CorruptIndexException e) {
189                 e.printStackTrace();
190             } catch (IOException e) {
191                 e.printStackTrace();
192             }
193         }
194     }
195     
196     public void query() {
197         try {
198             IndexReader indexReader = IndexReader.open(directory);
199             System.out.println("maxDoc:" + indexReader.maxDoc());
200             System.out.println("numDocs:" + indexReader.numDocs());
201             System.out.println("numDeleteDocs:"+indexReader.numDeletedDocs());
202         } catch (CorruptIndexException e) {
203             e.printStackTrace();
204         } catch (IOException e) {
205             e.printStackTrace();
206         }
207 
208     }
209 }

 

posted @ 2012-04-17 23:30  Paul.Lau  阅读(1845)  评论(0编辑  收藏  举报