diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 88b09e9badc..f5b056e5207 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -231,6 +231,9 @@ Improvements * LUCENE-8529: TopSuggestDocsCollector will now use the completion key to tiebreak completion suggestion with identical scores. (Jim Ferenczi) +* LUCENE-8575: SegmentInfos#toString now includes attributes and diagnostics. + (Namgyu Kim via Adrien Grand) + Optimizations * LUCENE-8552: FieldInfos.getMergedFieldInfos no longer does any merging if there is <= 1 segment. diff --git a/lucene/core/src/java/org/apache/lucene/index/SegmentInfo.java b/lucene/core/src/java/org/apache/lucene/index/SegmentInfo.java index 5e6d773cc95..7c91eeb2ff3 100644 --- a/lucene/core/src/java/org/apache/lucene/index/SegmentInfo.java +++ b/lucene/core/src/java/org/apache/lucene/index/SegmentInfo.java @@ -211,7 +211,17 @@ public final class SegmentInfo { s.append(']'); } - // TODO: we could append toString of attributes() here? + if (!diagnostics.isEmpty()) { + s.append(":[diagnostics="); + s.append(diagnostics.toString()); + s.append(']'); + } + + if (!attributes.isEmpty()) { + s.append(":[attributes="); + s.append(attributes.toString()); + s.append(']'); + } return s.toString(); } diff --git a/lucene/core/src/test/org/apache/lucene/index/TestSegmentInfos.java b/lucene/core/src/test/org/apache/lucene/index/TestSegmentInfos.java index de78ffc50d0..7611d156367 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestSegmentInfos.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestSegmentInfos.java @@ -18,7 +18,9 @@ package org.apache.lucene.index; import org.apache.lucene.codecs.Codec; +import org.apache.lucene.search.Sort; import org.apache.lucene.store.BaseDirectoryWrapper; +import org.apache.lucene.store.Directory; import org.apache.lucene.store.IOContext; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.StringHelper; @@ -26,6 +28,9 @@ import org.apache.lucene.util.Version; import java.io.IOException; import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; public class TestSegmentInfos extends LuceneTestCase { @@ -98,5 +103,53 @@ public class TestSegmentInfos extends LuceneTestCase { assertEquals(Version.LATEST, sis.getCommitLuceneVersion()); dir.close(); } + + /** Test toString method */ + public void testToString() throws Throwable{ + SegmentInfo si; + final Directory dir = newDirectory(); + Codec codec = Codec.getDefault(); + + // diagnostics map + Map diagnostics = new LinkedHashMap<>(); + diagnostics.put("key1", "value1"); + diagnostics.put("key2", "value2"); + + // attributes map + Map attributes = new LinkedHashMap<>(); + attributes.put("key1", "value1"); + attributes.put("key2", "value2"); + + // diagnostics X, attributes X + si = new SegmentInfo(dir, Version.LATEST, Version.LATEST, "TEST", 10000, false, codec, Collections.emptyMap(), StringHelper.randomId(), new HashMap<>(), Sort.INDEXORDER); + assertEquals("TEST(" + Version.LATEST.toString() + ")" + + ":C10000" + + ":[indexSort=]", si.toString()); + + // diagnostics O, attributes X + si = new SegmentInfo(dir, Version.LATEST, Version.LATEST, "TEST", 10000, false, codec, diagnostics, StringHelper.randomId(), new HashMap<>(), Sort.INDEXORDER); + assertEquals("TEST(" + Version.LATEST.toString() + ")" + + ":C10000" + + ":[indexSort=]" + + ":[diagnostics={key1=value1, key2=value2}]", si.toString()); + + // diagnostics X, attributes O + si = new SegmentInfo(dir, Version.LATEST, Version.LATEST, "TEST", 10000, false, codec, Collections.emptyMap(), StringHelper.randomId(), attributes, Sort.INDEXORDER); + assertEquals("TEST(" + Version.LATEST.toString() + ")" + + ":C10000" + + ":[indexSort=]" + + ":[attributes={key1=value1, key2=value2}]", si.toString()); + + // diagnostics O, attributes O + si = new SegmentInfo(dir, Version.LATEST, Version.LATEST, "TEST", 10000, false, codec, diagnostics, StringHelper.randomId(), attributes, Sort.INDEXORDER); + System.out.println(si.toString()); + assertEquals("TEST(" + Version.LATEST.toString() + ")" + + ":C10000" + + ":[indexSort=]" + + ":[diagnostics={key1=value1, key2=value2}]" + + ":[attributes={key1=value1, key2=value2}]", si.toString()); + + dir.close(); + } }