|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| IndexModifier | Line # 86 | 115 | 41 | 90.1% |
0.9005848
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (3) | |||
| Result | |||
|
0.86549705
|
org.apache.lucene.index.TestIndexModifier.testIndex
org.apache.lucene.index.TestIndexModifier.testIndex
|
1 PASS | |
|
0.4093567
|
org.apache.lucene.index.TestIndexModifier.testIndexWithThreads
org.apache.lucene.index.TestIndexModifier.testIndexWithThreads
|
1 PASS | |
|
0.33333334
|
org.apache.lucene.index.TestIndexModifier.testExtendedIndex
org.apache.lucene.index.TestIndexModifier.testExtendedIndex
|
1 PASS | |
| 1 | package org.apache.lucene.index; | |
| 2 | ||
| 3 | /** | |
| 4 | * Copyright 2005 The Apache Software Foundation | |
| 5 | * | |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 7 | * you may not use this file except in compliance with the License. | |
| 8 | * You may obtain a copy of the License at | |
| 9 | * | |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 11 | * | |
| 12 | * Unless required by applicable law or agreed to in writing, software | |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 15 | * See the License for the specific language governing permissions and | |
| 16 | * limitations under the License. | |
| 17 | */ | |
| 18 | ||
| 19 | import org.apache.lucene.analysis.Analyzer; | |
| 20 | import org.apache.lucene.document.Document; | |
| 21 | import org.apache.lucene.store.Directory; | |
| 22 | import org.apache.lucene.store.FSDirectory; | |
| 23 | ||
| 24 | import java.io.File; | |
| 25 | import java.io.IOException; | |
| 26 | import java.io.PrintStream; | |
| 27 | ||
| 28 | /** | |
| 29 | * A class to modify an index, i.e. to delete and add documents. This | |
| 30 | * class hides {@link IndexReader} and {@link IndexWriter} so that you | |
| 31 | * do not need to care about implementation details such as that adding | |
| 32 | * documents is done via IndexWriter and deletion is done via IndexReader. | |
| 33 | * | |
| 34 | * <p>Note that you cannot create more than one <code>IndexModifier</code> object | |
| 35 | * on the same directory at the same time. | |
| 36 | * | |
| 37 | * <p>Example usage: | |
| 38 | * | |
| 39 | <!-- ======================================================== --> | |
| 40 | <!-- = Java Sourcecode to HTML automatically converted code = --> | |
| 41 | <!-- = Java2Html Converter V4.1 2004 by Markus Gebhard markus@jave.de = --> | |
| 42 | <!-- = Further information: http://www.java2html.de = --> | |
| 43 | <div align="left" class="java"> | |
| 44 | <table border="0" cellpadding="3" cellspacing="0" bgcolor="#ffffff"> | |
| 45 | <tr> | |
| 46 | <!-- start source code --> | |
| 47 | <td nowrap="nowrap" valign="top" align="left"> | |
| 48 | <code> | |
| 49 | <font color="#ffffff"> </font><font color="#000000">Analyzer analyzer = </font><font color="#7f0055"><b>new </b></font><font color="#000000">StandardAnalyzer</font><font color="#000000">()</font><font color="#000000">;</font><br/> | |
| 50 | <font color="#ffffff"> </font><font color="#3f7f5f">// create an index in /tmp/index, overwriting an existing one:</font><br/> | |
| 51 | <font color="#ffffff"> </font><font color="#000000">IndexModifier indexModifier = </font><font color="#7f0055"><b>new </b></font><font color="#000000">IndexModifier</font><font color="#000000">(</font><font color="#2a00ff">"/tmp/index"</font><font color="#000000">, analyzer, </font><font color="#7f0055"><b>true</b></font><font color="#000000">)</font><font color="#000000">;</font><br/> | |
| 52 | <font color="#ffffff"> </font><font color="#000000">Document doc = </font><font color="#7f0055"><b>new </b></font><font color="#000000">Document</font><font color="#000000">()</font><font color="#000000">;</font><br/> | |
| 53 | <font color="#ffffff"> </font><font color="#000000">doc.add</font><font color="#000000">(</font><font color="#7f0055"><b>new </b></font><font color="#000000">Field</font><font color="#000000">(</font><font color="#2a00ff">"id"</font><font color="#000000">, </font><font color="#2a00ff">"1"</font><font color="#000000">, Field.Store.YES, Field.Index.UN_TOKENIZED</font><font color="#000000">))</font><font color="#000000">;</font><br/> | |
| 54 | <font color="#ffffff"> </font><font color="#000000">doc.add</font><font color="#000000">(</font><font color="#7f0055"><b>new </b></font><font color="#000000">Field</font><font color="#000000">(</font><font color="#2a00ff">"body"</font><font color="#000000">, </font><font color="#2a00ff">"a simple test"</font><font color="#000000">, Field.Store.YES, Field.Index.TOKENIZED</font><font color="#000000">))</font><font color="#000000">;</font><br/> | |
| 55 | <font color="#ffffff"> </font><font color="#000000">indexModifier.addDocument</font><font color="#000000">(</font><font color="#000000">doc</font><font color="#000000">)</font><font color="#000000">;</font><br/> | |
| 56 | <font color="#ffffff"> </font><font color="#7f0055"><b>int </b></font><font color="#000000">deleted = indexModifier.delete</font><font color="#000000">(</font><font color="#7f0055"><b>new </b></font><font color="#000000">Term</font><font color="#000000">(</font><font color="#2a00ff">"id"</font><font color="#000000">, </font><font color="#2a00ff">"1"</font><font color="#000000">))</font><font color="#000000">;</font><br/> | |
| 57 | <font color="#ffffff"> </font><font color="#000000">System.out.println</font><font color="#000000">(</font><font color="#2a00ff">"Deleted " </font><font color="#000000">+ deleted + </font><font color="#2a00ff">" document"</font><font color="#000000">)</font><font color="#000000">;</font><br/> | |
| 58 | <font color="#ffffff"> </font><font color="#000000">indexModifier.flush</font><font color="#000000">()</font><font color="#000000">;</font><br/> | |
| 59 | <font color="#ffffff"> </font><font color="#000000">System.out.println</font><font color="#000000">(</font><font color="#000000">indexModifier.docCount</font><font color="#000000">() </font><font color="#000000">+ </font><font color="#2a00ff">" docs in index"</font><font color="#000000">)</font><font color="#000000">;</font><br/> | |
| 60 | <font color="#ffffff"> </font><font color="#000000">indexModifier.close</font><font color="#000000">()</font><font color="#000000">;</font></code> | |
| 61 | ||
| 62 | </td> | |
| 63 | <!-- end source code --> | |
| 64 | </tr> | |
| 65 | </table> | |
| 66 | </div> | |
| 67 | <!-- = END of automatically generated HTML code = --> | |
| 68 | <!-- ======================================================== --> | |
| 69 | * | |
| 70 | * <p>Not all methods of IndexReader and IndexWriter are offered by this | |
| 71 | * class. If you need access to additional methods, either use those classes | |
| 72 | * directly or implement your own class that extends <code>IndexModifier</code>. | |
| 73 | * | |
| 74 | * <p>Although an instance of this class can be used from more than one | |
| 75 | * thread, you will not get the best performance. You might want to use | |
| 76 | * IndexReader and IndexWriter directly for that (but you will need to | |
| 77 | * care about synchronization yourself then). | |
| 78 | * | |
| 79 | * <p>While you can freely mix calls to add() and delete() using this class, | |
| 80 | * you should batch you calls for best performance. For example, if you | |
| 81 | * want to update 20 documents, you should first delete all those documents, | |
| 82 | * then add all the new documents. | |
| 83 | * | |
| 84 | * @author Daniel Naber | |
| 85 | */ | |
| 86 | public class IndexModifier { | |
| 87 | ||
| 88 | protected IndexWriter indexWriter = null; | |
| 89 | protected IndexReader indexReader = null; | |
| 90 | ||
| 91 | protected Directory directory = null; | |
| 92 | protected Analyzer analyzer = null; | |
| 93 | protected boolean open = false; | |
| 94 | ||
| 95 | // Lucene defaults: | |
| 96 | protected PrintStream infoStream = null; | |
| 97 | protected boolean useCompoundFile = true; | |
| 98 | protected int maxBufferedDocs = IndexWriter.DEFAULT_MAX_BUFFERED_DOCS; | |
| 99 | protected int maxFieldLength = IndexWriter.DEFAULT_MAX_FIELD_LENGTH; | |
| 100 | protected int mergeFactor = IndexWriter.DEFAULT_MERGE_FACTOR; | |
| 101 | ||
| 102 | /** | |
| 103 | * Open an index with write access. | |
| 104 | * | |
| 105 | * @param directory the index directory | |
| 106 | * @param analyzer the analyzer to use for adding new documents | |
| 107 | * @param create <code>true</code> to create the index or overwrite the existing one; | |
| 108 | * <code>false</code> to append to the existing index | |
| 109 | */ | |
| 110 | 5 |
public IndexModifier(Directory directory, Analyzer analyzer, boolean create) throws IOException { |
| 111 | 5 | init(directory, analyzer, create); |
| 112 | } | |
| 113 | ||
| 114 | /** | |
| 115 | * Open an index with write access. | |
| 116 | * | |
| 117 | * @param dirName the index directory | |
| 118 | * @param analyzer the analyzer to use for adding new documents | |
| 119 | * @param create <code>true</code> to create the index or overwrite the existing one; | |
| 120 | * <code>false</code> to append to the existing index | |
| 121 | */ | |
| 122 | 0 |
public IndexModifier(String dirName, Analyzer analyzer, boolean create) throws IOException { |
| 123 | 0 | Directory dir = FSDirectory.getDirectory(dirName, create); |
| 124 | 0 | init(dir, analyzer, create); |
| 125 | } | |
| 126 | ||
| 127 | /** | |
| 128 | * Open an index with write access. | |
| 129 | * | |
| 130 | * @param file the index directory | |
| 131 | * @param analyzer the analyzer to use for adding new documents | |
| 132 | * @param create <code>true</code> to create the index or overwrite the existing one; | |
| 133 | * <code>false</code> to append to the existing index | |
| 134 | */ | |
| 135 | 0 |
public IndexModifier(File file, Analyzer analyzer, boolean create) throws IOException { |
| 136 | 0 | Directory dir = FSDirectory.getDirectory(file, create); |
| 137 | 0 | init(dir, analyzer, create); |
| 138 | } | |
| 139 | ||
| 140 | /** | |
| 141 | * Initialize an IndexWriter. | |
| 142 | * @throws IOException | |
| 143 | */ | |