Search in sources :

Example 71 with XContentBuilder

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"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 72 with XContentBuilder

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);
        }
    }
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 73 with XContentBuilder

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));
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 74 with XContentBuilder

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"));
}
Also used : BulkByScrollTask(org.elasticsearch.action.bulk.byscroll.BulkByScrollTask) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 75 with XContentBuilder

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\""));
}
Also used : BulkByScrollTask(org.elasticsearch.action.bulk.byscroll.BulkByScrollTask) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) IOException(java.io.IOException)

Aggregations

XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)619 IOException (java.io.IOException)127 XContentParser (org.elasticsearch.common.xcontent.XContentParser)122 Settings (org.elasticsearch.common.settings.Settings)60 ArrayList (java.util.ArrayList)59 SearchResponse (org.elasticsearch.action.search.SearchResponse)56 Matchers.containsString (org.hamcrest.Matchers.containsString)53 HashMap (java.util.HashMap)47 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)43 Map (java.util.Map)41 RestRequest (org.elasticsearch.rest.RestRequest)37 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)36 BytesRestResponse (org.elasticsearch.rest.BytesRestResponse)35 Test (org.junit.Test)34 RestController (org.elasticsearch.rest.RestController)33 NodeClient (org.elasticsearch.client.node.NodeClient)32 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)32 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)32 GeoPoint (org.elasticsearch.common.geo.GeoPoint)31 CrateUnitTest (io.crate.test.integration.CrateUnitTest)28