Skip to content

Apache Lucene open-source search software

LUCENE-9905: Allow Lucene90Codec to be configured with a per-field vector format (#164)

Julie Tibshiranieecd1971fa74
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90Codec.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90Codec.java
index 4fef31d274e..9615058760c 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90Codec.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90Codec.java
@@ -63,8 +63,9 @@ public class Lucene90Codec extends Codec {
private final SegmentInfoFormat segmentInfosFormat = new Lucene90SegmentInfoFormat();
private final LiveDocsFormat liveDocsFormat = new Lucene90LiveDocsFormat();
private final CompoundFormat compoundFormat = new Lucene90CompoundFormat();
- private final PostingsFormat defaultFormat;
+ private final NormsFormat normsFormat = new Lucene90NormsFormat();
+ private final PostingsFormat defaultPostingsFormat;
private final PostingsFormat postingsFormat =
new PerFieldPostingsFormat() {
@Override
@@ -73,6 +74,7 @@ public class Lucene90Codec extends Codec {
}
};
+ private final DocValuesFormat defaultDVFormat;
private final DocValuesFormat docValuesFormat =
new PerFieldDocValuesFormat() {
@Override
@@ -81,11 +83,12 @@ public class Lucene90Codec extends Codec {
}
};
+ private final VectorFormat defaultVectorFormat;
private final VectorFormat vectorFormat =
new PerFieldVectorFormat() {
@Override
public VectorFormat getVectorFormatForField(String field) {
- return new Lucene90HnswVectorFormat();
+ return Lucene90Codec.this.getVectorFormatForField(field);
}
};
@@ -105,8 +108,9 @@ public class Lucene90Codec extends Codec {
super("Lucene90");
this.storedFieldsFormat =
new Lucene90StoredFieldsFormat(Objects.requireNonNull(mode).storedMode);
- this.defaultFormat = new Lucene90PostingsFormat();
+ this.defaultPostingsFormat = new Lucene90PostingsFormat();
this.defaultDVFormat = new Lucene90DocValuesFormat();
+ this.defaultVectorFormat = new Lucene90HnswVectorFormat();
}
@Override
@@ -163,7 +167,7 @@ public class Lucene90Codec extends Codec {
* future version of Lucene are only guaranteed to be able to read the default implementation,
*/
public PostingsFormat getPostingsFormatForField(String field) {
- return defaultFormat;
+ return defaultPostingsFormat;
}
/**
@@ -179,15 +183,23 @@ public class Lucene90Codec extends Codec {
return defaultDVFormat;
}
+ /**
+ * Returns the vectors format that should be used for writing new segments of <code>field</code>
+ *
+ * <p>The default implementation always returns "Lucene90".
+ *
+ * <p><b>WARNING:</b> if you subclass, you are responsible for index backwards compatibility:
+ * future version of Lucene are only guaranteed to be able to read the default implementation.
+ */
+ public VectorFormat getVectorFormatForField(String field) {
+ return defaultVectorFormat;
+ }
+
@Override
public final DocValuesFormat docValuesFormat() {
return docValuesFormat;
}
- private final DocValuesFormat defaultDVFormat;
-
- private final NormsFormat normsFormat = new Lucene90NormsFormat();
-
@Override
public final NormsFormat normsFormat() {
return normsFormat;
diff --git a/lucene/core/src/test/org/apache/lucene/codecs/perfield/TestPerFieldVectorFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/perfield/TestPerFieldVectorFormat.java
index 0ae1e5b57d0..ab0f68ff53f 100644
--- a/lucene/core/src/test/org/apache/lucene/codecs/perfield/TestPerFieldVectorFormat.java
+++ b/lucene/core/src/test/org/apache/lucene/codecs/perfield/TestPerFieldVectorFormat.java
@@ -52,53 +52,54 @@ public class TestPerFieldVectorFormat extends BaseVectorFormatTestCase {
return codec;
}
- // just a simple trivial test
public void testTwoFieldsTwoFormats() throws IOException {
Analyzer analyzer = new MockAnalyzer(random());
try (Directory directory = newDirectory()) {
// we don't use RandomIndexWriter because it might add more values than we expect !!!!1
IndexWriterConfig iwc = newIndexWriterConfig(analyzer);
- final VectorFormat fast = TestUtil.getDefaultVectorFormat();
- final VectorFormat slow = VectorFormat.forName("Asserting");
+ VectorFormat defaultFormat = TestUtil.getDefaultVectorFormat();
+ VectorFormat emptyFormat = VectorFormat.EMPTY;
iwc.setCodec(
new AssertingCodec() {
@Override
public VectorFormat getVectorFormatForField(String field) {
- if ("v1".equals(field)) {
- return fast;
+ if ("empty".equals(field)) {
+ return emptyFormat;
} else {
- return slow;
+ return defaultFormat;
}
}
});
+
try (IndexWriter iwriter = new IndexWriter(directory, iwc)) {
Document doc = new Document();
doc.add(newTextField("id", "1", Field.Store.YES));
- doc.add(new VectorField("v1", new float[] {1, 2, 3}));
+ doc.add(new VectorField("field", new float[] {1, 2, 3}));
iwriter.addDocument(doc);
- doc = new Document();
+ iwriter.commit();
+
+ // Check that we use the empty vector format, which doesn't support writes
+ doc.clear();
doc.add(newTextField("id", "2", Field.Store.YES));
- doc.add(new VectorField("v2", new float[] {4, 5, 6}));
- iwriter.addDocument(doc);
+ doc.add(new VectorField("empty", new float[] {4, 5, 6}));
+ expectThrows(
+ RuntimeException.class,
+ () -> {
+ iwriter.addDocument(doc);
+ iwriter.commit();
+ });
}
- // Now search the index:
+ // Now search for the field that was successfully indexed
try (IndexReader ireader = DirectoryReader.open(directory)) {
TopDocs hits1 =
ireader
.leaves()
.get(0)
.reader()
- .searchNearestVectors("v1", new float[] {1, 2, 3}, 10, 1);
+ .searchNearestVectors("field", new float[] {1, 2, 3}, 10, 1);
assertEquals(1, hits1.scoreDocs.length);
- TopDocs hits2 =
- ireader
- .leaves()
- .get(0)
- .reader()
- .searchNearestVectors("v2", new float[] {1, 2, 3}, 10, 1);
- assertEquals(1, hits2.scoreDocs.length);
}
}
}
diff --git a/lucene/test-framework/src/java/org/apache/lucene/codecs/asserting/AssertingCodec.java b/lucene/test-framework/src/java/org/apache/lucene/codecs/asserting/AssertingCodec.java
index 5d1dcf32968..5b3b6ee814a 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/codecs/asserting/AssertingCodec.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/codecs/asserting/AssertingCodec.java
@@ -27,6 +27,7 @@ import org.apache.lucene.codecs.TermVectorsFormat;
import org.apache.lucene.codecs.VectorFormat;
import org.apache.lucene.codecs.perfield.PerFieldDocValuesFormat;
import org.apache.lucene.codecs.perfield.PerFieldPostingsFormat;
+import org.apache.lucene.codecs.perfield.PerFieldVectorFormat;
import org.apache.lucene.util.TestUtil;
/** Acts like the default codec but with additional asserts. */
@@ -61,6 +62,14 @@ public class AssertingCodec extends FilterCodec {
}
};
+ private final VectorFormat vectorFormat =
+ new PerFieldVectorFormat() {
+ @Override
+ public VectorFormat getVectorFormatForField(String field) {
+ return AssertingCodec.this.getVectorFormatForField(field);
+ }
+ };
+
private final TermVectorsFormat vectors = new AssertingTermVectorsFormat();
private final StoredFieldsFormat storedFields = new AssertingStoredFieldsFormat();
private final NormsFormat norms = new AssertingNormsFormat();
@@ -111,7 +120,7 @@ public class AssertingCodec extends FilterCodec {
@Override
public VectorFormat vectorFormat() {
- return defaultVectorFormat;
+ return vectorFormat;
}
@Override
@@ -138,6 +147,11 @@ public class AssertingCodec extends FilterCodec {
return defaultDVFormat;
}
+ /**
+ * Returns the vectors format that should be used for writing new segments of <code>field</code>.
+ *
+ * <p>The default implementation always returns "Asserting"
+ */
public VectorFormat getVectorFormatForField(String field) {
return defaultVectorFormat;
}