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();
}
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));
}
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));
}
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"));
}
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")));
}
Aggregations