Search in sources :

Example 16 with PutMappingResponse

use of org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse in project elasticsearch by elastic.

the class RareClusterStateIT method testDelayedMappingPropagationOnReplica.

public void testDelayedMappingPropagationOnReplica() throws Exception {
    // This is essentially the same thing as testDelayedMappingPropagationOnPrimary
    // but for replicas
    // Here we want to test that everything goes well if the mappings that
    // are needed for a document are not available on the replica at the
    // time of indexing it
    final List<String> nodeNames = internalCluster().startNodes(2, Settings.builder().put(DiscoverySettings.COMMIT_TIMEOUT_SETTING.getKey(), // explicitly set so it won't default to publish timeout
    "30s").put(DiscoverySettings.PUBLISH_TIMEOUT_SETTING.getKey(), // don't wait post commit as we are blocking things by design
    "0s").build());
    assertFalse(client().admin().cluster().prepareHealth().setWaitForNodes("2").get().isTimedOut());
    final String master = internalCluster().getMasterName();
    assertThat(nodeNames, hasItem(master));
    String otherNode = null;
    for (String node : nodeNames) {
        if (node.equals(master) == false) {
            otherNode = node;
            break;
        }
    }
    assertNotNull(otherNode);
    // Force allocation of the primary on the master node by first only allocating on the master
    // and then allowing all nodes so that the replica gets allocated on the other node
    assertAcked(prepareCreate("index").setSettings(Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1).put("index.routing.allocation.include._name", master)).get());
    assertAcked(client().admin().indices().prepareUpdateSettings("index").setSettings(Settings.builder().put("index.routing.allocation.include._name", "")).get());
    ensureGreen();
    // Check routing tables
    ClusterState state = client().admin().cluster().prepareState().get().getState();
    assertEquals(master, state.nodes().getMasterNode().getName());
    List<ShardRouting> shards = state.routingTable().allShards("index");
    assertThat(shards, hasSize(2));
    for (ShardRouting shard : shards) {
        if (shard.primary()) {
            // primary must be on the master
            assertEquals(state.nodes().getMasterNodeId(), shard.currentNodeId());
        } else {
            assertTrue(shard.active());
        }
    }
    // Block cluster state processing on the replica
    BlockClusterStateProcessing disruption = new BlockClusterStateProcessing(otherNode, random());
    internalCluster().setDisruptionScheme(disruption);
    disruption.startDisrupting();
    final AtomicReference<Object> putMappingResponse = new AtomicReference<>();
    client().admin().indices().preparePutMapping("index").setType("type").setSource("field", "type=long").execute(new ActionListener<PutMappingResponse>() {

        @Override
        public void onResponse(PutMappingResponse response) {
            putMappingResponse.set(response);
        }

        @Override
        public void onFailure(Exception e) {
            putMappingResponse.set(e);
        }
    });
    final Index index = resolveIndex("index");
    // Wait for mappings to be available on master
    assertBusy(new Runnable() {

        @Override
        public void run() {
            final IndicesService indicesService = internalCluster().getInstance(IndicesService.class, master);
            final IndexService indexService = indicesService.indexServiceSafe(index);
            assertNotNull(indexService);
            final MapperService mapperService = indexService.mapperService();
            DocumentMapper mapper = mapperService.documentMapper("type");
            assertNotNull(mapper);
            assertNotNull(mapper.mappers().getMapper("field"));
        }
    });
    final AtomicReference<Object> docIndexResponse = new AtomicReference<>();
    client().prepareIndex("index", "type", "1").setSource("field", 42).execute(new ActionListener<IndexResponse>() {

        @Override
        public void onResponse(IndexResponse response) {
            docIndexResponse.set(response);
        }

        @Override
        public void onFailure(Exception e) {
            docIndexResponse.set(e);
        }
    });
    // Wait for document to be indexed on primary
    assertBusy(new Runnable() {

        @Override
        public void run() {
            assertTrue(client().prepareGet("index", "type", "1").setPreference("_primary").get().isExists());
        }
    });
    // The mappings have not been propagated to the replica yet as a consequence the document count not be indexed
    // We wait on purpose to make sure that the document is not indexed because the shard operation is stalled
    // and not just because it takes time to replicate the indexing request to the replica
    Thread.sleep(100);
    assertThat(putMappingResponse.get(), equalTo(null));
    assertThat(docIndexResponse.get(), equalTo(null));
    // Now make sure the indexing request finishes successfully
    disruption.stopDisrupting();
    assertBusy(new Runnable() {

        @Override
        public void run() {
            assertThat(putMappingResponse.get(), instanceOf(PutMappingResponse.class));
            PutMappingResponse resp = (PutMappingResponse) putMappingResponse.get();
            assertTrue(resp.isAcknowledged());
            assertThat(docIndexResponse.get(), instanceOf(IndexResponse.class));
            IndexResponse docResp = (IndexResponse) docIndexResponse.get();
            assertEquals(Arrays.toString(docResp.getShardInfo().getFailures()), 2, // both shards should have succeeded
            docResp.getShardInfo().getTotal());
        }
    });
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) BlockClusterStateProcessing(org.elasticsearch.test.disruption.BlockClusterStateProcessing) IndexService(org.elasticsearch.index.IndexService) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) IndicesService(org.elasticsearch.indices.IndicesService) AtomicReference(java.util.concurrent.atomic.AtomicReference) Index(org.elasticsearch.index.Index) IOException(java.io.IOException) IndexResponse(org.elasticsearch.action.index.IndexResponse) PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) MapperService(org.elasticsearch.index.mapper.MapperService)

Example 17 with PutMappingResponse

use of org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse in project elasticsearch by elastic.

the class UpdateMappingIntegrationIT method testUpdateMappingNoChanges.

/*
    Second regression test for https://github.com/elastic/elasticsearch/issues/3381
     */
public void testUpdateMappingNoChanges() throws Exception {
    client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put("index.number_of_shards", 2).put("index.number_of_replicas", 0)).addMapping("type", "{\"type\":{\"properties\":{\"body\":{\"type\":\"text\"}}}}", XContentType.JSON).execute().actionGet();
    client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
    PutMappingResponse putMappingResponse = client().admin().indices().preparePutMapping("test").setType("type").setSource("{\"type\":{\"properties\":{\"body\":{\"type\":\"text\"}}}}", XContentType.JSON).execute().actionGet();
    //no changes, we return
    assertThat(putMappingResponse.isAcknowledged(), equalTo(true));
}
Also used : PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse)

Example 18 with PutMappingResponse

use of org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse in project elasticsearch by elastic.

the class UpdateMappingIntegrationIT method testUpdateMappingWithoutTypeMultiObjects.

public void testUpdateMappingWithoutTypeMultiObjects() throws Exception {
    client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0)).execute().actionGet();
    client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
    PutMappingResponse putMappingResponse = client().admin().indices().preparePutMapping("test").setType("doc").setSource("{\"properties\":{\"date\":{\"type\":\"integer\"}}}", XContentType.JSON).execute().actionGet();
    assertThat(putMappingResponse.isAcknowledged(), equalTo(true));
    GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("test").execute().actionGet();
    assertThat(getMappingsResponse.mappings().get("test").get("doc").source().toString(), equalTo("{\"doc\":{\"properties\":{\"date\":{\"type\":\"integer\"}}}}"));
}
Also used : PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 19 with PutMappingResponse

use of org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse in project elasticsearch by elastic.

the class ChildQuerySearchIT method testAddingParentToExistingMapping.

public void testAddingParentToExistingMapping() throws IOException {
    createIndex("test");
    ensureGreen();
    PutMappingResponse putMappingResponse = client().admin().indices().preparePutMapping("test").setType("child").setSource("number", "type=integer").get();
    assertThat(putMappingResponse.isAcknowledged(), equalTo(true));
    GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("test").get();
    Map<String, Object> mapping = getMappingsResponse.getMappings().get("test").get("child").getSourceAsMap();
    // there are potentially some meta fields configured randomly
    assertThat(mapping.size(), greaterThanOrEqualTo(1));
    assertThat(mapping.get("properties"), notNullValue());
    try {
        // Adding _parent metadata field to existing mapping is prohibited:
        client().admin().indices().preparePutMapping("test").setType("child").setSource(jsonBuilder().startObject().startObject("child").startObject("_parent").field("type", "parent").endObject().endObject().endObject()).get();
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e.toString(), containsString("The _parent field's type option can't be changed: [null]->[parent]"));
    }
}
Also used : PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 20 with PutMappingResponse

use of org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse in project elasticsearch-opennlp-plugin by spinscale.

the class OpenNlpPluginIntegrationTest method putMapping.

private void putMapping(String mappingFile) throws IOException {
    String mapping = copyToStringFromClasspath(mappingFile);
    PutMappingRequestBuilder putMappingRequestBuilder = new PutMappingRequestBuilder(node.client().admin().indices());
    PutMappingResponse putMappingResponse = putMappingRequestBuilder.setIndices(index).setType(type).setSource(mapping).execute().actionGet();
    assertThat(putMappingResponse.isAcknowledged(), is(true));
}
Also used : PutMappingRequestBuilder(org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequestBuilder) PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse)

Aggregations

PutMappingResponse (org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse)22 IOException (java.io.IOException)7 GetMappingsResponse (org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)6 ClusterState (org.elasticsearch.cluster.ClusterState)4 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 MappingMetaData (org.elasticsearch.cluster.metadata.MappingMetaData)3 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)3 Map (java.util.Map)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)2 IndexResponse (org.elasticsearch.action.index.IndexResponse)2 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)2 Settings (org.elasticsearch.common.settings.Settings)2 Index (org.elasticsearch.index.Index)2 IndexService (org.elasticsearch.index.IndexService)2 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)2 MapperService (org.elasticsearch.index.mapper.MapperService)2 IndicesService (org.elasticsearch.indices.IndicesService)2