Improved how aliases get parsed in create index

Added also CreateIndexRequest#aliases(BytesReference) method
This commit is contained in:
javanna 2014-03-03 14:52:43 +01:00 committed by Luca Cavanna
parent a93d6d55a5
commit 9f502b0129
2 changed files with 18 additions and 7 deletions

View File

@ -256,7 +256,7 @@ public class CreateIndexRequest extends AcknowledgedRequest<CreateIndexRequest>
try { try {
XContentBuilder builder = XContentFactory.jsonBuilder(); XContentBuilder builder = XContentFactory.jsonBuilder();
builder.map(source); builder.map(source);
return aliases(builder.string()); return aliases(builder.bytes());
} catch (IOException e) { } catch (IOException e) {
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e); throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
} }
@ -266,19 +266,22 @@ public class CreateIndexRequest extends AcknowledgedRequest<CreateIndexRequest>
* Sets the aliases that will be associated with the index when it gets created * Sets the aliases that will be associated with the index when it gets created
*/ */
public CreateIndexRequest aliases(XContentBuilder source) { public CreateIndexRequest aliases(XContentBuilder source) {
try { return aliases(source.bytes());
return aliases(source.string());
} catch (IOException e) {
throw new ElasticsearchIllegalArgumentException("Failed to build json for aliases", e);
}
} }
/** /**
* Sets the aliases that will be associated with the index when it gets created * Sets the aliases that will be associated with the index when it gets created
*/ */
public CreateIndexRequest aliases(String source) { 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) {
try { try {
XContentParser parser = XContentHelper.createParser(new BytesArray(source)); XContentParser parser = XContentHelper.createParser(source);
//move to the first alias //move to the first alias
parser.nextToken(); parser.nextToken();
while ((parser.nextToken()) != XContentParser.Token.END_OBJECT) { while ((parser.nextToken()) != XContentParser.Token.END_OBJECT) {

View File

@ -174,6 +174,14 @@ public class CreateIndexRequestBuilder extends AcknowledgedRequestBuilder<Create
return this; return this;
} }
/**
* Sets the aliases that will be associated with the index when it gets created
*/
public CreateIndexRequestBuilder setAliases(BytesReference source) {
request.aliases(source);
return this;
}
/** /**
* Adds an alias that will be associated with the index when it gets created * Adds an alias that will be associated with the index when it gets created
*/ */