?? indexmodifier.java
字號:
package org.apache.lucene.index;/** * Copyright 2005 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.document.Document;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import java.io.File;import java.io.IOException;import java.io.PrintStream;/** * A class to modify an index, i.e. to delete and add documents. This * class hides {@link IndexReader} and {@link IndexWriter} so that you * do not need to care about implementation details such as that adding * documents is done via IndexWriter and deletion is done via IndexReader. * * <p>Note that you cannot create more than one <code>IndexModifier</code> object * on the same directory at the same time. * * <p>Example usage: * <!-- ======================================================== --><!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter V4.1 2004 by Markus Gebhard markus@jave.de = --><!-- = Further information: http://www.java2html.de = --><div align="left" class="java"><table border="0" cellpadding="3" cellspacing="0" bgcolor="#ffffff"> <tr> <!-- start source code --> <td nowrap="nowrap" valign="top" align="left"> <code><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/><font color="#ffffff"> </font><font color="#3f7f5f">// create an index in /tmp/index, overwriting an existing one:</font><br/><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/><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/><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/><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/><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/><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/><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/><font color="#ffffff"> </font><font color="#000000">indexModifier.flush</font><font color="#000000">()</font><font color="#000000">;</font><br/><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/><font color="#ffffff"> </font><font color="#000000">indexModifier.close</font><font color="#000000">()</font><font color="#000000">;</font></code> </td> <!-- end source code --> </tr></table></div><!-- = END of automatically generated HTML code = --><!-- ======================================================== --> * * <p>Not all methods of IndexReader and IndexWriter are offered by this * class. If you need access to additional methods, either use those classes * directly or implement your own class that extends <code>IndexModifier</code>. * * <p>Although an instance of this class can be used from more than one * thread, you will not get the best performance. You might want to use * IndexReader and IndexWriter directly for that (but you will need to * care about synchronization yourself then). * * <p>While you can freely mix calls to add() and delete() using this class, * you should batch you calls for best performance. For example, if you * want to update 20 documents, you should first delete all those documents, * then add all the new documents. * * @author Daniel Naber */public class IndexModifier { protected IndexWriter indexWriter = null; protected IndexReader indexReader = null; protected Directory directory = null; protected Analyzer analyzer = null; protected boolean open = false; // Lucene defaults: protected PrintStream infoStream = null; protected boolean useCompoundFile = true; protected int maxBufferedDocs = IndexWriter.DEFAULT_MAX_BUFFERED_DOCS; protected int maxFieldLength = IndexWriter.DEFAULT_MAX_FIELD_LENGTH; protected int mergeFactor = IndexWriter.DEFAULT_MERGE_FACTOR; /** * Open an index with write access. * * @param directory the index directory * @param analyzer the analyzer to use for adding new documents * @param create <code>true</code> to create the index or overwrite the existing one; * <code>false</code> to append to the existing index */ public IndexModifier(Directory directory, Analyzer analyzer, boolean create) throws IOException { init(directory, analyzer, create); } /** * Open an index with write access. * * @param dirName the index directory * @param analyzer the analyzer to use for adding new documents * @param create <code>true</code> to create the index or overwrite the existing one; * <code>false</code> to append to the existing index */ public IndexModifier(String dirName, Analyzer analyzer, boolean create) throws IOException { Directory dir = FSDirectory.getDirectory(dirName, create); init(dir, analyzer, create); } /** * Open an index with write access. * * @param file the index directory * @param analyzer the analyzer to use for adding new documents * @param create <code>true</code> to create the index or overwrite the existing one; * <code>false</code> to append to the existing index */ public IndexModifier(File file, Analyzer analyzer, boolean create) throws IOException { Directory dir = FSDirectory.getDirectory(file, create); init(dir, analyzer, create); } /** * Initialize an IndexWriter. * @throws IOException */ protected void init(Directory directory, Analyzer analyzer, boolean create) throws IOException { this.directory = directory; synchronized(this.directory) { this.analyzer = analyzer; indexWriter = new IndexWriter(directory, analyzer, create); open = true; } } /** * Throw an IllegalStateException if the index is closed. * @throws IllegalStateException */ protected void assureOpen() { if (!open) { throw new IllegalStateException("Index is closed"); } } /** * Close the IndexReader and open an IndexWriter. * @throws IOException */ protected void createIndexWriter() throws IOException { if (indexWriter == null) { if (indexReader != null) { indexReader.close(); indexReader = null; } indexWriter = new IndexWriter(directory, analyzer, false); indexWriter.setInfoStream(infoStream); indexWriter.setUseCompoundFile(useCompoundFile); indexWriter.setMaxBufferedDocs(maxBufferedDocs); indexWriter.setMaxFieldLength(maxFieldLength); indexWriter.setMergeFactor(mergeFactor); } } /** * Close the IndexWriter and open an IndexReader. * @throws IOException */ protected void createIndexReader() throws IOException { if (indexReader == null) { if (indexWriter != null) { indexWriter.close(); indexWriter = null; } indexReader = IndexReader.open(directory); } } /** * Make sure all changes are written to disk. * @throws IOException */ public void flush() throws IOException { synchronized(directory) { assureOpen(); if (indexWriter != null) { indexWriter.close(); indexWriter = null; createIndexWriter(); } else { indexReader.close(); indexReader = null; createIndexReader(); } } } /** * Adds a document to this index, using the provided analyzer instead of the * one specific in the constructor. If the document contains more than * {@link #setMaxFieldLength(int)} terms for a given field, the remainder are * discarded. * @see IndexWriter#addDocument(Document, Analyzer) * @throws IllegalStateException if the index is closed */ public void addDocument(Document doc, Analyzer docAnalyzer) throws IOException { synchronized(directory) { assureOpen(); createIndexWriter(); if (docAnalyzer != null) indexWriter.addDocument(doc, docAnalyzer); else indexWriter.addDocument(doc); } } /** * Adds a document to this index. If the document contains more than * {@link #setMaxFieldLength(int)} terms for a given field, the remainder are * discarded. * @see IndexWriter#addDocument(Document) * @throws IllegalStateException if the index is closed */ public void addDocument(Document doc) throws IOException { addDocument(doc, null); } /** * Deletes all documents containing <code>term</code>. * This is useful if one uses a document field to hold a unique ID string for * the document. Then to delete such a document, one merely constructs a * term with the appropriate field and the unique ID string as its text and * passes it to this method. Returns the number of documents deleted. * @return the number of documents deleted * @see IndexReader#deleteDocuments(Term) * @throws IllegalStateException if the index is closed */ public int deleteDocuments(Term term) throws IOException { synchronized(directory) { assureOpen(); createIndexReader();
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -