Skip to content

Apache Lucene open-source search software

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

Michael McCandless9a186ed52139
diff --git a/src/java/org/apache/lucene/index/IndexWriter.java b/src/java/org/apache/lucene/index/IndexWriter.java
index ef1de08a9a8..fb811285430 100644
--- a/src/java/org/apache/lucene/index/IndexWriter.java
+++ b/src/java/org/apache/lucene/index/IndexWriter.java
@@ -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
diff --git a/src/test/org/apache/lucene/index/TestIndexWriter.java b/src/test/org/apache/lucene/index/TestIndexWriter.java
index 456cfa6b990..fc9d3a2bdf5 100644
--- a/src/test/org/apache/lucene/index/TestIndexWriter.java
+++ b/src/test/org/apache/lucene/index/TestIndexWriter.java
@@ -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();
+ }
}