📄 indexwriter.java
字号:
* @param path the path to the index directory * @param a the analyzer to use * @param create <code>true</code> to create the index or overwrite * the existing one; <code>false</code> to append to the existing * index * @throws CorruptIndexException if the index is corrupt * @throws LockObtainFailedException if another writer * has this index open (<code>write.lock</code> could not * be obtained) * @throws IOException if the directory cannot be read/written to, or * if it does not exist and <code>create</code> is * <code>false</code> or if there is any other low-level * IO error */ public IndexWriter(String path, Analyzer a, boolean create) throws CorruptIndexException, LockObtainFailedException, IOException { init(FSDirectory.getDirectory(path), a, create, true, null, true); } /** * Constructs an IndexWriter for the index in <code>path</code>. * Text will be analyzed with <code>a</code>. If <code>create</code> * is true, then a new, empty index will be created in * <code>path</code>, replacing the index already there, if any. * * @param path the path to the index directory * @param a the analyzer to use * @param create <code>true</code> to create the index or overwrite * the existing one; <code>false</code> to append to the existing * index * @throws CorruptIndexException if the index is corrupt * @throws LockObtainFailedException if another writer * has this index open (<code>write.lock</code> could not * be obtained) * @throws IOException if the directory cannot be read/written to, or * if it does not exist and <code>create</code> is * <code>false</code> or if there is any other low-level * IO error */ public IndexWriter(File path, Analyzer a, boolean create) throws CorruptIndexException, LockObtainFailedException, IOException { init(FSDirectory.getDirectory(path), a, create, true, null, true); } /** * Constructs an IndexWriter for the index in <code>d</code>. * Text will be analyzed with <code>a</code>. If <code>create</code> * is true, then a new, empty index will be created in * <code>d</code>, replacing the index already there, if any. * * @param d the index directory * @param a the analyzer to use * @param create <code>true</code> to create the index or overwrite * the existing one; <code>false</code> to append to the existing * index * @throws CorruptIndexException if the index is corrupt * @throws LockObtainFailedException if another writer * has this index open (<code>write.lock</code> could not * be obtained) * @throws IOException if the directory cannot be read/written to, or * if it does not exist and <code>create</code> is * <code>false</code> or if there is any other low-level * IO error */ public IndexWriter(Directory d, Analyzer a, boolean create) throws CorruptIndexException, LockObtainFailedException, IOException { init(d, a, create, false, null, true); } /** * Constructs an IndexWriter for the index in * <code>path</code>, first creating it if it does not * already exist. Text will be analyzed with * <code>a</code>. * * @param path the path to the index directory * @param a the analyzer to use * @throws CorruptIndexException if the index is corrupt * @throws LockObtainFailedException if another writer * has this index open (<code>write.lock</code> could not * be obtained) * @throws IOException if the directory cannot be * read/written to or if there is any other low-level * IO error */ public IndexWriter(String path, Analyzer a) throws CorruptIndexException, LockObtainFailedException, IOException { init(FSDirectory.getDirectory(path), a, true, null, true); } /** * Constructs an IndexWriter for the index in * <code>path</code>, first creating it if it does not * already exist. Text will be analyzed with * <code>a</code>. * * @param path the path to the index directory * @param a the analyzer to use * @throws CorruptIndexException if the index is corrupt * @throws LockObtainFailedException if another writer * has this index open (<code>write.lock</code> could not * be obtained) * @throws IOException if the directory cannot be * read/written to or if there is any other low-level * IO error */ public IndexWriter(File path, Analyzer a) throws CorruptIndexException, LockObtainFailedException, IOException { init(FSDirectory.getDirectory(path), a, true, null, true); } /** * Constructs an IndexWriter for the index in * <code>d</code>, first creating it if it does not * already exist. Text will be analyzed with * <code>a</code>. * * @param d the index directory * @param a the analyzer to use * @throws CorruptIndexException if the index is corrupt * @throws LockObtainFailedException if another writer * has this index open (<code>write.lock</code> could not * be obtained) * @throws IOException if the directory cannot be * read/written to or if there is any other low-level * IO error */ public IndexWriter(Directory d, Analyzer a) throws CorruptIndexException, LockObtainFailedException, IOException { init(d, a, false, null, true); } /** * Constructs an IndexWriter for the index in * <code>d</code>, first creating it if it does not * already exist. Text will be analyzed with * <code>a</code>. * * @param d the index directory * @param autoCommit see <a href="#autoCommit">above</a> * @param a the analyzer to use * @throws CorruptIndexException if the index is corrupt * @throws LockObtainFailedException if another writer * has this index open (<code>write.lock</code> could not * be obtained) * @throws IOException if the directory cannot be * read/written to or if there is any other low-level * IO error */ public IndexWriter(Directory d, boolean autoCommit, Analyzer a) throws CorruptIndexException, LockObtainFailedException, IOException { init(d, a, false, null, autoCommit); } /** * Constructs an IndexWriter for the index in <code>d</code>. * Text will be analyzed with <code>a</code>. If <code>create</code> * is true, then a new, empty index will be created in * <code>d</code>, replacing the index already there, if any. * * @param d the index directory * @param autoCommit see <a href="#autoCommit">above</a> * @param a the analyzer to use * @param create <code>true</code> to create the index or overwrite * the existing one; <code>false</code> to append to the existing * index * @throws CorruptIndexException if the index is corrupt * @throws LockObtainFailedException if another writer * has this index open (<code>write.lock</code> could not * be obtained) * @throws IOException if the directory cannot be read/written to, or * if it does not exist and <code>create</code> is * <code>false</code> or if there is any other low-level * IO error */ public IndexWriter(Directory d, boolean autoCommit, Analyzer a, boolean create) throws CorruptIndexException, LockObtainFailedException, IOException { init(d, a, create, false, null, autoCommit); } /** * Expert: constructs an IndexWriter with a custom {@link * IndexDeletionPolicy}, for the index in <code>d</code>, * first creating it if it does not already exist. Text * will be analyzed with <code>a</code>. * * @param d the index directory * @param autoCommit see <a href="#autoCommit">above</a> * @param a the analyzer to use * @param deletionPolicy see <a href="#deletionPolicy">above</a> * @throws CorruptIndexException if the index is corrupt * @throws LockObtainFailedException if another writer * has this index open (<code>write.lock</code> could not * be obtained) * @throws IOException if the directory cannot be * read/written to or if there is any other low-level * IO error */ public IndexWriter(Directory d, boolean autoCommit, Analyzer a, IndexDeletionPolicy deletionPolicy) throws CorruptIndexException, LockObtainFailedException, IOException { init(d, a, false, deletionPolicy, autoCommit); } /** * Expert: constructs an IndexWriter with a custom {@link * IndexDeletionPolicy}, for the index in <code>d</code>. * Text will be analyzed with <code>a</code>. If * <code>create</code> is true, then a new, empty index * will be created in <code>d</code>, replacing the index * already there, if any. * * @param d the index directory * @param autoCommit see <a href="#autoCommit">above</a> * @param a the analyzer to use * @param create <code>true</code> to create the index or overwrite * the existing one; <code>false</code> to append to the existing * index * @param deletionPolicy see <a href="#deletionPolicy">above</a> * @throws CorruptIndexException if the index is corrupt * @throws LockObtainFailedException if another writer * has this index open (<code>write.lock</code> could not * be obtained) * @throws IOException if the directory cannot be read/written to, or * if it does not exist and <code>create</code> is * <code>false</code> or if there is any other low-level * IO error */ public IndexWriter(Directory d, boolean autoCommit, Analyzer a, boolean create, IndexDeletionPolicy deletionPolicy) throws CorruptIndexException, LockObtainFailedException, IOException { init(d, a, create, false, deletionPolicy, autoCommit); } private void init(Directory d, Analyzer a, boolean closeDir, IndexDeletionPolicy deletionPolicy, boolean autoCommit) throws CorruptIndexException, LockObtainFailedException, IOException { if (IndexReader.indexExists(d)) { init(d, a, false, closeDir, deletionPolicy, autoCommit); } else { init(d, a, true, closeDir, deletionPolicy, autoCommit); } } private void init(Directory d, Analyzer a, final boolean create, boolean closeDir, IndexDeletionPolicy deletionPolicy, boolean autoCommit) throws CorruptIndexException, LockObtainFailedException, IOException { this.closeDir = closeDir; directory = d; analyzer = a; this.infoStream = defaultInfoStream; if (create) { // Clear the write lock in case it's leftover: directory.clearLock(IndexWriter.WRITE_LOCK_NAME); } Lock writeLock = directory.makeLock(IndexWriter.WRITE_LOCK_NAME); if (!writeLock.obtain(writeLockTimeout)) // obtain write lock throw new LockObtainFailedException("Index locked for write: " + writeLock); this.writeLock = writeLock; // save it try { if (create) { // Try to read first. This is to allow create // against an index that's currently open for // searching. In this case we write the next // segments_N file with no segments: try { segmentInfos.read(directory); segmentInfos.clear(); } catch (IOException e) { // Likely this means it's a fresh directory } segmentInfos.write(directory); } else { segmentInfos.read(directory); } this.autoCommit = autoCommit; if (!autoCommit) { rollbackSegmentInfos = (SegmentInfos) segmentInfos.clone(); } // Default deleter (for backwards compatibility) is // KeepOnlyLastCommitDeleter: deleter = new IndexFileDeleter(directory, deletionPolicy == null ? new KeepOnlyLastCommitDeletionPolicy() : deletionPolicy, segmentInfos, infoStream); } catch (IOException e) { this.writeLock.release(); this.writeLock = null; throw e; } } /** Determines the largest number of documents ever merged by addDocument(). * Small values (e.g., less than 10,000) are best for interactive indexing, * as this limits the length of pauses while indexing to a few seconds. * Larger values are best for batched indexing and speedier searches. * * <p>The default value is {@link Integer#MAX_VALUE}. */ public void setMaxMergeDocs(int maxMergeDocs) { ensureOpen(); this.maxMergeDocs = maxMergeDocs; } /** * Returns the largest number of documents allowed in a * single segment. * @see #setMaxMergeDocs */ public int getMaxMergeDocs() { ensureOpen(); return maxMergeDocs; } /** * The maximum number of terms that will be indexed for a single field in a * document. This limits the amount of memory required for indexing, so that * collections with very large files will not crash the indexing process by * running out of memory.<p/> * Note that this effectively truncates large documents, excluding from the * index terms that occur further in the document. If you know your source * documents are large, be sure to set this value high enough to accomodate * the expected size. If you set it to Integer.MAX_VALUE, then the only limit * is your memory, but you should anticipate an OutOfMemoryError.<p/> * By default, no more than 10,000 terms will be indexed for a field. */ public void setMaxFieldLength(int maxFieldLength) { ensureOpen(); this.maxFieldLength = maxFieldLength; } /** * Returns the maximum number of terms that will be * indexed for a single field in a document. * @see #setMaxFieldLength */ public int getMaxFieldLength() { ensureOpen(); return maxFieldLength; } /** Determines the minimal number of documents required before the buffered
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -