Skip to content

Commit

Permalink
allow to set the full source when creating index (settings + mappings…
Browse files Browse the repository at this point in the history
…) in the Java API
  • Loading branch information
kimchy committed Apr 28, 2012
1 parent 3893417 commit 6e09eab
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 44 deletions.
Expand Up @@ -19,8 +19,10 @@

package org.elasticsearch.action.admin.indices.create;

import com.google.common.base.Charsets;
import org.elasticsearch.ElasticSearchGenerationException;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.ElasticSearchParseException;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.MasterNodeOperationRequest;
import org.elasticsearch.common.collect.MapBuilder;
Expand Down Expand Up @@ -222,6 +224,71 @@ public CreateIndexRequest mapping(String type, Map source) {
}
}

/**
* Sets the settings and mappings as a single source.
*/
public CreateIndexRequest source(String source) {
return source(source.getBytes(Charsets.UTF_8));
}

/**
* Sets the settings and mappings as a single source.
*/
public CreateIndexRequest source(XContentBuilder source) {
try {
return source(source.underlyingBytes(), 0, source.underlyingBytesLength());
} catch (IOException e) {
throw new ElasticSearchParseException("failed to parse source to create index", e);
}
}

/**
* Sets the settings and mappings as a single source.
*/
public CreateIndexRequest source(byte[] source) {
return source(source, 0, source.length);
}

/**
* Sets the settings and mappings as a single source.
*/
public CreateIndexRequest source(byte[] source, int offset, int length) {
XContentType xContentType = XContentFactory.xContentType(source, offset, length);
if (xContentType != null) {
try {
source(XContentFactory.xContent(xContentType).createParser(source, offset, length).mapAndClose());
} catch (IOException e) {
throw new ElasticSearchParseException("failed to parse source for create index", e);
}
} else {
settings(new String(source, offset, length, Charsets.UTF_8));
}
return this;
}

/**
* Sets the settings and mappings as a single source.
*/
public CreateIndexRequest source(Map<String, Object> source) {
boolean found = false;
if (source.containsKey("settings")) {
settings((Map<String, Object>) source.get("settings"));
found = true;
}
if (source.containsKey("mappings")) {
found = true;
Map<String, Object> mappings = (Map<String, Object>) source.get("mappings");
for (Map.Entry<String, Object> entry : mappings.entrySet()) {
mapping(entry.getKey(), (Map<String, Object>) entry.getValue());
}
}
if (!found) {
// the top level are settings, use them
settings(source);
}
return this;
}

Map<String, String> mappings() {
return this.mappings;
}
Expand Down
Expand Up @@ -127,6 +127,46 @@ public CreateIndexRequestBuilder addMapping(String type, Map<String, Object> sou
return this;
}

/**
* Sets the settings and mappings as a single source.
*/
public CreateIndexRequestBuilder setSource(String source) {
request.source(source);
return this;
}

/**
* Sets the settings and mappings as a single source.
*/
public CreateIndexRequestBuilder setSource(byte[] source) {
request.source(source);
return this;
}

/**
* Sets the settings and mappings as a single source.
*/
public CreateIndexRequestBuilder setSource(byte[] source, int offset, int length) {
request.source(source, offset, length);
return this;
}

/**
* Sets the settings and mappings as a single source.
*/
public CreateIndexRequestBuilder setSource(Map<String, Object> source) {
request.source(source);
return this;
}

/**
* Sets the settings and mappings as a single source.
*/
public CreateIndexRequestBuilder setSource(XContentBuilder source) {
request.source(source);
return this;
}

/**
* Timeout to wait for the index creation to be acknowledged by current cluster nodes. Defaults
* to <tt>10s</tt>.
Expand Down
Expand Up @@ -25,18 +25,13 @@
import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsException;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestXContentBuilder;

import java.io.IOException;
import java.util.Map;

import static org.elasticsearch.common.unit.TimeValue.timeValueSeconds;
import static org.elasticsearch.rest.RestStatus.BAD_REQUEST;
import static org.elasticsearch.rest.RestStatus.OK;

/**
Expand All @@ -56,47 +51,15 @@ public RestCreateIndexAction(Settings settings, Client client, RestController co
public void handleRequest(final RestRequest request, final RestChannel channel) {
CreateIndexRequest createIndexRequest = new CreateIndexRequest(request.param("index"));
if (request.hasContent()) {
XContentType xContentType = XContentFactory.xContentType(request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength());
if (xContentType != null) {
try {
createIndexRequest.source(request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength());
} catch (Exception e) {
try {
Map<String, Object> source = XContentFactory.xContent(xContentType)
.createParser(request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength()).mapAndClose();
boolean found = false;
if (source.containsKey("settings")) {
createIndexRequest.settings((Map<String, Object>) source.get("settings"));
found = true;
}
if (source.containsKey("mappings")) {
found = true;
Map<String, Object> mappings = (Map<String, Object>) source.get("mappings");
for (Map.Entry<String, Object> entry : mappings.entrySet()) {
createIndexRequest.mapping(entry.getKey(), (Map<String, Object>) entry.getValue());
}
}
if (!found) {
// the top level are settings, use them
createIndexRequest.settings(source);
}
} catch (Exception e) {
try {
channel.sendResponse(new XContentThrowableRestResponse(request, e));
} catch (IOException e1) {
logger.warn("Failed to send response", e1);
}
return;
}
} else {
// its plain settings, parse and set them
try {
createIndexRequest.settings(request.contentAsString());
} catch (Exception e) {
try {
channel.sendResponse(new XContentThrowableRestResponse(request, BAD_REQUEST, new SettingsException("Failed to parse index settings", e)));
} catch (IOException e1) {
logger.warn("Failed to send response", e1);
}
return;
channel.sendResponse(new XContentThrowableRestResponse(request, e));
} catch (IOException e1) {
logger.warn("Failed to send response", e1);
}
return;
}
}

Expand Down

0 comments on commit 6e09eab

Please sign in to comment.