use of org.elasticsearch.index.mapper.ParsedDocument in project crate by crate.
the class ArrayMapperTest method testCopyToFieldsOfInnerMapping.
@Test
public void testCopyToFieldsOfInnerMapping() throws Exception {
// @formatter:off
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject(TYPE).startObject("properties").startObject("string_array").field("type", ArrayMapper.CONTENT_TYPE).startObject(ArrayMapper.INNER_TYPE).field("type", "text").field("index", "true").field("copy_to", "string_array_ft").endObject().endObject().endObject().endObject().endObject());
// @formatter:on
DocumentMapper mapper = mapper(INDEX, mapping);
FieldMapper arrayMapper = (FieldMapper) mapper.mappers().getMapper("string_array");
assertThat(arrayMapper, is(instanceOf(ArrayMapper.class)));
assertThat(arrayMapper.copyTo().copyToFields(), contains("string_array_ft"));
// @formatter:on
BytesReference bytesReference = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().startArray("string_array").value("foo").value("bar").endArray().endObject());
SourceToParse sourceToParse = new SourceToParse(INDEX, "1", bytesReference, XContentType.JSON);
ParsedDocument doc = mapper.parse(sourceToParse);
// @formatter:off
List<String> copyValues = new ArrayList<>();
Document document = doc.docs().get(0);
IndexableField[] fields = document.getFields("string_array_ft");
for (var field : fields) {
if (field.fieldType().docValuesType() == DocValuesType.SORTED_SET) {
copyValues.add(field.binaryValue().utf8ToString());
}
}
assertThat(copyValues, containsInAnyOrder("foo", "bar"));
}
use of org.elasticsearch.index.mapper.ParsedDocument in project crate by crate.
the class ArrayMapperTest method testParseNullOnObjectArray.
@Test
public void testParseNullOnObjectArray() throws Exception {
// @formatter: on
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject(TYPE).startObject("properties").startObject("array_field").field("type", ArrayMapper.CONTENT_TYPE).startObject(ArrayMapper.INNER_TYPE).field("type", "object").startObject("properties").endObject().endObject().endObject().endObject().endObject().endObject());
// @formatter: off
DocumentMapper mapper = mapper(INDEX, mapping);
BytesReference bytesReference = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().nullField("array_field").endObject());
SourceToParse sourceToParse = new SourceToParse(INDEX, "abc", bytesReference, XContentType.JSON);
ParsedDocument parsedDoc = mapper.parse(sourceToParse);
assertThat(parsedDoc.docs().size(), is(1));
assertThat(parsedDoc.docs().get(0).getField("array_field"), is(nullValue()));
}
use of org.elasticsearch.index.mapper.ParsedDocument in project crate by crate.
the class ArrayMapperTest method testSimpleArrayMapping.
@Test
public void testSimpleArrayMapping() throws Exception {
// @formatter:off
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject(TYPE).startObject("properties").startObject("array_field").field("type", ArrayMapper.CONTENT_TYPE).startObject(ArrayMapper.INNER_TYPE).field("type", "keyword").endObject().endObject().endObject().endObject().endObject());
// @formatter:on
DocumentMapper mapper = mapper(INDEX, mapping);
assertThat(mapper.mappers().getMapper("array_field"), is(instanceOf(ArrayMapper.class)));
BytesReference bytesReference = BytesReference.bytes(JsonXContent.contentBuilder().startObject().array("array_field", "a", "b", "c").endObject());
SourceToParse sourceToParse = new SourceToParse(INDEX, "abc", bytesReference, XContentType.JSON);
ParsedDocument doc = mapper.parse(sourceToParse);
assertThat(doc.dynamicMappingsUpdate() == null, is(true));
assertThat(doc.docs().size(), is(1));
ParseContext.Document fields = doc.docs().get(0);
Set<String> values = uniqueValuesFromFields(fields, "array_field");
assertThat(values, Matchers.containsInAnyOrder("a", "b", "c"));
assertThat(mapper.mappingSource().string(), is("{\"default\":{" + "\"properties\":{" + "\"array_field\":{" + "\"type\":\"array\"," + "\"inner\":{" + "\"type\":\"keyword\"" + "}" + "}" + "}" + "}}"));
}
use of org.elasticsearch.index.mapper.ParsedDocument in project crate by crate.
the class ArrayMapperTest method testObjectArrayMapping.
@Test
public void testObjectArrayMapping() throws Exception {
// @formatter: off
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject(TYPE).startObject("properties").startObject("array_field").field("type", ArrayMapper.CONTENT_TYPE).startObject(ArrayMapper.INNER_TYPE).field("type", "object").field("dynamic", true).startObject("properties").startObject("s").field("type", "keyword").endObject().endObject().endObject().endObject().endObject().endObject().endObject());
DocumentMapper mapper = mapper(INDEX, mapping);
// child object mapper
assertThat(mapper.objectMappers().get("array_field"), is(instanceOf(ObjectArrayMapper.class)));
BytesReference bytesReference = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().startArray("array_field").startObject().field("s", "a").endObject().startObject().field("s", "b").endObject().startObject().field("s", "c").endObject().endArray().endObject());
SourceToParse sourceToParse = new SourceToParse(INDEX, "abc", bytesReference, XContentType.JSON);
ParsedDocument doc = mapper.parse(sourceToParse);
// @formatter: off
assertThat(doc.dynamicMappingsUpdate(), nullValue());
assertThat(doc.docs().size(), is(1));
assertThat(uniqueValuesFromFields(doc.docs().get(0), "array_field.s"), containsInAnyOrder("a", "b", "c"));
assertThat(mapper.mappers().getMapper("array_field.s"), instanceOf(KeywordFieldMapper.class));
assertThat(mapper.mappingSource().string(), is("{\"default\":{" + "\"properties\":{" + "\"array_field\":{" + "\"type\":\"array\"," + "\"inner\":{" + "\"dynamic\":\"true\"," + "\"properties\":{" + "\"s\":{" + "\"type\":\"keyword\"" + "}" + "}" + "}" + "}" + "}}}"));
}
use of org.elasticsearch.index.mapper.ParsedDocument in project crate by crate.
the class ArrayMapperTest method testObjectArrayMappingNewColumn.
@Test
public void testObjectArrayMappingNewColumn() throws Exception {
// @formatter: off
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject(TYPE).startObject("properties").startObject("array_field").field("type", ArrayMapper.CONTENT_TYPE).startObject(ArrayMapper.INNER_TYPE).field("type", "object").field("dynamic", true).startObject("properties").startObject("s").field("type", "keyword").endObject().endObject().endObject().endObject().endObject().endObject().endObject());
DocumentMapper mapper = mapper(INDEX, mapping);
// child object mapper
assertThat(mapper.objectMappers().get("array_field"), is(instanceOf(ObjectArrayMapper.class)));
BytesReference bytesReference = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().startArray("array_field").startObject().field("s", "a").field("new", true).endObject().endArray().endObject());
SourceToParse sourceToParse = new SourceToParse(INDEX, "abc", bytesReference, XContentType.JSON);
ParsedDocument doc = mapper.parse(sourceToParse);
Mapping mappingUpdate = doc.dynamicMappingsUpdate();
assertThat(mappingUpdate, notNullValue());
mapper = mapper.merge(mappingUpdate);
assertThat(doc.docs().size(), is(1));
String[] values = doc.docs().get(0).getValues("array_field.new");
assertThat(values, arrayContainingInAnyOrder(is("T"), is("1")));
String mappingSourceString = new CompressedXContent(mapper, XContentType.JSON, ToXContent.EMPTY_PARAMS).string();
assertThat(mappingSourceString, is("{\"default\":{" + "\"properties\":{" + "\"array_field\":{" + "\"type\":\"array\"," + "\"inner\":{" + "\"dynamic\":\"true\"," + "\"properties\":{" + "\"new\":{\"type\":\"boolean\"}," + "\"s\":{" + "\"type\":\"keyword\"" + "}" + "}" + "}" + "}" + "}}}"));
}
Aggregations