Search in sources :

Example 6 with SourceToParse

use of org.elasticsearch.index.mapper.SourceToParse in project crate by crate.

the class ArrayMapperTest method testInvalidArrayNonConvertableType.

@Test
public void testInvalidArrayNonConvertableType() throws Exception {
    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", "double").endObject().endObject().endObject().endObject().endObject());
    DocumentMapper mapper = mapper(INDEX, mapping);
    expectedException.expect(MapperParsingException.class);
    expectedException.expectMessage("failed to parse field [array_field] of type [double]");
    BytesReference bytesReference = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().array("array_field", true, false, true).endObject());
    SourceToParse sourceToParse = new SourceToParse(INDEX, "abc", bytesReference, XContentType.JSON);
    mapper.parse(sourceToParse);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) SourceToParse(org.elasticsearch.index.mapper.SourceToParse) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 7 with SourceToParse

use of org.elasticsearch.index.mapper.SourceToParse 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"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) IndexableField(org.apache.lucene.index.IndexableField) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) ArrayList(java.util.ArrayList) SourceToParse(org.elasticsearch.index.mapper.SourceToParse) Document(org.elasticsearch.index.mapper.ParseContext.Document) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) FieldMapper(org.elasticsearch.index.mapper.FieldMapper) KeywordFieldMapper(org.elasticsearch.index.mapper.KeywordFieldMapper) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 8 with SourceToParse

use of org.elasticsearch.index.mapper.SourceToParse 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()));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) SourceToParse(org.elasticsearch.index.mapper.SourceToParse) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 9 with SourceToParse

use of org.elasticsearch.index.mapper.SourceToParse 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\"" + "}" + "}" + "}" + "}}"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) Document(org.elasticsearch.index.mapper.ParseContext.Document) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) ParseContext(org.elasticsearch.index.mapper.ParseContext) SourceToParse(org.elasticsearch.index.mapper.SourceToParse) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 10 with SourceToParse

use of org.elasticsearch.index.mapper.SourceToParse 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\"" + "}" + "}" + "}" + "}" + "}}}"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) KeywordFieldMapper(org.elasticsearch.index.mapper.KeywordFieldMapper) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) SourceToParse(org.elasticsearch.index.mapper.SourceToParse) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Aggregations

SourceToParse (org.elasticsearch.index.mapper.SourceToParse)19 Test (org.junit.Test)11 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)9 BytesReference (org.elasticsearch.common.bytes.BytesReference)9 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)9 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)8 Engine (org.elasticsearch.index.engine.Engine)7 Translog (org.elasticsearch.index.translog.Translog)5 BytesArray (org.elasticsearch.common.bytes.BytesArray)4 VersionType (org.elasticsearch.index.VersionType)4 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 List (java.util.List)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Collectors (java.util.stream.Collectors)2 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)2 ResyncReplicationRequest (org.elasticsearch.action.resync.ResyncReplicationRequest)2 PlainActionFuture (org.elasticsearch.action.support.PlainActionFuture)2