Search in sources :

Example 16 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder in project crate by crate.

the class ArrayMapper method toXContent.

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    /*
         * array mapping should look like:
         *
         * "fieldName": {
         *      "type": "array":
         *      "inner": {
         *          "type": "string"
         *          ...
         *      }
         * }
         *
         *
         * Use the innerMapper to generate the mapping for the inner type which will look like:
         *
         * "fieldName": {
         *      "type": "string",
         *      ...
         * }
         *
         * and then parse the contents of the object to set it into the "inner" field of the outer array type.
         */
    XContentBuilder innerBuilder = new XContentBuilder(builder.contentType().xContent(), new BytesStreamOutput(0));
    innerBuilder.startObject();
    innerBuilder = innerMapper.toXContent(innerBuilder, params);
    innerBuilder.endObject();
    innerBuilder.close();
    XContentParser parser = builder.contentType().xContent().createParser(innerBuilder.bytes());
    //noinspection StatementWithEmptyBody
    while ((parser.nextToken() != XContentParser.Token.START_OBJECT)) {
    // consume tokens until start of object
    }
    //noinspection unchecked
    Map<String, Object> innerMap = (Map<String, Object>) parser.mapOrdered().get(innerMapper.simpleName());
    assert innerMap != null : "innerMap was null";
    builder.startObject(simpleName());
    builder.field("type", contentType());
    builder.field(INNER, innerMap);
    return builder.endObject();
}
Also used : XContentBuilderString(org.elasticsearch.common.xcontent.XContentBuilderString) Map(java.util.Map) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 17 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder in project crate by crate.

the class ArrayMapperTest method mapper.

/**
     * create index with type and mapping and validate DocumentMapper serialization
     */
private DocumentMapper mapper(String indexName, String type, String mapping) throws IOException {
    // we serialize and deserialize the mapping to make sure serialization works just fine
    client().admin().indices().prepareCreate(indexName).addMapping(type, mapping).setSettings(Settings.builder().put("number_of_replicas", 0).build()).execute().actionGet();
    client().admin().cluster().prepareHealth(indexName).setWaitForGreenStatus().setWaitForRelocatingShards(0).setWaitForEvents(Priority.LANGUID).execute().actionGet();
    IndicesService instanceFromNode = internalCluster().getInstance(IndicesService.class);
    IndexService indexService = instanceFromNode.indexServiceSafe(indexName);
    DocumentMapperParser parser = indexService.mapperService().documentMapperParser();
    DocumentMapper defaultMapper = parser.parse(type, new CompressedXContent(mapping));
    XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
    builder.startObject();
    defaultMapper.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.endObject();
    String rebuildMapping = builder.string();
    return parser.parse(type, new CompressedXContent(rebuildMapping));
}
Also used : IndexService(org.elasticsearch.index.IndexService) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) IndicesService(org.elasticsearch.indices.IndicesService) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 18 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder in project crate by crate.

the class DocIndexMetaDataTest method testNoBackwardCompatibleArrayMapping.

@Test
public void testNoBackwardCompatibleArrayMapping() throws Exception {
    XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("_meta").field("primary_keys", "id").startObject("columns").startObject("array_col").field("collection_type", "array").endObject().startObject("nested").startObject("properties").startObject("inner_nested").field("collection_type", "array").endObject().endObject().endObject().endObject().endObject().startObject("properties").startObject("id").field("type", "integer").field("index", "not_analyzed").endObject().startObject("title").field("type", "string").field("index", "no").endObject().startObject("array_col").field("type", "ip").field("index", "not_analyzed").endObject().startObject("nested").field("type", "nested").startObject("properties").startObject("inner_nested").field("type", "date").field("index", "not_analyzed").endObject().endObject().endObject().endObject().endObject();
    IndexMetaData indexMetaData = getIndexMetaData("test1", builder);
    DocIndexMetaData docIndexMetaData = newMeta(indexMetaData, "test1");
    // ARRAY TYPES NOT DETECTED
    assertThat(docIndexMetaData.references().get(ColumnIdent.fromPath("array_col")).valueType(), is((DataType) DataTypes.IP));
    assertThat(docIndexMetaData.references().get(ColumnIdent.fromPath("nested.inner_nested")).valueType(), is((DataType) DataTypes.TIMESTAMP));
}
Also used : DataType(io.crate.types.DataType) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 19 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder in project crate by crate.

the class DocIndexMetaDataTest method testAnalyzedColumnWithAnalyzer.

@Test
public void testAnalyzedColumnWithAnalyzer() throws Exception {
    XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject(Constants.DEFAULT_MAPPING_TYPE).startObject("properties").startObject("content_de").field("type", "string").field("index", "analyzed").field("analyzer", "german").endObject().startObject("content_en").field("type", "string").field("analyzer", "english").endObject().endObject().endObject();
    DocIndexMetaData md = newMeta(getIndexMetaData("test_analyzer", builder), "test_analyzer");
    assertThat(md.columns().size(), is(2));
    assertThat(md.columns().get(0).indexType(), is(Reference.IndexType.ANALYZED));
    assertThat(md.columns().get(0).ident().columnIdent().fqn(), is("content_de"));
    assertThat(md.columns().get(1).indexType(), is(Reference.IndexType.ANALYZED));
    assertThat(md.columns().get(1).ident().columnIdent().fqn(), is("content_en"));
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 20 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder in project crate by crate.

the class DocIndexMetaDataTest method extractRoutingColumnFromEmptyIndex.

@Test
public void extractRoutingColumnFromEmptyIndex() throws Exception {
    XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject(Constants.DEFAULT_MAPPING_TYPE).endObject().endObject();
    DocIndexMetaData md = newMeta(getIndexMetaData("test11", builder), "test11");
    assertThat(md.routingCol(), is(new ColumnIdent("_id")));
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Aggregations

XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)618 IOException (java.io.IOException)126 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