From 56e1ad484a2c7431932a95e442bc1a584f60698e Mon Sep 17 00:00:00 2001 From: Shalin Shekhar Mangar Date: Tue, 25 Apr 2017 00:26:21 +0530 Subject: [PATCH] SOLR-10047: Move test into its own test class and force use of NoMergePolicy to fix test failures This closes #195 --- .../org/apache/solr/schema/DocValuesTest.java | 72 ---------- .../solr/schema/TestHalfAndHalfDocValues.java | 136 ++++++++++++++++++ 2 files changed, 136 insertions(+), 72 deletions(-) create mode 100644 solr/core/src/test/org/apache/solr/schema/TestHalfAndHalfDocValues.java diff --git a/solr/core/src/test/org/apache/solr/schema/DocValuesTest.java b/solr/core/src/test/org/apache/solr/schema/DocValuesTest.java index 2d8afee18a9..cf43a68d2e1 100644 --- a/solr/core/src/test/org/apache/solr/schema/DocValuesTest.java +++ b/solr/core/src/test/org/apache/solr/schema/DocValuesTest.java @@ -25,14 +25,10 @@ import java.util.List; import java.util.function.Function; import java.util.function.Supplier; -import org.apache.lucene.document.Document; -import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.DocValuesType; import org.apache.lucene.index.FieldInfos; import org.apache.lucene.index.IndexableField; import org.apache.lucene.index.LeafReader; -import org.apache.lucene.index.LeafReaderContext; -import org.apache.lucene.index.MultiFields; import org.apache.lucene.index.NumericDocValues; import org.apache.lucene.index.SortedDocValues; import org.apache.lucene.queries.function.FunctionValues; @@ -155,74 +151,6 @@ public class DocValuesTest extends SolrTestCaseJ4 { } } - - public void testHalfAndHalfDocValues() throws Exception { - // Insert two docs without docvalues - String fieldname = "string_add_dv_later"; - assertU(adoc("id", "3", fieldname, "c")); - assertU(commit()); - assertU(adoc("id", "1", fieldname, "a")); - assertU(commit()); - - - try (SolrCore core = h.getCoreInc()) { - assertFalse(core.getLatestSchema().getField(fieldname).hasDocValues()); - // Add docvalues to the field type - IndexSchema schema = core.getLatestSchema(); - SchemaField oldField = schema.getField(fieldname); - int newProperties = oldField.getProperties() | SchemaField.DOC_VALUES; - - SchemaField sf = new SchemaField( fieldname, oldField.getType(), newProperties, null); - schema.getFields().put( fieldname, sf ); - - // Insert a new doc with docvalues - assertU(adoc("id", "2", fieldname, "b")); - assertU(commit()); - - - // Check there are a mix of segments with and without docvalues - final RefCounted searcherRef = core.openNewSearcher(true, true); - final SolrIndexSearcher searcher = searcherRef.get(); - try { - final DirectoryReader topReader = searcher.getRawReader(); - - //Assert no merges - - assertEquals(3, topReader.numDocs()); - assertEquals(3, topReader.leaves().size()); - - final FieldInfos infos = MultiFields.getMergedFieldInfos(topReader); - //The global field type should have docValues because a document with dvs was added - assertEquals(DocValuesType.SORTED, infos.fieldInfo(fieldname).getDocValuesType()); - - for(LeafReaderContext ctx: topReader.leaves()) { - LeafReader r = ctx.reader(); - //Make sure there were no merges - assertEquals(1, r.numDocs()); - Document doc = r.document(0); - String id = doc.getField("id").stringValue(); - - if(id.equals("1") || id.equals("3")) { - assertEquals(DocValuesType.NONE, r.getFieldInfos().fieldInfo(fieldname).getDocValuesType()); - } else { - assertEquals(DocValuesType.SORTED, r.getFieldInfos().fieldInfo(fieldname).getDocValuesType()); - } - - } - } finally { - searcherRef.decref(); - } - } - - // Assert sort order is correct - assertQ(req("q", "string_add_dv_later:*", "sort", "string_add_dv_later asc"), - "//*[@numFound='3']", - "//result/doc[1]/int[@name='id'][.=1]", - "//result/doc[2]/int[@name='id'][.=2]", - "//result/doc[3]/int[@name='id'][.=3]" - ); - } - private void tstToObj(SchemaField sf, Object o) { List fields = sf.createFields(o); for (IndexableField field : fields) { diff --git a/solr/core/src/test/org/apache/solr/schema/TestHalfAndHalfDocValues.java b/solr/core/src/test/org/apache/solr/schema/TestHalfAndHalfDocValues.java new file mode 100644 index 00000000000..b0c39566217 --- /dev/null +++ b/solr/core/src/test/org/apache/solr/schema/TestHalfAndHalfDocValues.java @@ -0,0 +1,136 @@ +/* + * 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.solr.schema; + +import org.apache.lucene.document.Document; +import org.apache.lucene.index.DirectoryReader; +import org.apache.lucene.index.DocValuesType; +import org.apache.lucene.index.FieldInfos; +import org.apache.lucene.index.LeafReader; +import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.index.MultiFields; +import org.apache.solr.SolrTestCaseJ4; +import org.apache.solr.core.SolrCore; +import org.apache.solr.index.NoMergePolicyFactory; +import org.apache.solr.search.SolrIndexSearcher; +import org.apache.solr.util.RefCounted; +import org.junit.BeforeClass; + +/** + * Added in SOLR-10047 + */ +public class TestHalfAndHalfDocValues extends SolrTestCaseJ4 { + + @BeforeClass + public static void beforeTests() throws Exception { + // we need consistent segments that aren't merged because we want to have + // segments with and without docvalues + systemSetPropertySolrTestsMergePolicyFactory(NoMergePolicyFactory.class.getName()); + + // HACK: Don't use a RandomMergePolicy, but only use the mergePolicyFactory that we've just set + System.setProperty(SYSTEM_PROPERTY_SOLR_TESTS_USEMERGEPOLICYFACTORY, "true"); + System.setProperty(SYSTEM_PROPERTY_SOLR_TESTS_USEMERGEPOLICY, "false"); + + initCore("solrconfig-basic.xml", "schema-docValues.xml"); + + // sanity check our schema meets our expectations + final IndexSchema schema = h.getCore().getLatestSchema(); + for (String f : new String[]{"floatdv", "intdv", "doubledv", "longdv", "datedv", "stringdv", "booldv"}) { + final SchemaField sf = schema.getField(f); + assertFalse(f + " is multiValued, test is useless, who changed the schema?", + sf.multiValued()); + assertFalse(f + " is indexed, test is useless, who changed the schema?", + sf.indexed()); + assertTrue(f + " has no docValues, test is useless, who changed the schema?", + sf.hasDocValues()); + } + } + + public void setUp() throws Exception { + super.setUp(); + assertU(delQ("*:*")); + } + + public void testHalfAndHalfDocValues() throws Exception { + // Insert two docs without docvalues + String fieldname = "string_add_dv_later"; + assertU(adoc("id", "3", fieldname, "c")); + assertU(commit()); + assertU(adoc("id", "1", fieldname, "a")); + assertU(commit()); + + + try (SolrCore core = h.getCoreInc()) { + assertFalse(core.getLatestSchema().getField(fieldname).hasDocValues()); + // Add docvalues to the field type + IndexSchema schema = core.getLatestSchema(); + SchemaField oldField = schema.getField(fieldname); + int newProperties = oldField.getProperties() | SchemaField.DOC_VALUES; + + SchemaField sf = new SchemaField(fieldname, oldField.getType(), newProperties, null); + schema.getFields().put(fieldname, sf); + + // Insert a new doc with docvalues + assertU(adoc("id", "2", fieldname, "b")); + assertU(commit()); + + + // Check there are a mix of segments with and without docvalues + final RefCounted searcherRef = core.openNewSearcher(true, true); + final SolrIndexSearcher searcher = searcherRef.get(); + try { + final DirectoryReader topReader = searcher.getRawReader(); + + //Assert no merges + + assertEquals(3, topReader.numDocs()); + assertEquals(3, topReader.leaves().size()); + + final FieldInfos infos = MultiFields.getMergedFieldInfos(topReader); + //The global field type should have docValues because a document with dvs was added + assertEquals(DocValuesType.SORTED, infos.fieldInfo(fieldname).getDocValuesType()); + + for (LeafReaderContext ctx : topReader.leaves()) { + LeafReader r = ctx.reader(); + //Make sure there were no merges + assertEquals(1, r.numDocs()); + Document doc = r.document(0); + String id = doc.getField("id").stringValue(); + + if (id.equals("1") || id.equals("3")) { + assertEquals(DocValuesType.NONE, r.getFieldInfos().fieldInfo(fieldname).getDocValuesType()); + } else { + assertEquals(DocValuesType.SORTED, r.getFieldInfos().fieldInfo(fieldname).getDocValuesType()); + } + + } + } finally { + searcherRef.decref(); + } + } + + // Assert sort order is correct + assertQ(req("q", "string_add_dv_later:*", "sort", "string_add_dv_later asc"), + "//*[@numFound='3']", + "//result/doc[1]/int[@name='id'][.=1]", + "//result/doc[2]/int[@name='id'][.=2]", + "//result/doc[3]/int[@name='id'][.=3]" + ); + } + +}