Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.action.bulk;

import org.elasticsearch.Version;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.delete.DeleteRequest;
Expand All @@ -42,6 +43,7 @@
import org.elasticsearch.test.ESIntegTestCase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -53,6 +55,8 @@
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

import org.elasticsearch.script.ScriptType;
import org.elasticsearch.test.InternalSettingsPlugin;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
Expand All @@ -66,7 +70,7 @@ public class BulkWithUpdatesIT extends ESIntegTestCase {

@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
return Collections.singleton(CustomScriptPlugin.class);
return Arrays.asList(InternalSettingsPlugin.class, CustomScriptPlugin.class);
}

public static class CustomScriptPlugin extends MockScriptPlugin {
Expand Down Expand Up @@ -457,7 +461,7 @@ public void testBulkIndexingWhileInitializing() throws Exception {
*/
public void testBulkUpdateChildMissingParentRouting() throws Exception {
assertAcked(prepareCreate("test")
.setSettings("index.mapping.single_type", false)
.setSettings("index.version.created", Version.V_5_6_0.id) // allows for multiple types
.addMapping("parent", "{\"parent\":{}}", XContentType.JSON)
.addMapping("child", "{\"child\": {\"_parent\": {\"type\": \"parent\"}}}", XContentType.JSON));
ensureGreen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.elasticsearch.cluster.metadata;

import org.elasticsearch.Version;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingClusterStateUpdateRequest;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.service.ClusterService;
Expand All @@ -27,15 +28,23 @@
import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.ESSingleNodeTestCase;
import org.elasticsearch.test.InternalSettingsPlugin;

import java.util.Collection;
import java.util.Collections;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;

public class MetaDataMappingServiceTests extends ESSingleNodeTestCase {

@Override
protected Collection<Class<? extends Plugin>> getPlugins() {
return Collections.singleton(InternalSettingsPlugin.class);
}

// Tests _parent meta field logic, because part of the validation is in MetaDataMappingService
public void testAddChildTypePointingToAlreadyExistingType() throws Exception {
createIndex("test", Settings.EMPTY, "type", "field", "type=keyword");
Expand All @@ -54,7 +63,7 @@ public void testAddChildTypePointingToAlreadyExistingType() throws Exception {
// Tests _parent meta field logic, because part of the validation is in MetaDataMappingService
public void testAddExtraChildTypePointingToAlreadyParentExistingType() throws Exception {
IndexService indexService = createIndex("test", client().admin().indices().prepareCreate("test")
.setSettings("index.mapping.single_type", false)
.setSettings("index.version.created", Version.V_5_6_0.id)
.addMapping("parent")
.addMapping("child1", "_parent", "type=parent")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public void testLatestVersionLoaded() throws Exception {
// clean two nodes
internalCluster().startNodes(2, Settings.builder().put("gateway.recover_after_nodes", 2).build());

assertAcked(client().admin().indices().prepareCreate("test").setSettings("index.mapping.single_type", false));
assertAcked(client().admin().indices().prepareCreate("test"));
client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("field", "value1").endObject()).execute().actionGet();
client().admin().indices().prepareFlush().execute().actionGet();
client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject().field("field", "value2").endObject()).execute().actionGet();
Expand Down Expand Up @@ -350,10 +350,7 @@ public void testLatestVersionLoaded() throws Exception {
assertHitCount(client().prepareSearch().setSize(0).setQuery(matchAllQuery()).execute().actionGet(), 3);
}

logger.info("--> add some metadata, additional type and template");
client().admin().indices().preparePutMapping("test").setType("type2")
.setSource(jsonBuilder().startObject().startObject("type2").endObject().endObject())
.execute().actionGet();
logger.info("--> add some metadata and additional template");
client().admin().indices().preparePutTemplate("template_1")
.setTemplate("te*")
.setOrder(0)
Expand Down Expand Up @@ -381,7 +378,6 @@ public void testLatestVersionLoaded() throws Exception {
}

ClusterState state = client().admin().cluster().prepareState().execute().actionGet().getState();
assertThat(state.metaData().index("test").mapping("type2"), notNullValue());
assertThat(state.metaData().templates().get("template_1").patterns(), equalTo(Collections.singletonList("te*")));
assertThat(state.metaData().index("test").getAliases().get("test_alias"), notNullValue());
assertThat(state.metaData().index("test").getAliases().get("test_alias").filter(), notNullValue());
Expand Down
108 changes: 96 additions & 12 deletions core/src/test/java/org/elasticsearch/get/GetActionIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.get;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.Version;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.ShardOperationFailedException;
import org.elasticsearch.action.admin.indices.alias.Alias;
Expand All @@ -38,9 +39,12 @@
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.engine.VersionConflictEngineException;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.InternalSettingsPlugin;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
Expand All @@ -58,6 +62,11 @@

public class GetActionIT extends ESIntegTestCase {

@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
return Collections.singleton(InternalSettingsPlugin.class);
}

public void testSimpleGet() {
assertAcked(prepareCreate("test")
.addMapping("type1", "field1", "type=keyword,store=true", "field2", "type=keyword,store=true")
Expand Down Expand Up @@ -246,15 +255,55 @@ public void testGetDocWithMultivaluedFields() throws Exception {
.startObject("field").field("type", "text").field("store", true).endObject()
.endObject()
.endObject().endObject().string();
assertAcked(prepareCreate("test")
.addMapping("type1", mapping1, XContentType.JSON));
ensureGreen();

GetResponse response = client().prepareGet("test", "type1", "1").get();
assertThat(response.isExists(), equalTo(false));
assertThat(response.isExists(), equalTo(false));

client().prepareIndex("test", "type1", "1")
.setSource(jsonBuilder().startObject().array("field", "1", "2").endObject()).get();

response = client().prepareGet("test", "type1", "1").setStoredFields("field").get();
assertThat(response.isExists(), equalTo(true));
assertThat(response.getId(), equalTo("1"));
assertThat(response.getType(), equalTo("type1"));
Set<String> fields = new HashSet<>(response.getFields().keySet());
assertThat(fields, equalTo(singleton("field")));
assertThat(response.getFields().get("field").getValues().size(), equalTo(2));
assertThat(response.getFields().get("field").getValues().get(0).toString(), equalTo("1"));
assertThat(response.getFields().get("field").getValues().get(1).toString(), equalTo("2"));

// Now test values being fetched from stored fields.
refresh();
response = client().prepareGet("test", "type1", "1").setStoredFields("field").get();
assertThat(response.isExists(), equalTo(true));
assertThat(response.getId(), equalTo("1"));
fields = new HashSet<>(response.getFields().keySet());
assertThat(fields, equalTo(singleton("field")));
assertThat(response.getFields().get("field").getValues().size(), equalTo(2));
assertThat(response.getFields().get("field").getValues().get(0).toString(), equalTo("1"));
assertThat(response.getFields().get("field").getValues().get(1).toString(), equalTo("2"));
}

public void testGetDocWithMultivaluedFieldsMultiTypeBWC() throws Exception {
assertTrue("remove this multi type test", Version.CURRENT.before(Version.fromString("7.0.0")));
String mapping1 = XContentFactory.jsonBuilder().startObject().startObject("type1")
.startObject("properties")
.startObject("field").field("type", "text").field("store", true).endObject()
.endObject()
.endObject().endObject().string();
String mapping2 = XContentFactory.jsonBuilder().startObject().startObject("type2")
.startObject("properties")
.startObject("field").field("type", "text").field("store", true).endObject()
.endObject()
.endObject().endObject().string();
.startObject("properties")
.startObject("field").field("type", "text").field("store", true).endObject()
.endObject()
.endObject().endObject().string();
assertAcked(prepareCreate("test")
.addMapping("type1", mapping1, XContentType.JSON)
.addMapping("type2", mapping2, XContentType.JSON)
.setSettings("index.refresh_interval", -1, "index.mapping.single_type", false));
.addMapping("type1", mapping1, XContentType.JSON)
.addMapping("type2", mapping2, XContentType.JSON)
.setSettings("index.refresh_interval", -1, "index.version.created", Version.V_5_6_0.id)); // multi types in 5.6
ensureGreen();

GetResponse response = client().prepareGet("test", "type1", "1").get();
Expand All @@ -263,10 +312,10 @@ public void testGetDocWithMultivaluedFields() throws Exception {
assertThat(response.isExists(), equalTo(false));

client().prepareIndex("test", "type1", "1")
.setSource(jsonBuilder().startObject().array("field", "1", "2").endObject()).get();
.setSource(jsonBuilder().startObject().array("field", "1", "2").endObject()).get();

client().prepareIndex("test", "type2", "1")
.setSource(jsonBuilder().startObject().array("field", "1", "2").endObject()).get();
.setSource(jsonBuilder().startObject().array("field", "1", "2").endObject()).get();

response = client().prepareGet("test", "type1", "1").setStoredFields("field").get();
assertThat(response.isExists(), equalTo(true));
Expand Down Expand Up @@ -524,12 +573,47 @@ public void testMultiGetWithVersion() throws Exception {
assertThat(response.getResponses()[2].getResponse().getSourceAsMap().get("field").toString(), equalTo("value2"));
}

public void testGetFieldsMetaData() throws Exception {
public void testGetFieldsMetaDataWithRouting() throws Exception {
assertAcked(prepareCreate("test")
.addMapping("doc", "field1", "type=keyword,store=true")
.addAlias(new Alias("alias"))
.setSettings("index.refresh_interval", -1, "index.version.created", Version.V_5_6_0.id)); // multi types in 5.6

client().prepareIndex("test", "doc", "1")
.setRouting("1")
.setSource(jsonBuilder().startObject().field("field1", "value").endObject())
.get();

GetResponse getResponse = client().prepareGet(indexOrAlias(), "doc", "1")
.setRouting("1")
.setStoredFields("field1")
.get();
assertThat(getResponse.isExists(), equalTo(true));
assertThat(getResponse.getField("field1").isMetadataField(), equalTo(false));
assertThat(getResponse.getField("field1").getValue().toString(), equalTo("value"));
assertThat(getResponse.getField("_routing").isMetadataField(), equalTo(true));
assertThat(getResponse.getField("_routing").getValue().toString(), equalTo("1"));

flush();

getResponse = client().prepareGet(indexOrAlias(), "doc", "1")
.setStoredFields("field1")
.setRouting("1")
.get();
assertThat(getResponse.isExists(), equalTo(true));
assertThat(getResponse.getField("field1").isMetadataField(), equalTo(false));
assertThat(getResponse.getField("field1").getValue().toString(), equalTo("value"));
assertThat(getResponse.getField("_routing").isMetadataField(), equalTo(true));
assertThat(getResponse.getField("_routing").getValue().toString(), equalTo("1"));
}

public void testGetFieldsMetaDataWithParentChild() throws Exception {
assertTrue("remove this multi type test", Version.CURRENT.before(Version.fromString("7.0.0")));
assertAcked(prepareCreate("test")
.addMapping("parent")
.addMapping("my-type1", "_parent", "type=parent", "field1", "type=keyword,store=true")
.addAlias(new Alias("alias"))
.setSettings("index.refresh_interval", -1, "index.mapping.single_type", false));
.setSettings("index.refresh_interval", -1, "index.version.created", Version.V_5_6_0.id)); // multi types in 5.6
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we're better off testing old style parent/child metadata in its own test. That way we can remove the entire test when we no longer support it. Maybe that'd be as simple as moving routing into another test and renaming the test.


client().prepareIndex("test", "my-type1", "1")
.setRouting("1")
Expand Down Expand Up @@ -593,7 +677,7 @@ public void testGetFieldsNonLeafField() throws Exception {

public void testGetFieldsComplexField() throws Exception {
assertAcked(prepareCreate("my-index")
.setSettings("index.refresh_interval", -1, "index.mapping.single_type", false)
.setSettings("index.refresh_interval", -1, "index.version.created", Version.V_5_6_0.id) // multi types in 5.6
.addMapping("my-type2", jsonBuilder().startObject().startObject("my-type2").startObject("properties")
.startObject("field1").field("type", "object").startObject("properties")
.startObject("field2").field("type", "object").startObject("properties")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,10 @@ public void testDotsWithDynamicNestedMapper() throws Exception {

public void testNestedHaveIdAndTypeFields() throws Exception {
DocumentMapperParser mapperParser1 = createIndex("index1", Settings.builder()
.put("index.mapping.single_type", false).build()
).mapperService().documentMapperParser();
DocumentMapperParser mapperParser2 = createIndex("index2", Settings.builder()
.put("index.mapping.single_type", true).build()
.put("index.version.created", Version.V_5_6_0) // allows for multiple types
.build()
).mapperService().documentMapperParser();
DocumentMapperParser mapperParser2 = createIndex("index2").mapperService().documentMapperParser();

XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties");
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.elasticsearch.index.mapper;

import org.elasticsearch.Version;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
Expand All @@ -28,9 +29,12 @@
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.indices.TypeMissingException;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.InternalSettingsPlugin;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
Expand All @@ -42,6 +46,11 @@

public class DynamicMappingIT extends ESIntegTestCase {

@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
return Collections.singleton(InternalSettingsPlugin.class);
}

public void testConflictingDynamicMappings() {
// we don't use indexRandom because the order of requests is important here
createIndex("index");
Expand Down Expand Up @@ -75,7 +84,21 @@ private static void assertMappingsHaveField(GetMappingsResponse mappings, String
}

public void testMappingsPropagatedToMasterNodeImmediately() throws IOException {
assertAcked(prepareCreate("index").setSettings("index.mapping.single_type", false));
assertAcked(prepareCreate("index"));

// works when the type has been dynamically created
client().prepareIndex("index", "type", "1").setSource("foo", 3).get();
GetMappingsResponse mappings = client().admin().indices().prepareGetMappings("index").setTypes("type").get();
assertMappingsHaveField(mappings, "index", "type", "foo");

// works if the type already existed
client().prepareIndex("index", "type", "1").setSource("bar", "baz").get();
mappings = client().admin().indices().prepareGetMappings("index").setTypes("type").get();
assertMappingsHaveField(mappings, "index", "type", "bar");
}

public void testMappingsPropagatedToMasterNodeImmediatelyMultiType() throws IOException {
assertAcked(prepareCreate("index").setSettings("index.version.created", Version.V_5_6_0.id)); // allows for multiple types

// works when the type has been dynamically created
client().prepareIndex("index", "type", "1").setSource("foo", 3).get();
Expand Down
Loading