final ActionListener<AcknowledgedResponse> listener) { try { final Index[] concreteIndices = request.getConcreteIndex() == null ? indexNameExpressionResolver.concreteIndices(state, request) : new Index[] {request.getConcreteIndex()}; PutMappingClusterStateUpdateRequest updateRequest = new PutMappingClusterStateUpdateRequest() .ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout()) .indices(concreteIndices).type(request.type()) .updateAllTypes(request.updateAllTypes()) .source(request.source()); } catch (IndexNotFoundException ex) { logger.debug(() -> new ParameterizedMessage("failed to put mappings on indices [{}], type [{}]", request.indices(), request.type()), ex); throw ex;
@Override public boolean putMapping(String indexName, String type, Object mapping) { Assert.notNull(indexName, "No index defined for putMapping()"); Assert.notNull(type, "No type defined for putMapping()"); PutMappingRequest request = new PutMappingRequest(indexName).type(type); if (mapping instanceof String) { request.source(String.valueOf(mapping), XContentType.JSON); } else if (mapping instanceof Map) { request.source((Map) mapping); } else if (mapping instanceof XContentBuilder) { request.source((XContentBuilder) mapping); } try { return client.indices().putMapping(request).isAcknowledged(); } catch (IOException e) { throw new ElasticsearchException("Failed to put mapping for " + indexName, e); } }
/** * A specialized simplified mapping source method, takes the form of simple properties definition: * ("field1", "type=string,store=true"). * * Also supports metadata mapping fields such as `_all` and `_parent` as property definition, these metadata * mapping fields will automatically be put on the top level mapping object. */ public PutMappingRequest source(Object... source) { return source(buildFromSimplifiedDef(type, source)); }
@Override public String toString() { StringBuilder message = new StringBuilder(); message.append("ES put mapping request"); if (request.indices().length > 0) { message.append(String.format(" on indices '%s'", StringUtils.join(request.indices(), ","))); } String type = request.type(); if (type != null) { message.append(String.format(" on type '%s'", type)); } String source = request.source(); if (source != null) { message.append(String.format(" with source '%s'", source)); } return message.toString(); } }
@Override public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { PutMappingRequest putMappingRequest = putMappingRequest(Strings.splitStringByCommaToArray(request.param("index"))); putMappingRequest.type(request.param("type")); putMappingRequest.source(request.requiredContent(), request.getXContentType()); if (request.hasParam("update_all_types")) { DEPRECATION_LOGGER.deprecated("[update_all_types] is deprecated since indices may not have more than one type anymore"); } putMappingRequest.updateAllTypes(request.paramAsBoolean("update_all_types", false)); putMappingRequest.timeout(request.paramAsTime("timeout", putMappingRequest.timeout())); putMappingRequest.masterNodeTimeout(request.paramAsTime("master_timeout", putMappingRequest.masterNodeTimeout())); putMappingRequest.indicesOptions(IndicesOptions.fromRequest(request, putMappingRequest.indicesOptions())); return channel -> client.admin().indices().putMapping(putMappingRequest, new RestToXContentListener<>(channel)); } }
/** * The mapping source definition. */ public PutMappingRequestBuilder setSource(XContentBuilder mappingBuilder) { request.source(mappingBuilder); return this; }
static Request putMapping(PutMappingRequest putMappingRequest) throws IOException { // The concreteIndex is an internal concept, not applicable to requests made over the REST API. if (putMappingRequest.getConcreteIndex() != null) { throw new IllegalArgumentException("concreteIndex cannot be set on PutMapping requests made over the REST API"); } Request request = new Request(HttpPut.METHOD_NAME, RequestConverters.endpoint(putMappingRequest.indices(), "_mapping", putMappingRequest.type())); RequestConverters.Params parameters = new RequestConverters.Params(request); parameters.withTimeout(putMappingRequest.timeout()); parameters.withMasterTimeout(putMappingRequest.masterNodeTimeout()); request.setEntity(RequestConverters.createEntity(putMappingRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE)); return request; }
/** * The type of the mappings. */ public PutMappingRequestBuilder setType(String type) { request.type(type); return this; }
/** * Create a create mapping request against one or more indices. * * @param indices The indices to create mapping. Use {@code null} or {@code _all} to execute against all indices * @return The create mapping request * @see org.elasticsearch.client.IndicesAdminClient#putMapping(org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest) */ public static PutMappingRequest putMappingRequest(String... indices) { return new PutMappingRequest(indices); }
Index concreteIndex = pmr.getConcreteIndex(); if(concreteIndex != null && (pmr.indices() == null || pmr.indices().length == 0)) { String[] newIndices = provider.provide(new String[]{concreteIndex.getName()}, request, true); if(checkIndices(request, newIndices, true, allowEmptyIndices) == false) { ((PutMappingRequest) request).indices(newIndices); ((PutMappingRequest) request).setConcreteIndex(null); } else { String[] newIndices = provider.provide(((PutMappingRequest) request).indices(), request, true); if(checkIndices(request, newIndices, false, allowEmptyIndices) == false) { return false; ((PutMappingRequest) request).indices(newIndices);
public PutMappingRequestBuilder setIndices(String... indices) { request.indices(indices); return this; }
/** * A specialized simplified mapping source method, takes the form of simple properties definition: * ("field1", "type=string,store=true"). */ public PutIndexTemplateRequest mapping(String type, Object... source) { mapping(type, PutMappingRequest.buildFromSimplifiedDef(type, source)); return this; }
@Override protected ClusterBlockException checkBlock(PutMappingRequest request, ClusterState state) { String[] indices; if (request.getConcreteIndex() == null) { indices = indexNameExpressionResolver.concreteIndexNames(state, request); } else { indices = new String[] {request.getConcreteIndex().getName()}; } return state.blocks().indicesBlockedException(ClusterBlockLevel.METADATA_WRITE, indices); }
public PutMappingRequestBuilder<JsonInput, JsonOutput> timeout(String timeout) { request.timeout(timeout); return this; }
@Override public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) { PutMappingRequest putMappingRequest = putMappingRequest(Strings.splitStringByCommaToArray(request.param("index"))); putMappingRequest.type(request.param("type")); putMappingRequest.source(request.content().toUtf8()); putMappingRequest.updateAllTypes(request.paramAsBoolean("update_all_types", false)); putMappingRequest.timeout(request.paramAsTime("timeout", putMappingRequest.timeout())); putMappingRequest.masterNodeTimeout(request.paramAsTime("master_timeout", putMappingRequest.masterNodeTimeout())); putMappingRequest.indicesOptions(IndicesOptions.fromRequest(request, putMappingRequest.indicesOptions())); client.admin().indices().putMapping(putMappingRequest, new AcknowledgedRestListener<PutMappingResponse>(channel)); } }
/** * The mapping source definition. */ public PutMappingRequestBuilder setSource(Map mappingSource) { request.source(mappingSource); return this; }
private static Request putMapping(final PutMappingRequest putMappingRequest) { final String endpoint = "/" + putMappingRequest.indices()[0] + "/_mapping/" + putMappingRequest.type(); final ByteArrayEntity entity = new ByteArrayEntity(putMappingRequest.source().getBytes(), ContentType.APPLICATION_JSON); return new Request(HttpPut.METHOD_NAME, endpoint, Collections.emptyMap(), entity); }
static Request putMapping(PutMappingRequest putMappingRequest) throws IOException { // The concreteIndex is an internal concept, not applicable to requests made over the REST API. if (putMappingRequest.getConcreteIndex() != null) { throw new IllegalArgumentException("concreteIndex cannot be set on PutMapping requests made over the REST API"); } Request request = new Request(HttpPut.METHOD_NAME, RequestConverters.endpoint(putMappingRequest.indices(), "_mapping", putMappingRequest.type())); RequestConverters.Params parameters = new RequestConverters.Params(request); parameters.withTimeout(putMappingRequest.timeout()); parameters.withMasterTimeout(putMappingRequest.masterNodeTimeout()); request.setEntity(RequestConverters.createEntity(putMappingRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE)); return request; }
@Override public void onFailure(Exception t) { logger.debug(() -> new ParameterizedMessage("failed to put mappings on indices [{}], type [{}]", concreteIndices, request.type()), t); listener.onFailure(t); } });