mayya-sharipova commented on a change in pull request #11: URL: https://github.com/apache/lucene/pull/11#discussion_r607913700
########## File path: lucene/core/src/test/org/apache/lucene/document/TestPerFieldConsistency.java ########## @@ -0,0 +1,202 @@ +/* + * 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.document; + +import static com.carrotsearch.randomizedtesting.RandomizedTest.randomDouble; +import static com.carrotsearch.randomizedtesting.RandomizedTest.randomFloat; +import static com.carrotsearch.randomizedtesting.RandomizedTest.randomInt; +import static com.carrotsearch.randomizedtesting.RandomizedTest.randomIntBetween; +import static com.carrotsearch.randomizedtesting.RandomizedTest.randomLong; + +import com.carrotsearch.randomizedtesting.generators.RandomPicks; +import java.io.IOException; +import java.util.Random; +import org.apache.lucene.index.DirectoryReader; +import org.apache.lucene.index.IndexOptions; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.index.LeafReader; +import org.apache.lucene.index.VectorValues; +import org.apache.lucene.store.Directory; +import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.LuceneTestCase; + +public class TestPerFieldConsistency extends LuceneTestCase { + + private static Field randomIndexedField(Random random, String fieldName) { + FieldType fieldType = new FieldType(); + IndexOptions indexOptions = RandomPicks.randomFrom(random, IndexOptions.values()); + while (indexOptions == IndexOptions.NONE) { + indexOptions = RandomPicks.randomFrom(random, IndexOptions.values()); + } + fieldType.setIndexOptions(indexOptions); + fieldType.setStoreTermVectors(random.nextBoolean()); + if (fieldType.storeTermVectors()) { + fieldType.setStoreTermVectorPositions(random.nextBoolean()); + if (fieldType.storeTermVectorPositions()) { + fieldType.setStoreTermVectorPayloads(random.nextBoolean()); + fieldType.setStoreTermVectorOffsets(random.nextBoolean()); + } + } + fieldType.setOmitNorms(random.nextBoolean()); + fieldType.setStored(random.nextBoolean()); + fieldType.freeze(); + + return new Field(fieldName, "randomValue", fieldType); + } + + private static Field randomPointField(Random random, String fieldName) { + switch (random.nextInt(4)) { + case 0: + return new LongPoint(fieldName, randomLong()); + case 1: + return new IntPoint(fieldName, randomInt()); + case 2: + return new DoublePoint(fieldName, randomDouble()); + default: + return new FloatPoint(fieldName, randomFloat()); + } + } + + private static Field randomDocValuesField(Random random, String fieldName) { + switch (random.nextInt(4)) { + case 0: + return new BinaryDocValuesField(fieldName, new BytesRef("randomValue")); + case 1: + return new NumericDocValuesField(fieldName, randomLong()); + case 2: + return new DoubleDocValuesField(fieldName, randomDouble()); + default: + return new SortedSetDocValuesField(fieldName, new BytesRef("randomValue")); + } + } + + private static Field randomVectorField(Random random, String fieldName) { + VectorValues.SearchStrategy searchStrategy = + RandomPicks.randomFrom(random, VectorValues.SearchStrategy.values()); + while (searchStrategy == VectorValues.SearchStrategy.NONE) { + searchStrategy = RandomPicks.randomFrom(random, VectorValues.SearchStrategy.values()); + } + float[] values = new float[randomIntBetween(1, 10)]; + for (int i = 0; i < values.length; i++) { + values[i] = randomFloat(); + } + return new VectorField(fieldName, values, searchStrategy); + } + + private static Field[] randomFieldsWithTheSameName(String fieldName) { + final Field textField = randomIndexedField(random(), fieldName); + final Field docValuesField = randomDocValuesField(random(), fieldName); + final Field pointField = randomPointField(random(), fieldName); + final Field vectorField = randomVectorField(random(), fieldName); + return new Field[] {textField, docValuesField, pointField, vectorField}; + } + + public void testDocWithMissingSchemaOptionsThrowsError() throws IOException { + try (Directory dir = newDirectory(); + IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig()); ) { + final Field[] fields = randomFieldsWithTheSameName("myfield"); + final Document doc0 = new Document(); + for (Field field : fields) { + doc0.add(field); + } + writer.addDocument(doc0); + + // the same segment: indexing a doc with a missing field throws error + int missingFieldIdx = randomIntBetween(0, fields.length - 1); Review comment: Addressed in in 8fe59c1 -- 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