From 9a186ed521395673cf6b5b906818649dc3dbdafd Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Thu, 7 Feb 2008 18:22:21 +0000 Subject: [PATCH] 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 --- .../org/apache/lucene/index/IndexWriter.java | 56 ++++++++++++++----- .../apache/lucene/index/TestIndexWriter.java | 22 ++++++++ 2 files changed, 65 insertions(+), 13 deletions(-) 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 true to create the index or overwrite * the existing one; false 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 (write.lock could not @@ -492,7 +493,8 @@ public class IndexWriter { * @param create true to create the index or overwrite * the existing one; false 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 (write.lock could not @@ -545,7 +547,8 @@ public class IndexWriter { * @param create true to create the index or overwrite * the existing one; false 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 (write.lock 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 (write.lock 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 (write.lock 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 (write.lock could not @@ -731,7 +737,8 @@ public class IndexWriter { * @param d the index directory * @param autoCommit see above * @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 (write.lock could not @@ -781,7 +788,8 @@ public class IndexWriter { * @param create true to create the index or overwrite * the existing one; false 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 (write.lock could not @@ -834,7 +842,8 @@ public class IndexWriter { * @param autoCommit see above * @param a the analyzer to use * @param deletionPolicy see above - * @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 (write.lock could not @@ -888,7 +897,8 @@ public class IndexWriter { * the existing one; false to append to the existing * index * @param deletionPolicy see above - * @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 (write.lock 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(); + } }