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);
}
});
}
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());
}
}
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);
}
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));
}
}
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));
}
Aggregations