![JAR search and dependency download from the Maven repository](/logo.png)
org.opensearch.action.admin.indices.create.CreateIndexRequest Maven / Gradle / Ivy
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
package org.opensearch.action.admin.indices.create;
import org.opensearch.LegacyESVersion;
import org.opensearch.OpenSearchGenerationException;
import org.opensearch.OpenSearchParseException;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.action.IndicesRequest;
import org.opensearch.action.admin.indices.alias.Alias;
import org.opensearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.opensearch.action.support.ActiveShardCount;
import org.opensearch.action.support.IndicesOptions;
import org.opensearch.action.support.master.AcknowledgedRequest;
import org.opensearch.common.ParseField;
import org.opensearch.common.Strings;
import org.opensearch.common.bytes.BytesArray;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.collect.MapBuilder;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.DeprecationHandler;
import org.opensearch.common.xcontent.LoggingDeprecationHandler;
import org.opensearch.common.xcontent.NamedXContentRegistry;
import org.opensearch.common.xcontent.ToXContentObject;
import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.XContentParser;
import org.opensearch.common.xcontent.XContentType;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import static org.opensearch.action.ValidateActions.addValidationError;
import static org.opensearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
import static org.opensearch.common.settings.Settings.readSettingsFromStream;
import static org.opensearch.common.settings.Settings.writeSettingsToStream;
/**
* A request to create an index. Best created with {@link org.opensearch.client.Requests#createIndexRequest(String)}.
*
* The index created can optionally be created with {@link #settings(org.opensearch.common.settings.Settings)}.
*
* @see org.opensearch.client.IndicesAdminClient#create(CreateIndexRequest)
* @see org.opensearch.client.Requests#createIndexRequest(String)
* @see CreateIndexResponse
*/
public class CreateIndexRequest extends AcknowledgedRequest implements IndicesRequest, ToXContentObject {
public static final ParseField MAPPINGS = new ParseField("mappings");
public static final ParseField SETTINGS = new ParseField("settings");
public static final ParseField ALIASES = new ParseField("aliases");
private String cause = "";
private String index;
private Settings settings = EMPTY_SETTINGS;
private final Map mappings = new HashMap<>();
private final Set aliases = new HashSet<>();
private ActiveShardCount waitForActiveShards = ActiveShardCount.DEFAULT;
public CreateIndexRequest(StreamInput in) throws IOException {
super(in);
cause = in.readString();
index = in.readString();
settings = readSettingsFromStream(in);
int size = in.readVInt();
for (int i = 0; i < size; i++) {
final String type = in.readString();
String source = in.readString();
if (in.getVersion().before(LegacyESVersion.V_6_0_0_alpha1)) { // TODO change to 5.3.0 after backport
// we do not know the content type that comes from earlier versions so we autodetect and convert
source = XContentHelper.convertToJson(new BytesArray(source), false, false, XContentFactory.xContentType(source));
}
mappings.put(type, source);
}
if (in.getVersion().before(LegacyESVersion.V_6_5_0)) {
// This used to be the size of custom metadata classes
int customSize = in.readVInt();
assert customSize == 0 : "unexpected custom metadata when none is supported";
if (customSize > 0) {
throw new IllegalStateException("unexpected custom metadata when none is supported");
}
}
int aliasesSize = in.readVInt();
for (int i = 0; i < aliasesSize; i++) {
aliases.add(new Alias(in));
}
if (in.getVersion().before(LegacyESVersion.V_7_0_0)) {
in.readBoolean(); // updateAllTypes
}
waitForActiveShards = ActiveShardCount.readFrom(in);
}
public CreateIndexRequest() {}
/**
* Constructs a new request to create an index with the specified name.
*/
public CreateIndexRequest(String index) {
this(index, EMPTY_SETTINGS);
}
/**
* Constructs a new request to create an index with the specified name and settings.
*/
public CreateIndexRequest(String index, Settings settings) {
this.index = index;
this.settings = settings;
}
@Override
public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = null;
if (index == null) {
validationException = addValidationError("index is missing", validationException);
}
return validationException;
}
@Override
public String[] indices() {
return new String[] { index };
}
@Override
public IndicesOptions indicesOptions() {
return IndicesOptions.strictSingleIndexNoExpandForbidClosed();
}
/**
* The index name to create.
*/
public String index() {
return index;
}
public CreateIndexRequest index(String index) {
this.index = index;
return this;
}
/**
* The settings to create the index with.
*/
public Settings settings() {
return settings;
}
/**
* The cause for this index creation.
*/
public String cause() {
return cause;
}
/**
* The settings to create the index with.
*/
public CreateIndexRequest settings(Settings.Builder settings) {
this.settings = settings.build();
return this;
}
/**
* The settings to create the index with.
*/
public CreateIndexRequest settings(Settings settings) {
this.settings = settings;
return this;
}
/**
* The settings to create the index with (either json or yaml format)
*/
public CreateIndexRequest settings(String source, XContentType xContentType) {
this.settings = Settings.builder().loadFromSource(source, xContentType).build();
return this;
}
/**
* Allows to set the settings using a json builder.
*/
public CreateIndexRequest settings(XContentBuilder builder) {
settings(Strings.toString(builder), builder.contentType());
return this;
}
/**
* The settings to create the index with (either json/yaml/properties format)
*/
public CreateIndexRequest settings(Map source) {
this.settings = Settings.builder().loadFromMap(source).build();
return this;
}
/**
* Adds mapping that will be added when the index gets created.
*
* @param type The mapping type
* @param source The mapping source
* @param xContentType The content type of the source
*/
public CreateIndexRequest mapping(String type, String source, XContentType xContentType) {
return mapping(type, new BytesArray(source), xContentType);
}
/**
* Adds mapping that will be added when the index gets created.
*
* @param type The mapping type
* @param source The mapping source
* @param xContentType the content type of the mapping source
*/
private CreateIndexRequest mapping(String type, BytesReference source, XContentType xContentType) {
Objects.requireNonNull(xContentType);
Map mappingAsMap = XContentHelper.convertToMap(source, false, xContentType).v2();
return mapping(type, mappingAsMap);
}
/**
* The cause for this index creation.
*/
public CreateIndexRequest cause(String cause) {
this.cause = cause;
return this;
}
/**
* Adds mapping that will be added when the index gets created.
*
* @param type The mapping type
* @param source The mapping source
*/
public CreateIndexRequest mapping(String type, XContentBuilder source) {
return mapping(type, BytesReference.bytes(source), source.contentType());
}
/**
* Adds mapping that will be added when the index gets created.
*
* @param type The mapping type
* @param source The mapping source
*/
public CreateIndexRequest mapping(String type, Map source) {
if (mappings.containsKey(type)) {
throw new IllegalStateException("mappings for type \"" + type + "\" were already defined");
}
// wrap it in a type map if its not
if (source.size() != 1 || !source.containsKey(type)) {
source = MapBuilder.newMapBuilder().put(type, source).map();
}
try {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.map(source);
mappings.put(type, Strings.toString(builder));
return this;
} catch (IOException e) {
throw new OpenSearchGenerationException("Failed to generate [" + source + "]", e);
}
}
/**
* A specialized simplified mapping source method, takes the form of simple properties definition:
* ("field1", "type=string,store=true").
*/
public CreateIndexRequest mapping(String type, Object... source) {
mapping(type, PutMappingRequest.buildFromSimplifiedDef(type, source));
return this;
}
/**
* Sets the aliases that will be associated with the index when it gets created
*/
public CreateIndexRequest aliases(Map source) {
try {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.map(source);
return aliases(BytesReference.bytes(builder));
} catch (IOException e) {
throw new OpenSearchGenerationException("Failed to generate [" + source + "]", e);
}
}
/**
* Sets the aliases that will be associated with the index when it gets created
*/
public CreateIndexRequest aliases(XContentBuilder source) {
return aliases(BytesReference.bytes(source));
}
/**
* Sets the aliases that will be associated with the index when it gets created
*/
public CreateIndexRequest aliases(String source) {
return aliases(new BytesArray(source));
}
/**
* Sets the aliases that will be associated with the index when it gets created
*/
public CreateIndexRequest aliases(BytesReference source) {
// EMPTY is safe here because we never call namedObject
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, source)) {
// move to the first alias
parser.nextToken();
while ((parser.nextToken()) != XContentParser.Token.END_OBJECT) {
alias(Alias.fromXContent(parser));
}
return this;
} catch (IOException e) {
throw new OpenSearchParseException("Failed to parse aliases", e);
}
}
/**
* Adds an alias that will be associated with the index when it gets created
*/
public CreateIndexRequest alias(Alias alias) {
this.aliases.add(alias);
return this;
}
/**
* Sets the settings and mappings as a single source.
*/
public CreateIndexRequest source(String source, XContentType xContentType) {
return source(new BytesArray(source), xContentType);
}
/**
* Sets the settings and mappings as a single source.
*/
public CreateIndexRequest source(XContentBuilder source) {
return source(BytesReference.bytes(source), source.contentType());
}
/**
* Sets the settings and mappings as a single source.
*/
public CreateIndexRequest source(byte[] source, XContentType xContentType) {
return source(source, 0, source.length, xContentType);
}
/**
* Sets the settings and mappings as a single source.
*/
public CreateIndexRequest source(byte[] source, int offset, int length, XContentType xContentType) {
return source(new BytesArray(source, offset, length), xContentType);
}
/**
* Sets the settings and mappings as a single source.
*/
public CreateIndexRequest source(BytesReference source, XContentType xContentType) {
Objects.requireNonNull(xContentType);
source(XContentHelper.convertToMap(source, false, xContentType).v2(), LoggingDeprecationHandler.INSTANCE);
return this;
}
/**
* Sets the settings and mappings as a single source.
*/
@SuppressWarnings("unchecked")
public CreateIndexRequest source(Map source, DeprecationHandler deprecationHandler) {
for (Map.Entry entry : source.entrySet()) {
String name = entry.getKey();
if (SETTINGS.match(name, deprecationHandler)) {
if (entry.getValue() instanceof Map == false) {
throw new OpenSearchParseException("key [settings] must be an object");
}
settings((Map) entry.getValue());
} else if (MAPPINGS.match(name, deprecationHandler)) {
Map mappings = (Map) entry.getValue();
for (Map.Entry entry1 : mappings.entrySet()) {
mapping(entry1.getKey(), (Map) entry1.getValue());
}
} else if (ALIASES.match(name, deprecationHandler)) {
aliases((Map) entry.getValue());
} else {
throw new OpenSearchParseException("unknown key [{}] for create index", name);
}
}
return this;
}
public Map mappings() {
return this.mappings;
}
public Set aliases() {
return this.aliases;
}
public ActiveShardCount waitForActiveShards() {
return waitForActiveShards;
}
/**
* Sets the number of shard copies that should be active for index creation to return.
* Defaults to {@link ActiveShardCount#DEFAULT}, which will wait for one shard copy
* (the primary) to become active. Set this value to {@link ActiveShardCount#ALL} to
* wait for all shards (primary and all replicas) to be active before returning.
* Otherwise, use {@link ActiveShardCount#from(int)} to set this value to any
* non-negative integer, up to the number of copies per shard (number of replicas + 1),
* to wait for the desired amount of shard copies to become active before returning.
* Index creation will only wait up until the timeout value for the number of shard copies
* to be active before returning. Check {@link CreateIndexResponse#isShardsAcknowledged()} to
* determine if the requisite shard copies were all started before returning or timing out.
*
* @param waitForActiveShards number of active shard copies to wait on
*/
public CreateIndexRequest waitForActiveShards(ActiveShardCount waitForActiveShards) {
this.waitForActiveShards = waitForActiveShards;
return this;
}
/**
* A shortcut for {@link #waitForActiveShards(ActiveShardCount)} where the numerical
* shard count is passed in, instead of having to first call {@link ActiveShardCount#from(int)}
* to get the ActiveShardCount.
*/
public CreateIndexRequest waitForActiveShards(final int waitForActiveShards) {
return waitForActiveShards(ActiveShardCount.from(waitForActiveShards));
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeString(cause);
out.writeString(index);
writeSettingsToStream(settings, out);
out.writeVInt(mappings.size());
for (Map.Entry entry : mappings.entrySet()) {
out.writeString(entry.getKey());
out.writeString(entry.getValue());
}
if (out.getVersion().before(LegacyESVersion.V_6_5_0)) {
// Size of custom index metadata, which is removed
out.writeVInt(0);
}
out.writeVInt(aliases.size());
for (Alias alias : aliases) {
alias.writeTo(out);
}
if (out.getVersion().before(LegacyESVersion.V_7_0_0)) {
out.writeBoolean(true); // updateAllTypes
}
waitForActiveShards.writeTo(out);
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
innerToXContent(builder, params);
builder.endObject();
return builder;
}
public XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(SETTINGS.getPreferredName());
settings.toXContent(builder, params);
builder.endObject();
builder.startObject(MAPPINGS.getPreferredName());
for (Map.Entry entry : mappings.entrySet()) {
try (InputStream stream = new BytesArray(entry.getValue()).streamInput()) {
builder.rawField(entry.getKey(), stream, XContentType.JSON);
}
}
builder.endObject();
builder.startObject(ALIASES.getPreferredName());
for (Alias alias : aliases) {
alias.toXContent(builder, params);
}
builder.endObject();
return builder;
}
}