Search in sources :

Example 56 with CompressedXContent

use of org.elasticsearch.common.compress.CompressedXContent in project elasticsearch by elastic.

the class DocumentParserTests method testPropagateDynamicWithDynamicMapper.

public void testPropagateDynamicWithDynamicMapper() throws Exception {
    DocumentMapperParser mapperParser = createIndex("test").mapperService().documentMapperParser();
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").field("dynamic", false).startObject("properties").startObject("foo").field("type", "object").field("dynamic", true).startObject("properties").endObject().endObject().endObject().endObject().endObject().string();
    DocumentMapper mapper = mapperParser.parse("type", new CompressedXContent(mapping));
    BytesReference bytes = XContentFactory.jsonBuilder().startObject().startObject("foo").startObject("bar").field("baz", "something").endObject().endObject().endObject().bytes();
    ParsedDocument doc = mapper.parse("test", "type", "1", bytes);
    assertNotNull(doc.dynamicMappingsUpdate());
    assertNotNull(doc.rootDoc().getField("foo.bar.baz"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 57 with CompressedXContent

use of org.elasticsearch.common.compress.CompressedXContent in project elasticsearch by elastic.

the class CopyToMapperTests method testCopyToDynamicNestedObjectParsing.

public void testCopyToDynamicNestedObjectParsing() throws Exception {
    String mapping = jsonBuilder().startObject().startObject("type1").startArray("dynamic_templates").startObject().startObject("objects").field("match_mapping_type", "object").startObject("mapping").field("type", "nested").endObject().endObject().endObject().endArray().startObject("properties").startObject("copy_test").field("type", "text").field("copy_to", "very.inner.field").endObject().endObject().endObject().endObject().string();
    DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping));
    BytesReference json = jsonBuilder().startObject().field("copy_test", "foo").field("new_field", "bar").endObject().bytes();
    try {
        docMapper.parse("test", "type1", "1", json).rootDoc();
        fail();
    } catch (MapperParsingException ex) {
        assertThat(ex.getMessage(), startsWith("It is forbidden to create dynamic nested objects ([very]) through `copy_to`"));
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent)

Example 58 with CompressedXContent

use of org.elasticsearch.common.compress.CompressedXContent in project elasticsearch by elastic.

the class CopyToMapperTests method testCopyToStrictDynamicInnerObjectParsing.

public void testCopyToStrictDynamicInnerObjectParsing() throws Exception {
    String mapping = jsonBuilder().startObject().startObject("type1").field("dynamic", "strict").startObject("properties").startObject("copy_test").field("type", "text").field("copy_to", "very.inner.field").endObject().endObject().endObject().endObject().string();
    DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping));
    BytesReference json = jsonBuilder().startObject().field("copy_test", "foo").endObject().bytes();
    try {
        docMapper.parse("test", "type1", "1", json).rootDoc();
        fail();
    } catch (MapperParsingException ex) {
        assertThat(ex.getMessage(), startsWith("mapping set to strict, dynamic introduction of [very] within [type1] is not allowed"));
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent)

Example 59 with CompressedXContent

use of org.elasticsearch.common.compress.CompressedXContent in project elasticsearch by elastic.

the class CopyToMapperTests method testCopyToNestedField.

public void testCopyToNestedField() throws Exception {
    IndexService indexService = createIndex("test");
    DocumentMapperParser parser = indexService.mapperService().documentMapperParser();
    XContentBuilder mapping = jsonBuilder().startObject().startObject("type").startObject("properties").startObject("target").field("type", "long").field("doc_values", false).endObject().startObject("n1").field("type", "nested").startObject("properties").startObject("target").field("type", "long").field("doc_values", false).endObject().startObject("n2").field("type", "nested").startObject("properties").startObject("target").field("type", "long").field("doc_values", false).endObject().startObject("source").field("type", "long").field("doc_values", false).startArray("copy_to").value(// should go to the root doc
    "target").value(// should go to the parent doc
    "n1.target").value(// should go to the current doc
    "n1.n2.target").endArray().endObject().endObject().endObject().endObject().endObject().endObject().endObject().endObject();
    DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping.string()));
    XContentBuilder jsonDoc = XContentFactory.jsonBuilder().startObject().startArray("n1").startObject().startArray("n2").startObject().field("source", 3).endObject().startObject().field("source", 5).endObject().endArray().endObject().startObject().startArray("n2").startObject().field("source", 7).endObject().endArray().endObject().endArray().endObject();
    ParsedDocument doc = mapper.parse("test", "type", "1", jsonDoc.bytes());
    assertEquals(6, doc.docs().size());
    Document nested = doc.docs().get(0);
    assertFieldValue(nested, "n1.n2.target", 7L);
    assertFieldValue(nested, "n1.target");
    assertFieldValue(nested, "target");
    nested = doc.docs().get(2);
    assertFieldValue(nested, "n1.n2.target", 5L);
    assertFieldValue(nested, "n1.target");
    assertFieldValue(nested, "target");
    nested = doc.docs().get(3);
    assertFieldValue(nested, "n1.n2.target", 3L);
    assertFieldValue(nested, "n1.target");
    assertFieldValue(nested, "target");
    Document parent = doc.docs().get(1);
    assertFieldValue(parent, "target");
    assertFieldValue(parent, "n1.target", 7L);
    assertFieldValue(parent, "n1.n2.target");
    parent = doc.docs().get(4);
    assertFieldValue(parent, "target");
    assertFieldValue(parent, "n1.target", 3L, 5L);
    assertFieldValue(parent, "n1.n2.target");
    Document root = doc.docs().get(5);
    assertFieldValue(root, "target", 3L, 5L, 7L);
    assertFieldValue(root, "n1.target");
    assertFieldValue(root, "n1.n2.target");
}
Also used : IndexService(org.elasticsearch.index.IndexService) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Document(org.elasticsearch.index.mapper.ParseContext.Document) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 60 with CompressedXContent

use of org.elasticsearch.common.compress.CompressedXContent in project elasticsearch by elastic.

the class CopyToMapperTests method testCopyToFieldMerge.

public void testCopyToFieldMerge() throws Exception {
    String mappingBefore = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("copy_test").field("type", "text").array("copy_to", "foo", "bar").endObject().endObject().endObject().endObject().string();
    String mappingAfter = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("copy_test").field("type", "text").array("copy_to", "baz", "bar").endObject().endObject().endObject().endObject().string();
    MapperService mapperService = createIndex("test").mapperService();
    DocumentMapper docMapperBefore = mapperService.merge("type1", new CompressedXContent(mappingBefore), MapperService.MergeReason.MAPPING_UPDATE, false);
    assertEquals(Arrays.asList("foo", "bar"), docMapperBefore.mappers().getMapper("copy_test").copyTo().copyToFields());
    DocumentMapper docMapperAfter = mapperService.merge("type1", new CompressedXContent(mappingAfter), MapperService.MergeReason.MAPPING_UPDATE, false);
    assertEquals(Arrays.asList("baz", "bar"), docMapperAfter.mappers().getMapper("copy_test").copyTo().copyToFields());
    assertEquals(Arrays.asList("foo", "bar"), docMapperBefore.mappers().getMapper("copy_test").copyTo().copyToFields());
}
Also used : CompressedXContent(org.elasticsearch.common.compress.CompressedXContent)

Aggregations

CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)366 Matchers.containsString (org.hamcrest.Matchers.containsString)223 IndexableField (org.apache.lucene.index.IndexableField)75 BytesReference (org.elasticsearch.common.bytes.BytesReference)62 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)51 IndexService (org.elasticsearch.index.IndexService)49 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)46 Settings (org.elasticsearch.common.settings.Settings)25 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)19 BytesRef (org.apache.lucene.util.BytesRef)18 BytesArray (org.elasticsearch.common.bytes.BytesArray)16 MapperParsingException (org.elasticsearch.index.mapper.MapperParsingException)16 IOException (java.io.IOException)15 DocumentMapperParser (org.elasticsearch.index.mapper.DocumentMapperParser)15 Map (java.util.Map)13 Document (org.elasticsearch.index.mapper.ParseContext.Document)13 MapperService (org.elasticsearch.index.mapper.MapperService)12 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)10 IndexTemplateMetadata (org.elasticsearch.cluster.metadata.IndexTemplateMetadata)9 FieldMapper (org.elasticsearch.index.mapper.FieldMapper)9