LUCENE-1084: allow user-specified max field length when instantiating IndexWriter

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@619545 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2008-02-07 18:22:21 +00:00
parent 00941055fd
commit 9a186ed521
2 changed files with 65 additions and 13 deletions

View File

@ -439,7 +439,8 @@ public class IndexWriter {
* @param create <code>true</code> to create the index or overwrite
* the existing one; <code>false</code> to append to the existing
* index
* @param mfl whether or not to limit field lengths
* @param mfl Maximum field length: LIMITED, UNLIMITED, or user-specified
* via the MaxFieldLength constructor.
* @throws CorruptIndexException if the index is corrupt
* @throws LockObtainFailedException if another writer
* has this index open (<code>write.lock</code> could not
@ -492,7 +493,8 @@ public class IndexWriter {
* @param create <code>true</code> to create the index or overwrite
* the existing one; <code>false</code> to append to the existing
* index
* @param mfl whether or not to limit field lengths
* @param mfl Maximum field length: LIMITED, UNLIMITED, or user-specified
* via the MaxFieldLength constructor.
* @throws CorruptIndexException if the index is corrupt
* @throws LockObtainFailedException if another writer
* has this index open (<code>write.lock</code> could not
@ -545,7 +547,8 @@ public class IndexWriter {
* @param create <code>true</code> to create the index or overwrite
* the existing one; <code>false</code> to append to the existing
* index
* @param mfl whether or not to limit field lengths
* @param mfl Maximum field length: LIMITED, UNLIMITED, or user-specified
* via the MaxFieldLength constructor.
* @throws CorruptIndexException if the index is corrupt
* @throws LockObtainFailedException if another writer
* has this index open (<code>write.lock</code> could not
@ -595,7 +598,8 @@ public class IndexWriter {
*
* @param path the path to the index directory
* @param a the analyzer to use
* @param mfl whether or not to limit field lengths
* @param mfl Maximum field length: LIMITED, UNLIMITED, or user-specified
* via the MaxFieldLength constructor.
* @throws CorruptIndexException if the index is corrupt
* @throws LockObtainFailedException if another writer
* has this index open (<code>write.lock</code> could not
@ -640,7 +644,8 @@ public class IndexWriter {
*
* @param path the path to the index directory
* @param a the analyzer to use
* @param mfl whether or not to limit field lengths
* @param mfl Maximum field length: LIMITED, UNLIMITED, or user-specified
* via the MaxFieldLength constructor.
* @throws CorruptIndexException if the index is corrupt
* @throws LockObtainFailedException if another writer
* has this index open (<code>write.lock</code> could not
@ -685,7 +690,8 @@ public class IndexWriter {
*
* @param d the index directory
* @param a the analyzer to use
* @param mfl whether or not to limit field lengths
* @param mfl Maximum field length: LIMITED, UNLIMITED, or user-specified
* via the MaxFieldLength constructor.
* @throws CorruptIndexException if the index is corrupt
* @throws LockObtainFailedException if another writer
* has this index open (<code>write.lock</code> could not
@ -731,7 +737,8 @@ public class IndexWriter {
* @param d the index directory
* @param autoCommit see <a href="#autoCommit">above</a>
* @param a the analyzer to use
* @param mfl whether or not to limit field lengths
* @param mfl Maximum field length: LIMITED, UNLIMITED, or user-specified
* via the MaxFieldLength constructor.
* @throws CorruptIndexException if the index is corrupt
* @throws LockObtainFailedException if another writer
* has this index open (<code>write.lock</code> could not
@ -781,7 +788,8 @@ public class IndexWriter {
* @param create <code>true</code> to create the index or overwrite
* the existing one; <code>false</code> to append to the existing
* index
* @param mfl whether or not to limit field lengths
* @param mfl Maximum field length: LIMITED, UNLIMITED, or user-specified
* via the MaxFieldLength constructor.
* @throws CorruptIndexException if the index is corrupt
* @throws LockObtainFailedException if another writer
* has this index open (<code>write.lock</code> could not
@ -834,7 +842,8 @@ public class IndexWriter {
* @param autoCommit see <a href="#autoCommit">above</a>
* @param a the analyzer to use
* @param deletionPolicy see <a href="#deletionPolicy">above</a>
* @param mfl whether or not to limit field lengths
* @param mfl Maximum field length: LIMITED, UNLIMITED, or user-specified
* via the MaxFieldLength constructor.
* @throws CorruptIndexException if the index is corrupt
* @throws LockObtainFailedException if another writer
* has this index open (<code>write.lock</code> could not
@ -888,7 +897,8 @@ public class IndexWriter {
* the existing one; <code>false</code> to append to the existing
* index
* @param deletionPolicy see <a href="#deletionPolicy">above</a>
* @param mfl whether or not to limit field lengths
* @param mfl Maximum field length: LIMITED, UNLIMITED, or user-specified
* via the MaxFieldLength constructor.
* @throws CorruptIndexException if the index is corrupt
* @throws LockObtainFailedException if another writer
* has this index open (<code>write.lock</code> could not
@ -3639,19 +3649,39 @@ public class IndexWriter {
* {@link IndexWriter#setMaxFieldLength(int)} overrides the value set by
* the constructor.
*/
public static final class MaxFieldLength extends Parameter implements java.io.Serializable {
public static final class MaxFieldLength {
private int limit;
private String name;
/**
* Private type-safe-enum-pattern constructor.
*
* @param name instance name
* @param limit maximum field length
*/
private MaxFieldLength(String name, int limit) {
// typesafe enum pattern, no public constructor
super(name);
this.name = name;
this.limit = limit;
}
/**
* Public constructor to allow users to specify the maximum field size limit.
*
* @param limit The maximum field length
*/
public MaxFieldLength(int limit) {
this("User-specified", limit);
}
public int getLimit() {
return limit;
}
public String toString()
{
return name + ":" + limit;
}
/** Sets the maximum field length to {@link Integer#MAX_VALUE}. */
public static final MaxFieldLength UNLIMITED

View File

@ -2682,4 +2682,26 @@ public class TestIndexWriter extends LuceneTestCase
reader.close();
dir.close();
}
// LUCENE-1084: test user-specified field length
public void testUserSpecifiedMaxFieldLength() throws IOException {
Directory dir = new MockRAMDirectory();
IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), new IndexWriter.MaxFieldLength(100000));
Document doc = new Document();
StringBuffer b = new StringBuffer();
for(int i=0;i<10000;i++)
b.append(" a");
b.append(" x");
doc.add(new Field("field", b.toString(), Field.Store.NO, Field.Index.TOKENIZED));
writer.addDocument(doc);
writer.close();
IndexReader reader = IndexReader.open(dir);
Term t = new Term("field", "x");
assertEquals(1, reader.docFreq(t));
reader.close();
dir.close();
}
}