Search in sources :

Example 81 with XContentType

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class SearchSortValuesTests method testFromXContent.

public void testFromXContent() throws IOException {
    SearchSortValues sortValues = createTestItem();
    XContentType xcontentType = randomFrom(XContentType.values());
    boolean humanReadable = randomBoolean();
    BytesReference originalBytes = toXContent(sortValues, xcontentType, humanReadable);
    SearchSortValues parsed;
    try (XContentParser parser = createParser(xcontentType.xContent(), originalBytes)) {
        // skip to the elements start array token, fromXContent advances from there if called
        parser.nextToken();
        parser.nextToken();
        parser.nextToken();
        parsed = SearchSortValues.fromXContent(parser);
        parser.nextToken();
        assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
        assertNull(parser.nextToken());
    }
    assertToXContentEquivalent(originalBytes, toXContent(parsed, xcontentType, humanReadable), xcontentType);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 82 with XContentType

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class NestedIdentityTests method testFromXContent.

public void testFromXContent() throws IOException {
    NestedIdentity nestedIdentity = createTestItem(randomInt(3));
    XContentType xcontentType = randomFrom(XContentType.values());
    XContentBuilder builder = XContentFactory.contentBuilder(xcontentType);
    if (randomBoolean()) {
        builder.prettyPrint();
    }
    builder = nestedIdentity.innerToXContent(builder, ToXContent.EMPTY_PARAMS);
    XContentParser parser = createParser(builder);
    NestedIdentity parsedNestedIdentity = NestedIdentity.fromXContent(parser);
    assertEquals(nestedIdentity, parsedNestedIdentity);
    assertNull(parser.nextToken());
}
Also used : XContentType(org.elasticsearch.common.xcontent.XContentType) NestedIdentity(org.elasticsearch.search.SearchHit.NestedIdentity) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 83 with XContentType

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project crate by crate.

the class JsonXContentGenerator method writeRawField.

@Override
public void writeRawField(String name, InputStream content) throws IOException {
    if (content.markSupported() == false) {
        // needed for the XContentFactory.xContentType call
        content = new BufferedInputStream(content);
    }
    XContentType contentType = XContentFactory.xContentType(content);
    if (contentType == null) {
        throw new IllegalArgumentException("Can't write raw bytes whose xcontent-type can't be guessed");
    }
    writeRawField(name, content, contentType);
}
Also used : XContentType(org.elasticsearch.common.xcontent.XContentType) BufferedInputStream(java.io.BufferedInputStream)

Example 84 with XContentType

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project crate by crate.

the class ColumnPolicyIntegrationTest method testDynamicPartitionedTable.

@Test
public void testDynamicPartitionedTable() throws Exception {
    execute("create table numbers (" + "  num int, " + "  odd boolean," + "  prime boolean" + ") partitioned by (odd) with (column_policy='dynamic', number_of_replicas=0)");
    ensureYellow();
    GetIndexTemplatesResponse templateResponse = client().admin().indices().prepareGetTemplates(PartitionName.templateName(sqlExecutor.getCurrentSchema(), "numbers")).execute().actionGet();
    assertThat(templateResponse.getIndexTemplates().size(), is(1));
    IndexTemplateMetadata template = templateResponse.getIndexTemplates().get(0);
    CompressedXContent mappingStr = template.mappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(mappingStr, is(notNullValue()));
    Tuple<XContentType, Map<String, Object>> typeAndMap = XContentHelper.convertToMap(mappingStr.compressedReference(), false);
    @SuppressWarnings("unchecked") Map<String, Object> mapping = (Map<String, Object>) typeAndMap.v2().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(String.valueOf(mapping.get("dynamic")), is("true"));
    execute("insert into numbers (num, odd, prime) values (?, ?, ?)", new Object[] { 6, true, false });
    execute("refresh table numbers");
    Map<String, Object> sourceMap = getSourceMap(new PartitionName(new RelationName("doc", "numbers"), Collections.singletonList("true")).asIndexName());
    assertThat(String.valueOf(sourceMap.get("dynamic")), is("true"));
    execute("insert into numbers (num, odd, prime, perfect) values (?, ?, ?, ?)", new Object[] { 28, true, false, true });
    ensureYellow();
    execute("refresh table numbers");
    waitForMappingUpdateOnAll("numbers", "perfect");
    execute("select * from numbers order by num");
    assertThat(response.rowCount(), is(2L));
    assertThat(response.cols(), arrayContaining("num", "odd", "prime", "perfect"));
    assertThat(TestingHelpers.printedTable(response.rows()), is("6| true| false| NULL\n" + "28| true| false| true\n"));
    execute("update numbers set prime=true, changed='2014-10-23T10:20', author='troll' where num=28");
    assertThat(response.rowCount(), is(1L));
    waitForMappingUpdateOnAll("numbers", "changed");
}
Also used : PartitionName(io.crate.metadata.PartitionName) XContentType(org.elasticsearch.common.xcontent.XContentType) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) RelationName(io.crate.metadata.RelationName) Matchers.containsString(org.hamcrest.Matchers.containsString) Map(java.util.Map) Test(org.junit.Test)

Example 85 with XContentType

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project crate by crate.

the class ColumnPolicyIntegrationTest method testStrictPartitionedTableUpdate.

@Test
public void testStrictPartitionedTableUpdate() throws Exception {
    execute("create table numbers (" + "  num int, " + "  odd boolean," + "  prime boolean" + ") partitioned by (odd) with (column_policy='strict', number_of_replicas=0)");
    ensureYellow();
    GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates(PartitionName.templateName(sqlExecutor.getCurrentSchema(), "numbers")).execute().actionGet();
    assertThat(response.getIndexTemplates().size(), is(1));
    IndexTemplateMetadata template = response.getIndexTemplates().get(0);
    CompressedXContent mappingStr = template.mappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(mappingStr, is(notNullValue()));
    Tuple<XContentType, Map<String, Object>> typeAndMap = XContentHelper.convertToMap(mappingStr.compressedReference(), false);
    @SuppressWarnings("unchecked") Map<String, Object> mapping = (Map<String, Object>) typeAndMap.v2().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(decodeMappingValue(mapping.get("dynamic")), is(ColumnPolicy.STRICT));
    execute("insert into numbers (num, odd, prime) values (?, ?, ?)", new Object[] { 6, true, false });
    execute("refresh table numbers");
    Map<String, Object> sourceMap = getSourceMap(new PartitionName(new RelationName("doc", "numbers"), Arrays.asList("true")).asIndexName());
    assertThat(decodeMappingValue(sourceMap.get("dynamic")), is(ColumnPolicy.STRICT));
    assertThrowsMatches(() -> execute("update numbers set num=?, perfect=? where num=6", new Object[] { 28, true }), isSQLError(is("Column perfect unknown"), UNDEFINED_COLUMN, NOT_FOUND, 4043));
}
Also used : PartitionName(io.crate.metadata.PartitionName) XContentType(org.elasticsearch.common.xcontent.XContentType) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) RelationName(io.crate.metadata.RelationName) Matchers.containsString(org.hamcrest.Matchers.containsString) Map(java.util.Map) Test(org.junit.Test)

Aggregations

XContentType (org.elasticsearch.common.xcontent.XContentType)86 BytesReference (org.elasticsearch.common.bytes.BytesReference)50 XContentParser (org.elasticsearch.common.xcontent.XContentParser)45 Map (java.util.Map)20 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)20 HashMap (java.util.HashMap)14 IndexRequest (org.elasticsearch.action.index.IndexRequest)11 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)10 IOException (java.io.IOException)9 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)8 ArrayList (java.util.ArrayList)6 Collections.emptyMap (java.util.Collections.emptyMap)6 List (java.util.List)6 ElasticsearchException (org.elasticsearch.ElasticsearchException)6 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)6 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)6 Collections.singletonMap (java.util.Collections.singletonMap)5 HttpEntity (org.apache.http.HttpEntity)5 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)5 GetRequest (org.elasticsearch.action.get.GetRequest)5