diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSimpleDocValuesFormat.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSimpleDocValuesFormat.java index 053861a25d5..19aa562a5fd 100644 --- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSimpleDocValuesFormat.java +++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSimpleDocValuesFormat.java @@ -156,8 +156,14 @@ public class SimpleTextSimpleDocValuesFormat extends SimpleDocValuesFormat { SimpleTextUtil.write(data, Long.toString(minValue), scratch); SimpleTextUtil.writeNewline(data); - // build up our fixed-width "simple text packed ints" format - int maxBytesPerValue = BigInteger.valueOf(maxValue).subtract(BigInteger.valueOf(minValue)).toString().length(); + assert maxValue >= minValue; + + // build up our fixed-width "simple text packed ints" + // format + BigInteger maxBig = BigInteger.valueOf(maxValue); + BigInteger minBig = BigInteger.valueOf(minValue); + BigInteger diffBig = maxBig.subtract(minBig); + int maxBytesPerValue = diffBig.toString().length(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < maxBytesPerValue; i++) { sb.append('0'); @@ -167,8 +173,10 @@ public class SimpleTextSimpleDocValuesFormat extends SimpleDocValuesFormat { SimpleTextUtil.write(data, PATTERN); SimpleTextUtil.write(data, sb.toString(), scratch); SimpleTextUtil.writeNewline(data); + + final String patternString = sb.toString(); - final DecimalFormat encoder = new DecimalFormat(sb.toString(), new DecimalFormatSymbols(Locale.ROOT)); + final DecimalFormat encoder = new DecimalFormat(patternString, new DecimalFormatSymbols(Locale.ROOT)); return new NumericDocValuesConsumer() { int numDocsWritten = 0; @@ -176,7 +184,9 @@ public class SimpleTextSimpleDocValuesFormat extends SimpleDocValuesFormat { public void add(long value) throws IOException { assert value >= minValue; Number delta = BigInteger.valueOf(value).subtract(BigInteger.valueOf(minValue)); - SimpleTextUtil.write(data, encoder.format(delta), scratch); + String s = encoder.format(delta); + assert s.length() == patternString.length(); + SimpleTextUtil.write(data, s, scratch); SimpleTextUtil.writeNewline(data); numDocsWritten++; } diff --git a/lucene/core/src/java/org/apache/lucene/codecs/SimpleDVConsumer.java b/lucene/core/src/java/org/apache/lucene/codecs/SimpleDVConsumer.java index 6b359996f6f..02ef1d331be 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/SimpleDVConsumer.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/SimpleDVConsumer.java @@ -91,7 +91,7 @@ public abstract class SimpleDVConsumer implements Closeable { if (liveDocs == null || liveDocs.get(i)) { long val = docValues.get(i); minValue = Math.min(val, minValue); - maxValue = Math.min(val, maxValue); + maxValue = Math.max(val, maxValue); } mergeState.checkAbort.work(300); } diff --git a/lucene/core/src/test/org/apache/lucene/TestDemoDocValue.java b/lucene/core/src/test/org/apache/lucene/TestDemoDocValue.java index 2347e073b6f..dc1c55c209a 100644 --- a/lucene/core/src/test/org/apache/lucene/TestDemoDocValue.java +++ b/lucene/core/src/test/org/apache/lucene/TestDemoDocValue.java @@ -329,12 +329,12 @@ public class TestDemoDocValue extends LuceneTestCase { IndexWriter iwriter = new IndexWriter(directory, iwc); Document doc = new Document(); doc.add(newField("id", "0", StringField.TYPE_STORED)); - doc.add(new PackedLongDocValuesField("dv", 1)); + doc.add(new PackedLongDocValuesField("dv", -10)); iwriter.addDocument(doc); iwriter.commit(); doc = new Document(); doc.add(newField("id", "1", StringField.TYPE_STORED)); - doc.add(new PackedLongDocValuesField("dv", 3)); + doc.add(new PackedLongDocValuesField("dv", 99)); iwriter.addDocument(doc); iwriter.forceMerge(1); iwriter.close(); @@ -347,9 +347,9 @@ public class TestDemoDocValue extends LuceneTestCase { StoredDocument doc2 = ireader.leaves().get(0).reader().document(i); long expected; if (doc2.get("id").equals("0")) { - expected = 1; + expected = -10; } else { - expected = 3; + expected = 99; } assertEquals(expected, dv.get(i)); } @@ -386,6 +386,34 @@ public class TestDemoDocValue extends LuceneTestCase { directory.close(); } + public void testRange2() throws IOException { + Analyzer analyzer = new MockAnalyzer(random()); + + Directory directory = newDirectory(); + // we don't use RandomIndexWriter because it might add more docvalues than we expect !!!!1 + IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer); + iwc.setMergePolicy(newLogMergePolicy()); + IndexWriter iwriter = new IndexWriter(directory, iwc); + Document doc = new Document(); + doc.add(new PackedLongDocValuesField("dv", -8841491950446638677L)); + iwriter.addDocument(doc); + doc = new Document(); + doc.add(new PackedLongDocValuesField("dv", 9062230939892376225L)); + iwriter.addDocument(doc); + iwriter.forceMerge(1); + iwriter.close(); + + // Now search the index: + IndexReader ireader = DirectoryReader.open(directory); // read-only=true + assert ireader.leaves().size() == 1; + NumericDocValues dv = ireader.leaves().get(0).reader().getNumericDocValues("dv"); + assertEquals(-8841491950446638677L, dv.get(0)); + assertEquals(9062230939892376225L, dv.get(1)); + + ireader.close(); + directory.close(); + } + public void testDemoBytes() throws IOException { Analyzer analyzer = new MockAnalyzer(random());