Index: src/test/org/apache/lucene/index/TestIndexWriter.java =================================================================== --- src/test/org/apache/lucene/index/TestIndexWriter.java (revision 567086) +++ src/test/org/apache/lucene/index/TestIndexWriter.java (working copy) @@ -25,6 +25,7 @@ import junit.framework.TestCase; import org.apache.lucene.analysis.WhitespaceAnalyzer; +import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexReader; @@ -491,6 +492,28 @@ } /** + * Make sure we get a friendly exception for a wicked + * long term. + */ + public void testWickedLongTerm() throws IOException { + RAMDirectory dir = new RAMDirectory(); + IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true); + + char[] chars = new char[16384]; + Arrays.fill(chars, 'x'); + Document doc = new Document(); + String contents = "a b c " + new String(chars); + doc.add(new Field("content", contents, Field.Store.NO, Field.Index.TOKENIZED)); + try { + writer.addDocument(doc); + fail("did not hit expected exception"); + } catch (IllegalArgumentException e) { + } + writer.close(); + dir.close(); + } + + /** * Make sure optimize doesn't use any more than 1X * starting index size as its temporary free space * required. Index: src/java/org/apache/lucene/index/DocumentsWriter.java =================================================================== --- src/java/org/apache/lucene/index/DocumentsWriter.java (revision 567086) +++ src/java/org/apache/lucene/index/DocumentsWriter.java (working copy) @@ -1452,8 +1452,11 @@ p = postingsFreeList[--postingsFreeCount]; final int textLen1 = 1+tokenTextLen; - if (textLen1 + charPool.byteUpto > CHAR_BLOCK_SIZE) + if (textLen1 + charPool.byteUpto > CHAR_BLOCK_SIZE) { + if (textLen1 > CHAR_BLOCK_SIZE) + throw new IllegalArgumentException("term length " + tokenTextLen + " exceeds max term length " + (CHAR_BLOCK_SIZE-1)); charPool.nextBuffer(); + } final char[] text = charPool.buffer; final int textUpto = charPool.byteUpto; p.textStart = textUpto + charPool.byteOffset; Index: src/java/org/apache/lucene/index/IndexWriter.java =================================================================== --- src/java/org/apache/lucene/index/IndexWriter.java (revision 567086) +++ src/java/org/apache/lucene/index/IndexWriter.java (working copy) @@ -1091,6 +1091,10 @@ * temporary space usage) then the maximum free disk space * required is the same as {@link #optimize}.
* + *Note that each term in the document can be no longer + * than 16383 characters, otherwise an + * IllegalArgumentException will be thrown.
+ * * @throws CorruptIndexException if the index is corrupt * @throws IOException if there is a low-level IO error */