Skip to content

Commit

Permalink
Add tests for includeInObject backcompat
Browse files Browse the repository at this point in the history
  • Loading branch information
rjernst committed May 13, 2015
1 parent 565ffb1 commit 1df807d
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 34 deletions.
Expand Up @@ -214,7 +214,7 @@ protected TimestampFieldMapper(FieldType fieldType, Boolean docValues, EnabledAt
super(new Names(Defaults.NAME, Defaults.NAME, Defaults.NAME, Defaults.NAME), dateTimeFormatter,
Defaults.PRECISION_STEP_64_BIT, Defaults.BOOST, fieldType, docValues,
Defaults.NULL_VALUE, TimeUnit.MILLISECONDS /*always milliseconds*/,
ignoreMalformed, coerce, null, normsLoading, fieldDataSettings,
ignoreMalformed, coerce, null, normsLoading, fieldDataSettings,
indexSettings, MultiFields.empty(), null);
this.enabledState = enabledState;
this.path = path;
Expand Down
Expand Up @@ -41,13 +41,12 @@
import org.elasticsearch.index.engine.Engine.Searcher;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.DocumentMapperParser;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.index.mapper.ParseContext.Document;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.index.mapper.internal.AllFieldMapper;
import org.elasticsearch.index.mapper.internal.IndexFieldMapper;
import org.elasticsearch.index.mapper.internal.SizeFieldMapper;
import org.elasticsearch.index.mapper.internal.SourceFieldMapper;
import org.elasticsearch.test.ElasticsearchSingleNodeTest;
import org.hamcrest.Matchers;
import org.junit.Test;
Expand All @@ -62,7 +61,6 @@
import static org.elasticsearch.common.io.Streams.copyToBytesFromClasspath;
import static org.elasticsearch.common.io.Streams.copyToStringFromClasspath;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
Expand All @@ -71,12 +69,8 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;

/**
*
*/
public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest {

@Test
public void testSimpleAllMappers() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mapping.json");
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);
Expand All @@ -95,7 +89,6 @@ public void testSimpleAllMappers() throws Exception {
assertThat(mapper.queryStringTermQuery(new Term("_all", "foobar")), Matchers.instanceOf(AllTermQuery.class));
}

@Test
public void testAllMappersNoBoost() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/noboost-mapping.json");
IndexService index = createIndex("test");
Expand All @@ -111,7 +104,6 @@ public void testAllMappersNoBoost() throws Exception {
assertThat(field.fieldType().omitNorms(), equalTo(false));
}

@Test
public void testAllMappersTermQuery() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mapping_omit_positions_on_all.json");
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);
Expand All @@ -130,7 +122,6 @@ public void testAllMappersTermQuery() throws Exception {
}

// #6187: make sure we see AllTermQuery even when offsets are indexed in the _all field:
@Test
public void testAllMappersWithOffsetsTermQuery() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mapping_offsets_on_all.json");
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);
Expand All @@ -150,7 +141,6 @@ public void testAllMappersWithOffsetsTermQuery() throws Exception {
}

// #6187: if _all doesn't index positions then we never use AllTokenStream, even if some fields have boost
@Test
public void testBoostWithOmitPositions() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mapping_boost_omit_positions_on_all.json");
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);
Expand All @@ -162,7 +152,6 @@ public void testBoostWithOmitPositions() throws Exception {
}

// #6187: if no fields were boosted, we shouldn't use AllTokenStream
@Test
public void testNoBoost() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/noboost-mapping.json");
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);
Expand All @@ -173,8 +162,6 @@ public void testNoBoost() throws Exception {
assertThat(field.tokenStream(docMapper.mappers().indexAnalyzer(), null), Matchers.not(Matchers.instanceOf(AllTokenStream.class)));
}


@Test
public void testSimpleAllMappersWithReparse() throws Exception {
DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser();
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mapping.json");
Expand All @@ -194,7 +181,6 @@ public void testSimpleAllMappersWithReparse() throws Exception {
assertThat(field.fieldType().omitNorms(), equalTo(true));
}

@Test
public void testSimpleAllMappersWithStore() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/store-mapping.json");
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);
Expand All @@ -211,7 +197,6 @@ public void testSimpleAllMappersWithStore() throws Exception {
assertThat(field.fieldType().omitNorms(), equalTo(false));
}

@Test
public void testSimpleAllMappersWithReparseWithStore() throws Exception {
DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser();
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/store-mapping.json");
Expand All @@ -233,7 +218,6 @@ public void testSimpleAllMappersWithReparseWithStore() throws Exception {
assertThat(field.fieldType().omitNorms(), equalTo(false));
}

@Test
public void testRandom() throws Exception {
boolean omitNorms = false;
boolean stored = false;
Expand Down Expand Up @@ -338,7 +322,6 @@ public void testRandom() throws Exception {

}

@Test
public void testMultiField_includeInAllSetToFalse() throws IOException {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/multifield-mapping_include_in_all_set_to_false.json");
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);
Expand All @@ -357,7 +340,6 @@ public void testMultiField_includeInAllSetToFalse() throws IOException {
assertThat(allEntries.fields(), empty());
}

@Test
public void testMultiField_defaults() throws IOException {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/multifield-mapping_default.json");
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);
Expand Down Expand Up @@ -399,7 +381,6 @@ public void testMisplacedMappingAsRoot() throws IOException {

// issue https://github.com/elasticsearch/elasticsearch/issues/5864
// test that RootObjectMapping still works
@Test
public void testRootObjectMapperPropertiesDoNotCauseException() throws IOException {
DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser();
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/type_dynamic_template_mapping.json");
Expand All @@ -413,7 +394,6 @@ public void testRootObjectMapperPropertiesDoNotCauseException() throws IOExcepti
}

// issue https://github.com/elasticsearch/elasticsearch/issues/5864
@Test
public void testRootMappersStillWorking() {
String mapping = "{";
Map<String, String> rootTypes = new HashMap<>();
Expand Down Expand Up @@ -472,4 +452,17 @@ public void testAutoBoost() throws Exception {
}
}
}

public void testIncludeInObjectBackcompat() throws Exception {
String mapping = jsonBuilder().startObject().startObject("type").endObject().endObject().string();
Settings settings = ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build();
DocumentMapper docMapper = createIndex("test", settings).mapperService().documentMapperParser().parse(mapping);
ParsedDocument doc = docMapper.parse("type", "1", XContentFactory.jsonBuilder()
.startObject().field("_all", "foo").endObject().bytes());

assertNull(doc.rootDoc().get("_all"));
AllField field = (AllField) doc.rootDoc().getField("_all");
// the backcompat behavior is actually ignoring directly specifying _all
assertFalse(field.getAllEntries().fields().iterator().hasNext());
}
}
Expand Up @@ -29,6 +29,8 @@
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.index.mapper.SourceToParse;
import org.elasticsearch.index.mapper.Uid;
import org.elasticsearch.index.mapper.internal.IdFieldMapper;
import org.elasticsearch.index.mapper.internal.UidFieldMapper;
import org.elasticsearch.test.ElasticsearchSingleNodeTest;
Expand Down Expand Up @@ -98,4 +100,19 @@ public void testIdPathBackcompat() throws Exception {

assertThat(serialized_id_mapping, equalTo(expected_id_mapping));
}

public void testIncludeInObjectBackcompat() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject().string();
Settings settings = ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build();
DocumentMapper docMapper = createIndex("test", settings).mapperService().documentMapperParser().parse(mapping);

ParsedDocument doc = docMapper.parse(SourceToParse.source(XContentFactory.jsonBuilder()
.startObject()
.field("_id", "1")
.endObject()
.bytes()).type("type"));

// _id is not indexed so we need to check _uid
assertEquals(Uid.createUid("type", "1"), doc.rootDoc().get(UidFieldMapper.NAME));
}
}
Expand Up @@ -21,7 +21,10 @@

import org.apache.lucene.index.IndexOptions;
import org.elasticsearch.Version;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
Expand All @@ -38,28 +41,23 @@

import static org.hamcrest.Matchers.*;

/**
*
*/
public class RoutingTypeMapperTests extends ElasticsearchSingleNodeTest {

@Test
public void simpleRoutingMapperTests() throws Exception {
public void testRoutingMapper() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.endObject().endObject().string();
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);

ParsedDocument doc = docMapper.parse(SourceToParse.source(XContentFactory.jsonBuilder()
.startObject()
.field("field", "value")
.endObject()
.bytes()).type("type").id("1").routing("routing_value"));
.startObject()
.field("field", "value")
.endObject()
.bytes()).type("type").id("1").routing("routing_value"));

assertThat(doc.rootDoc().get("_routing"), equalTo("routing_value"));
assertThat(doc.rootDoc().get("field"), equalTo("value"));
}

@Test
public void testFieldTypeSettingsBackcompat() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_routing")
Expand All @@ -73,7 +71,6 @@ public void testFieldTypeSettingsBackcompat() throws Exception {
assertEquals(IndexOptions.NONE, docMapper.routingFieldMapper().fieldType().indexOptions());
}

@Test
public void testFieldTypeSettingsSerializationBackcompat() throws Exception {
String enabledMapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_routing").field("store", "no").field("index", "no").endObject()
Expand All @@ -93,4 +90,34 @@ public void testFieldTypeSettingsSerializationBackcompat() throws Exception {
assertThat(routingConfiguration, hasKey("index"));
assertThat(routingConfiguration.get("index").toString(), is("no"));
}

public void testPathBackcompat() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_routing").field("path", "custom_routing").endObject()
.endObject().endObject().string();
Settings settings = ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build();
DocumentMapper docMapper = createIndex("test", settings).mapperService().documentMapperParser().parse(mapping);

XContentBuilder doc = XContentFactory.jsonBuilder().startObject().field("custom_routing", "routing_value").endObject();
MappingMetaData mappingMetaData = new MappingMetaData(docMapper);
IndexRequest request = new IndexRequest("test", "type", "1").source(doc);
request.process(MetaData.builder().build(), mappingMetaData, true, "test");

assertEquals(request.routing(), "routing_value");
}

public void testIncludeInObjectBackcompat() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject().string();
Settings settings = ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build();
DocumentMapper docMapper = createIndex("test", settings).mapperService().documentMapperParser().parse(mapping);

XContentBuilder doc = XContentFactory.jsonBuilder().startObject().field("_timestamp", 2000000).endObject();
MappingMetaData mappingMetaData = new MappingMetaData(docMapper);
IndexRequest request = new IndexRequest("test", "type", "1").source(doc);
request.process(MetaData.builder().build(), mappingMetaData, true, "test");

// _routing in a document never worked, so backcompat is ignoring the field
assertNull(request.routing());
assertNull(docMapper.parse("type", "1", doc.bytes()).rootDoc().get("_routing"));
}
}
Expand Up @@ -689,7 +689,7 @@ void assertConflict(String mapping1, String mapping2, DocumentMapperParser parse
docMapper.refreshSource();
docMapper = parser.parse(docMapper.mappingSource().string());
MergeResult mergeResult = docMapper.merge(parser.parse(mapping2).mapping(), true);
assertThat(mergeResult.buildConflicts().length, equalTo(conflict == null ? 0:1));
assertThat(mergeResult.buildConflicts().length, equalTo(conflict == null ? 0 : 1));
if (conflict != null) {
assertThat(mergeResult.buildConflicts()[0], containsString(conflict));
}
Expand Down Expand Up @@ -749,4 +749,35 @@ void assertDocValuesSerialization(String mapping) throws Exception {
assertThat(docMapper.timestampFieldMapper().hasDocValues(), equalTo(docValues));
assertAcked(client().admin().indices().prepareDelete("test_doc_values"));
}

public void testPath() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_timestamp").field("enabled", true).field("path", "custom_timestamp").endObject()
.endObject().endObject().string();
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);

XContentBuilder doc = XContentFactory.jsonBuilder().startObject().field("custom_timestamp", 1).endObject();
MappingMetaData mappingMetaData = new MappingMetaData(docMapper);
IndexRequest request = new IndexRequest("test", "type", "1").source(doc);
request.process(MetaData.builder().build(), mappingMetaData, true, "test");

assertEquals(request.timestamp(), "1");
}

public void testIncludeInObjectBackcompat() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_timestamp").field("enabled", true).field("default", "1970").field("format", "YYYY").endObject()
.endObject().endObject().string();
Settings settings = ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build();
DocumentMapper docMapper = createIndex("test", settings).mapperService().documentMapperParser().parse(mapping);

XContentBuilder doc = XContentFactory.jsonBuilder().startObject().field("_timestamp", 2000000).endObject();
MappingMetaData mappingMetaData = new MappingMetaData(docMapper);
IndexRequest request = new IndexRequest("test", "type", "1").source(doc);
request.process(MetaData.builder().build(), mappingMetaData, true, "test");

// _timestamp in a document never worked, so backcompat is ignoring the field
assertEquals(MappingMetaData.Timestamp.parseStringTimestamp("1970", Joda.forPattern("YYYY")), request.timestamp());
assertNull(docMapper.parse("type", "1", doc.bytes()).rootDoc().get("_timestamp"));
}
}
Expand Up @@ -22,7 +22,10 @@
import org.apache.lucene.index.IndexOptions;
import org.elasticsearch.Version;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.compress.CompressedString;
import org.elasticsearch.common.settings.ImmutableSettings;
Expand Down Expand Up @@ -291,6 +294,23 @@ public void testThatSimulatedMergingLeavesStateUntouched() throws Exception {

}

public void testIncludeInObjectBackcompat() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_ttl").field("enabled", true).endObject()
.endObject().endObject().string();
Settings settings = ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build();
DocumentMapper docMapper = createIndex("test", settings).mapperService().documentMapperParser().parse(mapping);

XContentBuilder doc = XContentFactory.jsonBuilder().startObject().field("_ttl", "2d").endObject();
MappingMetaData mappingMetaData = new MappingMetaData(docMapper);
IndexRequest request = new IndexRequest("test", "type", "1").source(doc);
request.process(MetaData.builder().build(), mappingMetaData, true, "test");

// _ttl in a document never worked, so backcompat is ignoring the field
assertEquals(-1, request.ttl());
assertNull(docMapper.parse("type", "1", doc.bytes()).rootDoc().get("_ttl"));
}

private org.elasticsearch.common.xcontent.XContentBuilder getMappingWithTtlEnabled() throws IOException {
return getMappingWithTtlEnabled(null);
}
Expand Down

0 comments on commit 1df807d

Please sign in to comment.