📄 indexwriter.java
字号:
* <p>Note that this method is a convenience method: it * just calls mergePolicy.getUseCompoundFile as long as * mergePolicy is an instance of {@link LogMergePolicy}. * Otherwise an IllegalArgumentException is thrown.</p> * * @see #setUseCompoundFile(boolean) */ public boolean getUseCompoundFile() { return getLogMergePolicy().getUseCompoundFile(); } /** <p>Setting to turn on usage of a compound file. When on, * multiple files for each segment are merged into a * single file when a new segment is flushed.</p> * * <p>Note that this method is a convenience method: it * just calls mergePolicy.setUseCompoundFile as long as * mergePolicy is an instance of {@link LogMergePolicy}. * Otherwise an IllegalArgumentException is thrown.</p> */ public void setUseCompoundFile(boolean value) { getLogMergePolicy().setUseCompoundFile(value); getLogMergePolicy().setUseCompoundDocStore(value); } /** Expert: Set the Similarity implementation used by this IndexWriter. * * @see Similarity#setDefault(Similarity) */ public void setSimilarity(Similarity similarity) { ensureOpen(); this.similarity = similarity; } /** Expert: Return the Similarity implementation used by this IndexWriter. * * <p>This defaults to the current value of {@link Similarity#getDefault()}. */ public Similarity getSimilarity() { ensureOpen(); return this.similarity; } /** Expert: Set the interval between indexed terms. Large values cause less * memory to be used by IndexReader, but slow random-access to terms. Small * values cause more memory to be used by an IndexReader, and speed * random-access to terms. * * This parameter determines the amount of computation required per query * term, regardless of the number of documents that contain that term. In * particular, it is the maximum number of other terms that must be * scanned before a term is located and its frequency and position information * may be processed. In a large index with user-entered query terms, query * processing time is likely to be dominated not by term lookup but rather * by the processing of frequency and positional data. In a small index * or when many uncommon query terms are generated (e.g., by wildcard * queries) term lookup may become a dominant cost. * * In particular, <code>numUniqueTerms/interval</code> terms are read into * memory by an IndexReader, and, on average, <code>interval/2</code> terms * must be scanned for each random term access. * * @see #DEFAULT_TERM_INDEX_INTERVAL */ public void setTermIndexInterval(int interval) { ensureOpen(); this.termIndexInterval = interval; } /** Expert: Return the interval between indexed terms. * * @see #setTermIndexInterval(int) */ public int getTermIndexInterval() { ensureOpen(); return termIndexInterval; } /** * 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(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; setMessageID(); 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();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -