Search in sources :

Example 6 with GetMappingsResponse

use of org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse in project elasticsearch by elastic.

the class UpdateMappingOnClusterIT method compareMappingOnNodes.

private void compareMappingOnNodes(GetMappingsResponse previousMapping) {
    // make sure all nodes have same cluster state
    for (Client client : cluster().getClients()) {
        GetMappingsResponse currentMapping = client.admin().indices().prepareGetMappings(INDEX).addTypes(TYPE).setLocal(true).get();
        assertThat(previousMapping.getMappings().get(INDEX).get(TYPE).source(), equalTo(currentMapping.getMappings().get(INDEX).get(TYPE).source()));
    }
}
Also used : Client(org.elasticsearch.client.Client) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 7 with GetMappingsResponse

use of org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse in project elasticsearch by elastic.

the class UpdateMappingIntegrationIT method testUpdateMappingWithoutType.

public void testUpdateMappingWithoutType() throws Exception {
    client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0)).addMapping("doc", "{\"doc\":{\"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("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\":{\"body\":{\"type\":\"text\"},\"date\":{\"type\":\"integer\"}}}}"));
}
Also used : PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 8 with GetMappingsResponse

use of org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse in project elasticsearch by elastic.

the class UpdateMappingIntegrationIT method testUpdateDefaultMappingSettings.

@SuppressWarnings("unchecked")
public void testUpdateDefaultMappingSettings() throws Exception {
    logger.info("Creating index with _default_ mappings");
    client().admin().indices().prepareCreate("test").addMapping(MapperService.DEFAULT_MAPPING, JsonXContent.contentBuilder().startObject().startObject(MapperService.DEFAULT_MAPPING).field("date_detection", false).endObject().endObject()).get();
    GetMappingsResponse getResponse = client().admin().indices().prepareGetMappings("test").addTypes(MapperService.DEFAULT_MAPPING).get();
    Map<String, Object> defaultMapping = getResponse.getMappings().get("test").get(MapperService.DEFAULT_MAPPING).sourceAsMap();
    assertThat(defaultMapping, hasKey("date_detection"));
    logger.info("Emptying _default_ mappings");
    // now remove it
    PutMappingResponse putResponse = client().admin().indices().preparePutMapping("test").setType(MapperService.DEFAULT_MAPPING).setSource(JsonXContent.contentBuilder().startObject().startObject(MapperService.DEFAULT_MAPPING).endObject().endObject()).get();
    assertThat(putResponse.isAcknowledged(), equalTo(true));
    logger.info("Done Emptying _default_ mappings");
    getResponse = client().admin().indices().prepareGetMappings("test").addTypes(MapperService.DEFAULT_MAPPING).get();
    defaultMapping = getResponse.getMappings().get("test").get(MapperService.DEFAULT_MAPPING).sourceAsMap();
    assertThat(defaultMapping, not(hasKey("date_detection")));
    // now test you can change stuff that are normally unchangeable
    logger.info("Creating _default_ mappings with an analyzed field");
    putResponse = client().admin().indices().preparePutMapping("test").setType(MapperService.DEFAULT_MAPPING).setSource(JsonXContent.contentBuilder().startObject().startObject(MapperService.DEFAULT_MAPPING).startObject("properties").startObject("f").field("type", "text").field("index", true).endObject().endObject().endObject().endObject()).get();
    assertThat(putResponse.isAcknowledged(), equalTo(true));
    logger.info("Changing _default_ mappings field from analyzed to non-analyzed");
    putResponse = client().admin().indices().preparePutMapping("test").setType(MapperService.DEFAULT_MAPPING).setSource(JsonXContent.contentBuilder().startObject().startObject(MapperService.DEFAULT_MAPPING).startObject("properties").startObject("f").field("type", "keyword").endObject().endObject().endObject().endObject()).get();
    assertThat(putResponse.isAcknowledged(), equalTo(true));
    logger.info("Done changing _default_ mappings field from analyzed to non-analyzed");
    getResponse = client().admin().indices().prepareGetMappings("test").addTypes(MapperService.DEFAULT_MAPPING).get();
    defaultMapping = getResponse.getMappings().get("test").get(MapperService.DEFAULT_MAPPING).sourceAsMap();
    Map<String, Object> fieldSettings = (Map<String, Object>) ((Map) defaultMapping.get("properties")).get("f");
    assertThat(fieldSettings, hasEntry("type", (Object) "keyword"));
    // but we still validate the _default_ type
    logger.info("Confirming _default_ mappings validation");
    assertThrows(client().admin().indices().preparePutMapping("test").setType(MapperService.DEFAULT_MAPPING).setSource(JsonXContent.contentBuilder().startObject().startObject(MapperService.DEFAULT_MAPPING).startObject("properties").startObject("f").field("type", "DOESNT_EXIST").endObject().endObject().endObject().endObject()), MapperParsingException.class);
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) Map(java.util.Map) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 9 with GetMappingsResponse

use of org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse in project elasticsearch by elastic.

the class UpdateMappingIntegrationIT method testUpdateMappingOnAllTypes.

public void testUpdateMappingOnAllTypes() throws IOException {
    assertAcked(prepareCreate("index").addMapping("type1", "f", "type=keyword").addMapping("type2", "f", "type=keyword"));
    assertAcked(client().admin().indices().preparePutMapping("index").setType("type1").setUpdateAllTypes(true).setSource("f", "type=keyword,null_value=n/a").get());
    GetMappingsResponse mappings = client().admin().indices().prepareGetMappings("index").setTypes("type2").get();
    MappingMetaData type2Mapping = mappings.getMappings().get("index").get("type2").get();
    Map<String, Object> properties = (Map<String, Object>) type2Mapping.sourceAsMap().get("properties");
    Map<String, Object> f = (Map<String, Object>) properties.get("f");
    assertEquals("n/a", f.get("null_value"));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) Map(java.util.Map) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 10 with GetMappingsResponse

use of org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse in project elasticsearch by elastic.

the class UpdateMappingIntegrationIT method testUpdateMappingConcurrently.

public void testUpdateMappingConcurrently() throws Throwable {
    createIndex("test1", "test2");
    final AtomicReference<Exception> threadException = new AtomicReference<>();
    final AtomicBoolean stop = new AtomicBoolean(false);
    Thread[] threads = new Thread[3];
    final CyclicBarrier barrier = new CyclicBarrier(threads.length);
    final ArrayList<Client> clientArray = new ArrayList<>();
    for (Client c : clients()) {
        clientArray.add(c);
    }
    for (int j = 0; j < threads.length; j++) {
        threads[j] = new Thread(new Runnable() {

            @SuppressWarnings("unchecked")
            @Override
            public void run() {
                try {
                    barrier.await();
                    for (int i = 0; i < 100; i++) {
                        if (stop.get()) {
                            return;
                        }
                        Client client1 = clientArray.get(i % clientArray.size());
                        Client client2 = clientArray.get((i + 1) % clientArray.size());
                        String indexName = i % 2 == 0 ? "test2" : "test1";
                        String typeName = "type" + (i % 10);
                        String fieldName = Thread.currentThread().getName() + "_" + i;
                        PutMappingResponse response = client1.admin().indices().preparePutMapping(indexName).setType(typeName).setSource(JsonXContent.contentBuilder().startObject().startObject(typeName).startObject("properties").startObject(fieldName).field("type", "text").endObject().endObject().endObject().endObject()).get();
                        assertThat(response.isAcknowledged(), equalTo(true));
                        GetMappingsResponse getMappingResponse = client2.admin().indices().prepareGetMappings(indexName).get();
                        ImmutableOpenMap<String, MappingMetaData> mappings = getMappingResponse.getMappings().get(indexName);
                        assertThat(mappings.containsKey(typeName), equalTo(true));
                        assertThat(((Map<String, Object>) mappings.get(typeName).getSourceAsMap().get("properties")).keySet(), Matchers.hasItem(fieldName));
                    }
                } catch (Exception e) {
                    threadException.set(e);
                    stop.set(true);
                }
            }
        });
        threads[j].setName("t_" + j);
        threads[j].start();
    }
    for (Thread t : threads) t.join();
    if (threadException.get() != null) {
        throw threadException.get();
    }
}
Also used : ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) IOException(java.io.IOException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) Client(org.elasticsearch.client.Client) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Aggregations

GetMappingsResponse (org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)26 MappingMetaData (org.elasticsearch.cluster.metadata.MappingMetaData)11 Map (java.util.Map)8 PutMappingResponse (org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 IOException (java.io.IOException)4 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Client (org.elasticsearch.client.Client)2 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)2 TypeMissingException (org.elasticsearch.indices.TypeMissingException)2 ObjectObjectCursor (com.carrotsearch.hppc.cursors.ObjectObjectCursor)1 ReadContext (com.jayway.jsonpath.ReadContext)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1