msokolov commented on a change in pull request #1930:
URL: https://github.com/apache/lucene-solr/pull/1930#discussion_r506729885



##########
File path: lucene/core/src/test/org/apache/lucene/index/TestVectorValues.java
##########
@@ -0,0 +1,650 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.index;
+
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import org.apache.lucene.codecs.Codec;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.document.Field.Store;
+import org.apache.lucene.document.NumericDocValuesField;
+import org.apache.lucene.document.StringField;
+import org.apache.lucene.document.VectorField;
+import org.apache.lucene.index.VectorValues.ScoreFunction;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.SortField;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.FSDirectory;
+import org.apache.lucene.util.IOUtils;
+import org.apache.lucene.util.LuceneTestCase;
+import org.apache.lucene.util.TestUtil;
+
+import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS;
+
+/** Test Indexing/IndexWriter with vectors */
+public class TestVectorValues extends LuceneTestCase {
+
+  private IndexWriterConfig createIndexWriterConfig() {
+    IndexWriterConfig iwc = newIndexWriterConfig();
+    iwc.setCodec(Codec.forName("Lucene90"));
+    return iwc;
+  }
+
+  // Suddenly add vectors to an existing field:
+  public void testUpgradeFieldToVectors() throws Exception {
+    try (Directory dir = newDirectory()) {
+      try (IndexWriter w = new IndexWriter(dir, createIndexWriterConfig())) {
+        Document doc = new Document();
+        doc.add(newStringField("dim", "foo", Store.NO));
+        w.addDocument(doc);
+      }
+      try (IndexWriter w = new IndexWriter(dir, createIndexWriterConfig())) {
+        Document doc = new Document();
+        doc.add(new VectorField("dim", new float[4], 
ScoreFunction.DOT_PRODUCT));
+        w.addDocument(doc);
+      }
+    }
+  }
+
+  public void testFieldConstructor() {
+    float[] v = new float[1];
+    VectorField field = new VectorField("f", v);
+    assertEquals(1, field.fieldType().vectorDimension());
+    assertEquals(ScoreFunction.EUCLIDEAN, 
field.fieldType().vectorScoreFunction());
+    assertSame(v, field.vectorValue());
+  }
+
+  public void testFieldConstructorExceptions() {
+    expectThrows(IllegalArgumentException.class, () -> new VectorField(null, 
new float[1]));
+    expectThrows(IllegalArgumentException.class, () -> new VectorField("f", 
null));
+    expectThrows(IllegalArgumentException.class, () -> new VectorField("f", 
new float[1], null));
+    expectThrows(IllegalArgumentException.class, () -> new VectorField("f", 
new float[0]));
+    expectThrows(IllegalArgumentException.class, () -> new VectorField("f", 
new float[VectorValues.MAX_DIMENSIONS + 1]));
+  }
+
+  public void testFieldSetValue() {
+    VectorField field = new VectorField("f", new float[1]);
+    float[] v1 = new float[1];
+    field.setVectorValue(v1);
+    assertSame(v1, field.vectorValue());
+    expectThrows(IllegalArgumentException.class, () -> 
field.setVectorValue(new float[2]));
+    expectThrows(NullPointerException.class, () -> field.setVectorValue(null));
+  }
+
+  // Illegal schema change tests:
+
+  public void testIllegalDimChangeTwoDocs() throws Exception {
+    try (Directory dir = newDirectory();
+         IndexWriter w = new IndexWriter(dir, createIndexWriterConfig())) {
+      Document doc = new Document();
+      doc.add(new VectorField("dim", new float[4], ScoreFunction.DOT_PRODUCT));
+      w.addDocument(doc);
+      if (random().nextBoolean()) {
+        // sometimes test with two segments
+        w.commit();
+      }
+
+      Document doc2 = new Document();
+      doc2.add(new VectorField("dim", new float[3], 
ScoreFunction.DOT_PRODUCT));
+      IllegalArgumentException expected = 
expectThrows(IllegalArgumentException.class,
+          () -> w.addDocument(doc2));
+      assertEquals("cannot change vector dimension from 4 to 3 for 
field=\"dim\"", expected.getMessage());
+    }
+  }
+
+  public void testIllegalScoreFunctionChange() throws Exception {
+    try (Directory dir = newDirectory();
+         IndexWriter w = new IndexWriter(dir, createIndexWriterConfig())) {
+      Document doc = new Document();
+      doc.add(new VectorField("dim", new float[4], ScoreFunction.DOT_PRODUCT));
+      w.addDocument(doc);
+      if (random().nextBoolean()) {
+        // sometimes test with two segments
+        w.commit();
+      }
+
+      Document doc2 = new Document();
+      doc2.add(new VectorField("dim", new float[4], ScoreFunction.EUCLIDEAN));
+      IllegalArgumentException expected = 
expectThrows(IllegalArgumentException.class,
+          () -> w.addDocument(doc2));
+      assertEquals("cannot change vector score function from DOT_PRODUCT to 
EUCLIDEAN for field=\"dim\"", expected.getMessage());
+    }
+  }
+
+  public void testIllegalDimChangeTwoWriters() throws Exception {
+    try (Directory dir = newDirectory()) {
+      try (IndexWriter w = new IndexWriter(dir, createIndexWriterConfig())) {
+        Document doc = new Document();
+        doc.add(new VectorField("dim", new float[4], 
ScoreFunction.DOT_PRODUCT));
+        w.addDocument(doc);
+      }
+
+      try (IndexWriter w2 = new IndexWriter(dir, createIndexWriterConfig())) {
+        Document doc2 = new Document();
+        doc2.add(new VectorField("dim", new float[1], 
ScoreFunction.DOT_PRODUCT));
+        IllegalArgumentException expected = 
expectThrows(IllegalArgumentException.class,
+            () -> w2.addDocument(doc2));
+        assertEquals("cannot change vector dimension from 4 to 1 for 
field=\"dim\"", expected.getMessage());
+      }
+    }
+  }
+
+  public void testIllegalScoreFunctionChangeTwoWriters() throws Exception {
+    try (Directory dir = newDirectory()) {
+      try (IndexWriter w = new IndexWriter(dir, createIndexWriterConfig())) {
+        Document doc = new Document();
+        doc.add(new VectorField("dim", new float[4], 
ScoreFunction.DOT_PRODUCT));
+        w.addDocument(doc);
+      }
+
+      try (IndexWriter w2 = new IndexWriter(dir, createIndexWriterConfig())) {
+        Document doc2 = new Document();
+        doc2.add(new VectorField("dim", new float[4], 
ScoreFunction.EUCLIDEAN));
+        IllegalArgumentException expected = 
expectThrows(IllegalArgumentException.class,
+            () -> w2.addDocument(doc2));
+        assertEquals("cannot change vector score function from DOT_PRODUCT to 
EUCLIDEAN for field=\"dim\"", expected.getMessage());
+      }
+    }
+  }
+
+  public void testIllegalDimChangeViaAddIndexesDirectory() throws Exception {
+    try (Directory dir = newDirectory();
+         Directory dir2 = newDirectory()) {
+      try (IndexWriter w = new IndexWriter(dir, createIndexWriterConfig())) {
+        Document doc = new Document();
+        doc.add(new VectorField("dim", new float[4], 
ScoreFunction.DOT_PRODUCT));
+        w.addDocument(doc);
+      }
+      try (IndexWriter w2 = new IndexWriter(dir2, createIndexWriterConfig())) {
+        Document doc = new Document();
+        doc.add(new VectorField("dim", new float[5], 
ScoreFunction.DOT_PRODUCT));
+        w2.addDocument(doc);
+        IllegalArgumentException expected = 
expectThrows(IllegalArgumentException.class,
+            () -> w2.addIndexes(new Directory[]{dir}));
+        assertEquals("cannot change vector dimension from 5 to 4 for 
field=\"dim\"", expected.getMessage());
+      }
+    }
+  }
+
+  public void testIllegalScoreFunctionChangeViaAddIndexesDirectory() throws 
Exception {
+    try (Directory dir = newDirectory();
+         Directory dir2 = newDirectory()) {
+      try (IndexWriter w = new IndexWriter(dir, createIndexWriterConfig())) {
+        Document doc = new Document();
+        doc.add(new VectorField("dim", new float[4], 
ScoreFunction.DOT_PRODUCT));
+        w.addDocument(doc);
+      }
+      try (IndexWriter w2 = new IndexWriter(dir2, createIndexWriterConfig())) {
+        Document doc = new Document();
+        doc.add(new VectorField("dim", new float[4], ScoreFunction.EUCLIDEAN));
+        w2.addDocument(doc);
+        IllegalArgumentException expected = 
expectThrows(IllegalArgumentException.class,
+            () -> w2.addIndexes(dir));
+        assertEquals("cannot change vector score function from EUCLIDEAN to 
DOT_PRODUCT for field=\"dim\"", expected.getMessage());
+      }
+    }
+  }
+
+  public void testIllegalDimChangeViaAddIndexesCodecReader() throws Exception {
+    try (Directory dir = newDirectory();
+         Directory dir2 = newDirectory()) {
+      try (IndexWriter w = new IndexWriter(dir, createIndexWriterConfig())) {
+        Document doc = new Document();
+        doc.add(new VectorField("dim", new float[4], 
ScoreFunction.DOT_PRODUCT));
+        w.addDocument(doc);
+      }
+      try (IndexWriter w2 = new IndexWriter(dir2, createIndexWriterConfig())) {
+        Document doc = new Document();
+        doc.add(new VectorField("dim", new float[5], 
ScoreFunction.DOT_PRODUCT));
+        w2.addDocument(doc);
+        try (DirectoryReader r = DirectoryReader.open(dir)) {
+          IllegalArgumentException expected = 
expectThrows(IllegalArgumentException.class,
+              () -> w2.addIndexes(new CodecReader[]{(CodecReader) 
getOnlyLeafReader(r)}));
+          assertEquals("cannot change vector dimension from 5 to 4 for 
field=\"dim\"", expected.getMessage());
+        }
+      }
+    }
+  }
+
+  public void testIllegalScoreFunctionChangeViaAddIndexesCodecReader() throws 
Exception {
+    try (Directory dir = newDirectory();
+         Directory dir2 = newDirectory()) {
+      try (IndexWriter w = new IndexWriter(dir, createIndexWriterConfig())) {
+        Document doc = new Document();
+        doc.add(new VectorField("dim", new float[4], 
ScoreFunction.DOT_PRODUCT));
+        w.addDocument(doc);
+      }
+      try (IndexWriter w2 = new IndexWriter(dir2, createIndexWriterConfig())) {
+        Document doc = new Document();
+        doc.add(new VectorField("dim", new float[4], ScoreFunction.EUCLIDEAN));
+        w2.addDocument(doc);
+        try (DirectoryReader r = DirectoryReader.open(dir)) {
+          IllegalArgumentException expected = 
expectThrows(IllegalArgumentException.class,
+              () -> w2.addIndexes(new CodecReader[]{(CodecReader) 
getOnlyLeafReader(r)}));
+          assertEquals("cannot change vector score function from EUCLIDEAN to 
DOT_PRODUCT for field=\"dim\"", expected.getMessage());
+        }
+      }
+    }
+  }
+
+  public void testIllegalDimChangeViaAddIndexesSlowCodecReader() throws 
Exception {
+    try (Directory dir = newDirectory();
+         Directory dir2 = newDirectory()) {
+      try (IndexWriter w = new IndexWriter(dir, createIndexWriterConfig())) {
+        Document doc = new Document();
+        doc.add(new VectorField("dim", new float[4], 
ScoreFunction.DOT_PRODUCT));
+        w.addDocument(doc);
+      }
+      try (IndexWriter w2 = new IndexWriter(dir2, createIndexWriterConfig())) {
+        Document doc = new Document();
+        doc.add(new VectorField("dim", new float[5], 
ScoreFunction.DOT_PRODUCT));
+        w2.addDocument(doc);
+        try (DirectoryReader r = DirectoryReader.open(dir)) {
+          IllegalArgumentException expected = 
expectThrows(IllegalArgumentException.class,
+              () -> TestUtil.addIndexesSlowly(w2, r));
+          assertEquals("cannot change vector dimension from 5 to 4 for 
field=\"dim\"", expected.getMessage());
+        }
+      }
+    }
+  }
+
+  public void testIllegalScoreFunctionChangeViaAddIndexesSlowCodecReader() 
throws Exception {

Review comment:
       Ah, good idea, I will add




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to