Search in sources :

Example 31 with MappingMetadata

use of org.elasticsearch.cluster.metadata.MappingMetadata in project elasticsearch by elastic.

the class RestGetMappingAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    final String[] types = request.paramAsStringArrayOrEmptyIfAll("type");
    GetMappingsRequest getMappingsRequest = new GetMappingsRequest();
    getMappingsRequest.indices(indices).types(types);
    getMappingsRequest.indicesOptions(IndicesOptions.fromRequest(request, getMappingsRequest.indicesOptions()));
    getMappingsRequest.local(request.paramAsBoolean("local", getMappingsRequest.local()));
    return channel -> client.admin().indices().getMappings(getMappingsRequest, new RestBuilderListener<GetMappingsResponse>(channel) {

        @Override
        public RestResponse buildResponse(GetMappingsResponse response, XContentBuilder builder) throws Exception {
            ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappingsByIndex = response.getMappings();
            if (mappingsByIndex.isEmpty()) {
                if (indices.length != 0 && types.length != 0) {
                    return new BytesRestResponse(OK, builder.startObject().endObject());
                } else if (indices.length != 0) {
                    builder.close();
                    return new BytesRestResponse(channel, new IndexNotFoundException(indices[0]));
                } else if (types.length != 0) {
                    builder.close();
                    return new BytesRestResponse(channel, new TypeMissingException("_all", types[0]));
                } else {
                    return new BytesRestResponse(OK, builder.startObject().endObject());
                }
            }
            builder.startObject();
            for (ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>> indexEntry : mappingsByIndex) {
                if (indexEntry.value.isEmpty()) {
                    continue;
                }
                builder.startObject(indexEntry.key);
                builder.startObject(Fields.MAPPINGS);
                for (ObjectObjectCursor<String, MappingMetaData> typeEntry : indexEntry.value) {
                    builder.field(typeEntry.key);
                    builder.map(typeEntry.value.sourceAsMap());
                }
                builder.endObject();
                builder.endObject();
            }
            builder.endObject();
            return new BytesRestResponse(OK, builder);
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GET(org.elasticsearch.rest.RestRequest.Method.GET) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) RestResponse(org.elasticsearch.rest.RestResponse) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse) GetMappingsRequest(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) Settings(org.elasticsearch.common.settings.Settings) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) NodeClient(org.elasticsearch.client.node.NodeClient) TypeMissingException(org.elasticsearch.indices.TypeMissingException) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) TypeMissingException(org.elasticsearch.indices.TypeMissingException) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) GetMappingsRequest(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest) IOException(java.io.IOException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) TypeMissingException(org.elasticsearch.indices.TypeMissingException) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 32 with MappingMetadata

use of org.elasticsearch.cluster.metadata.MappingMetadata in project elasticsearch by elastic.

the class GetIndexIT method assertEmptyOrOnlyDefaultMappings.

private void assertEmptyOrOnlyDefaultMappings(GetIndexResponse response, String indexName) {
    ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = response.mappings();
    assertThat(mappings, notNullValue());
    assertThat(mappings.size(), equalTo(1));
    ImmutableOpenMap<String, MappingMetaData> indexMappings = mappings.get(indexName);
    assertThat(indexMappings, notNullValue());
    assertThat(indexMappings.size(), anyOf(equalTo(0), equalTo(1)));
    if (indexMappings.size() == 1) {
        MappingMetaData mapping = indexMappings.get("_default_");
        assertThat(mapping, notNullValue());
    }
}
Also used : ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData)

Example 33 with MappingMetadata

use of org.elasticsearch.cluster.metadata.MappingMetadata in project elasticsearch by elastic.

the class IndexShardTests method testRecoverFromLocalShard.

public void testRecoverFromLocalShard() throws IOException {
    Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1).put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).build();
    IndexMetaData metaData = IndexMetaData.builder("source").putMapping("test", "{ \"properties\": { \"foo\":  { \"type\": \"text\"}}}").settings(settings).primaryTerm(0, 1).build();
    IndexShard sourceShard = newShard(new ShardId(metaData.getIndex(), 0), true, "n1", metaData, null);
    recoveryShardFromStore(sourceShard);
    indexDoc(sourceShard, "test", "0", "{\"foo\" : \"bar\"}");
    indexDoc(sourceShard, "test", "1", "{\"foo\" : \"bar\"}");
    sourceShard.refresh("test");
    ShardRouting targetRouting = TestShardRouting.newShardRouting(new ShardId("index_1", "index_1", 0), "n1", true, ShardRoutingState.INITIALIZING, RecoverySource.LocalShardsRecoverySource.INSTANCE);
    final IndexShard targetShard;
    DiscoveryNode localNode = new DiscoveryNode("foo", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
    Map<String, MappingMetaData> requestedMappingUpdates = ConcurrentCollections.newConcurrentMap();
    {
        targetShard = newShard(targetRouting);
        targetShard.markAsRecovering("store", new RecoveryState(targetShard.routingEntry(), localNode, null));
        BiConsumer<String, MappingMetaData> mappingConsumer = (type, mapping) -> {
            assertNull(requestedMappingUpdates.put(type, mapping));
        };
        final IndexShard differentIndex = newShard(new ShardId("index_2", "index_2", 0), true);
        recoveryShardFromStore(differentIndex);
        expectThrows(IllegalArgumentException.class, () -> {
            targetShard.recoverFromLocalShards(mappingConsumer, Arrays.asList(sourceShard, differentIndex));
        });
        closeShards(differentIndex);
        assertTrue(targetShard.recoverFromLocalShards(mappingConsumer, Arrays.asList(sourceShard)));
        RecoveryState recoveryState = targetShard.recoveryState();
        assertEquals(RecoveryState.Stage.DONE, recoveryState.getStage());
        assertTrue(recoveryState.getIndex().fileDetails().size() > 0);
        for (RecoveryState.File file : recoveryState.getIndex().fileDetails()) {
            if (file.reused()) {
                assertEquals(file.recovered(), 0);
            } else {
                assertEquals(file.recovered(), file.length());
            }
        }
        targetShard.updateRoutingEntry(ShardRoutingHelper.moveToStarted(targetShard.routingEntry()));
        assertDocCount(targetShard, 2);
    }
    // now check that it's persistent ie. that the added shards are committed
    {
        final IndexShard newShard = reinitShard(targetShard);
        recoveryShardFromStore(newShard);
        assertDocCount(newShard, 2);
        closeShards(newShard);
    }
    assertThat(requestedMappingUpdates, hasKey("test"));
    assertThat(requestedMappingUpdates.get("test").get().source().string(), equalTo("{\"properties\":{\"foo\":{\"type\":\"text\"}}}"));
    closeShards(sourceShard, targetShard);
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Matchers.containsString(org.hamcrest.Matchers.containsString) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) RecoveryState(org.elasticsearch.indices.recovery.RecoveryState) Settings(org.elasticsearch.common.settings.Settings) BiConsumer(java.util.function.BiConsumer)

Example 34 with MappingMetadata

use of org.elasticsearch.cluster.metadata.MappingMetadata in project elasticsearch by elastic.

the class SimpleClusterStateIT method testLargeClusterStatePublishing.

public void testLargeClusterStatePublishing() throws Exception {
    int estimatedBytesSize = scaledRandomIntBetween(ByteSizeValue.parseBytesSizeValue("10k", "estimatedBytesSize").bytesAsInt(), ByteSizeValue.parseBytesSizeValue("256k", "estimatedBytesSize").bytesAsInt());
    XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties");
    int counter = 0;
    int numberOfFields = 0;
    while (true) {
        mapping.startObject(UUIDs.randomBase64UUID()).field("type", "text").endObject();
        // each field is about 10 bytes, assuming compression in place
        counter += 10;
        numberOfFields++;
        if (counter > estimatedBytesSize) {
            break;
        }
    }
    logger.info("number of fields [{}], estimated bytes [{}]", numberOfFields, estimatedBytesSize);
    mapping.endObject().endObject().endObject();
    int numberOfShards = scaledRandomIntBetween(1, cluster().numDataNodes());
    // if the create index is ack'ed, then all nodes have successfully processed the cluster state
    assertAcked(client().admin().indices().prepareCreate("test").setSettings(IndexMetaData.SETTING_NUMBER_OF_SHARDS, numberOfShards, IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0, MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING.getKey(), Long.MAX_VALUE).addMapping("type", mapping).setTimeout("60s").get());
    // wait for green state, so its both green, and there are no more pending events
    ensureGreen();
    MappingMetaData masterMappingMetaData = client().admin().indices().prepareGetMappings("test").setTypes("type").get().getMappings().get("test").get("type");
    for (Client client : clients()) {
        MappingMetaData mappingMetadata = client.admin().indices().prepareGetMappings("test").setTypes("type").setLocal(true).get().getMappings().get("test").get("type");
        assertThat(mappingMetadata.source().string(), equalTo(masterMappingMetaData.source().string()));
        assertThat(mappingMetadata, equalTo(masterMappingMetaData));
    }
}
Also used : MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) Client(org.elasticsearch.client.Client) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 35 with MappingMetadata

use of org.elasticsearch.cluster.metadata.MappingMetadata in project elasticsearch by elastic.

the class MultiFieldsIntegrationIT method testGeoPointMultiField.

public void testGeoPointMultiField() throws Exception {
    assertAcked(client().admin().indices().prepareCreate("my-index").addMapping("my-type", createMappingSource("geo_point")));
    GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("my-index").get();
    MappingMetaData mappingMetaData = getMappingsResponse.mappings().get("my-index").get("my-type");
    assertThat(mappingMetaData, not(nullValue()));
    Map<String, Object> mappingSource = mappingMetaData.sourceAsMap();
    Map aField = ((Map) XContentMapValues.extractValue("properties.a", mappingSource));
    logger.info("Keys: {}", aField.keySet());
    assertThat(aField.size(), equalTo(2));
    assertThat(aField.get("type").toString(), equalTo("geo_point"));
    assertThat(aField.get("fields"), notNullValue());
    Map bField = ((Map) XContentMapValues.extractValue("properties.a.fields.b", mappingSource));
    assertThat(bField.size(), equalTo(1));
    assertThat(bField.get("type").toString(), equalTo("keyword"));
    GeoPoint point = new GeoPoint(51, 19);
    client().prepareIndex("my-index", "my-type", "1").setSource("a", point.toString()).setRefreshPolicy(IMMEDIATE).get();
    SearchResponse countResponse = client().prepareSearch("my-index").setSize(0).setQuery(constantScoreQuery(geoDistanceQuery("a").point(51, 19).distance(50, DistanceUnit.KILOMETERS))).get();
    assertThat(countResponse.getHits().getTotalHits(), equalTo(1L));
    countResponse = client().prepareSearch("my-index").setSize(0).setQuery(matchQuery("a.b", point.geohash())).get();
    assertThat(countResponse.getHits().getTotalHits(), equalTo(1L));
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) Map(java.util.Map) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

MappingMetaData (org.elasticsearch.cluster.metadata.MappingMetaData)46 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)16 Map (java.util.Map)15 GetMappingsResponse (org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)11 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)10 Settings (org.elasticsearch.common.settings.Settings)9 Test (org.junit.Test)9 IOException (java.io.IOException)8 HashMap (java.util.HashMap)7 IndexTemplateMetaData (org.elasticsearch.cluster.metadata.IndexTemplateMetaData)7 PartitionName (io.crate.metadata.PartitionName)6 BytesRef (org.apache.lucene.util.BytesRef)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)5 Set (java.util.Set)4 ClusterState (org.elasticsearch.cluster.ClusterState)4 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)4 ObjectObjectCursor (com.carrotsearch.hppc.cursors.ObjectObjectCursor)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 GetIndexTemplatesResponse (org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse)3