use of org.elasticsearch.common.xcontent.XContentBuilder in project elasticsearch by elastic.
the class BulkRequestTests method testSmileIsSupported.
public void testSmileIsSupported() throws IOException {
XContentType xContentType = XContentType.SMILE;
BytesReference data;
try (BytesStreamOutput out = new BytesStreamOutput()) {
try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType, out)) {
builder.startObject();
builder.startObject("index");
builder.field("_index", "index");
builder.field("_type", "type");
builder.field("_id", "test");
builder.endObject();
builder.endObject();
}
out.write(xContentType.xContent().streamSeparator());
try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType, out)) {
builder.startObject();
builder.field("field", "value");
builder.endObject();
}
out.write(xContentType.xContent().streamSeparator());
data = out.bytes();
}
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(data, null, null, xContentType);
assertEquals(1, bulkRequest.requests().size());
DocWriteRequest docWriteRequest = bulkRequest.requests().get(0);
assertEquals(DocWriteRequest.OpType.INDEX, docWriteRequest.opType());
assertEquals("index", docWriteRequest.index());
assertEquals("type", docWriteRequest.type());
assertEquals("test", docWriteRequest.id());
assertThat(docWriteRequest, instanceOf(IndexRequest.class));
IndexRequest request = (IndexRequest) docWriteRequest;
assertEquals(1, request.sourceAsMap().size());
assertEquals("value", request.sourceAsMap().get("field"));
}
use of org.elasticsearch.common.xcontent.XContentBuilder in project elasticsearch by elastic.
the class RolloverRequestTests method testConditionsParsing.
public void testConditionsParsing() throws Exception {
final RolloverRequest request = new RolloverRequest(randomAsciiOfLength(10), randomAsciiOfLength(10));
final XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("conditions").field("max_age", "10d").field("max_docs", 100).endObject().endObject();
RolloverRequest.PARSER.parse(createParser(builder), request, null);
Set<Condition> conditions = request.getConditions();
assertThat(conditions.size(), equalTo(2));
for (Condition condition : conditions) {
if (condition instanceof MaxAgeCondition) {
MaxAgeCondition maxAgeCondition = (MaxAgeCondition) condition;
assertThat(maxAgeCondition.value.getMillis(), equalTo(TimeValue.timeValueHours(24 * 10).getMillis()));
} else if (condition instanceof MaxDocsCondition) {
MaxDocsCondition maxDocsCondition = (MaxDocsCondition) condition;
assertThat(maxDocsCondition.value, equalTo(100L));
} else {
fail("unexpected condition " + condition);
}
}
}
use of org.elasticsearch.common.xcontent.XContentBuilder in project elasticsearch by elastic.
the class RolloverRequestTests method testParsingWithIndexSettings.
public void testParsingWithIndexSettings() throws Exception {
final RolloverRequest request = new RolloverRequest(randomAsciiOfLength(10), randomAsciiOfLength(10));
final XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("conditions").field("max_age", "10d").field("max_docs", 100).endObject().startObject("mappings").startObject("type1").startObject("properties").startObject("field1").field("type", "string").field("index", "not_analyzed").endObject().endObject().endObject().endObject().startObject("settings").field("number_of_shards", 10).endObject().startObject("aliases").startObject("alias1").endObject().endObject().endObject();
RolloverRequest.PARSER.parse(createParser(builder), request, null);
Set<Condition> conditions = request.getConditions();
assertThat(conditions.size(), equalTo(2));
assertThat(request.getCreateIndexRequest().mappings().size(), equalTo(1));
assertThat(request.getCreateIndexRequest().aliases().size(), equalTo(1));
assertThat(request.getCreateIndexRequest().settings().getAsInt("number_of_shards", 0), equalTo(10));
}
use of org.elasticsearch.common.xcontent.XContentBuilder in project elasticsearch by elastic.
the class BulkByScrollTaskTests method testXContentRepresentationOfUnfinishedSlices.
public void testXContentRepresentationOfUnfinishedSlices() throws IOException {
XContentBuilder builder = JsonXContent.contentBuilder();
BulkByScrollTask.Status completedStatus = new BulkByScrollTask.Status(2, 0, 0, 0, 0, 0, 0, 0, 0, 0, timeValueMillis(0), Float.POSITIVE_INFINITY, null, timeValueMillis(0));
BulkByScrollTask.Status status = new BulkByScrollTask.Status(Arrays.asList(null, null, new BulkByScrollTask.StatusOrException(completedStatus)), null);
status.toXContent(builder, ToXContent.EMPTY_PARAMS);
assertThat(builder.string(), containsString("\"slices\":[null,null,{\"slice_id\":2"));
}
use of org.elasticsearch.common.xcontent.XContentBuilder in project elasticsearch by elastic.
the class BulkByScrollTaskTests method testXContentRepresentationOfSliceFailures.
public void testXContentRepresentationOfSliceFailures() throws IOException {
XContentBuilder builder = JsonXContent.contentBuilder();
Exception e = new Exception();
BulkByScrollTask.Status status = new BulkByScrollTask.Status(Arrays.asList(null, null, new BulkByScrollTask.StatusOrException(e)), null);
status.toXContent(builder, ToXContent.EMPTY_PARAMS);
assertThat(builder.string(), containsString("\"slices\":[null,null,{\"type\":\"exception\""));
}
Aggregations