Search in sources :

Example 71 with DocumentMapper

use of org.elasticsearch.index.mapper.DocumentMapper in project elasticsearch by elastic.

the class ObjectMapperTests method testMerge.

public void testMerge() throws IOException {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("foo").field("type", "keyword").endObject().endObject().endObject().endObject().string();
    MapperService mapperService = createIndex("test").mapperService();
    DocumentMapper mapper = mapperService.merge("type", new CompressedXContent(mapping), MergeReason.MAPPING_UPDATE, false);
    assertNull(mapper.root().includeInAll());
    assertNull(mapper.root().dynamic());
    String update = XContentFactory.jsonBuilder().startObject().startObject("type").field("include_in_all", false).field("dynamic", "strict").endObject().endObject().string();
    mapper = mapperService.merge("type", new CompressedXContent(update), MergeReason.MAPPING_UPDATE, false);
    assertFalse(mapper.root().includeInAll());
    assertEquals(Dynamic.STRICT, mapper.root().dynamic());
}
Also used : DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 72 with DocumentMapper

use of org.elasticsearch.index.mapper.DocumentMapper in project elasticsearch by elastic.

the class ObjectMapperTests method testDifferentInnerObjectTokenFailure.

public void testDifferentInnerObjectTokenFailure() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject().string();
    DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> {
        defaultMapper.parse("test", "type", "1", new BytesArray(" {\n" + "      \"object\": {\n" + "        \"array\":[\n" + "        {\n" + "          \"object\": { \"value\": \"value\" }\n" + "        },\n" + "        {\n" + "          \"object\":\"value\"\n" + "        }\n" + "        ]\n" + "      },\n" + "      \"value\":\"value\"\n" + "    }"));
    });
    assertTrue(e.getMessage(), e.getMessage().contains("different type"));
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 73 with DocumentMapper

use of org.elasticsearch.index.mapper.DocumentMapper in project elasticsearch by elastic.

the class PathMatchDynamicTemplateTests method testSimple.

public void testSimple() throws Exception {
    String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/dynamictemplate/pathmatch/test-mapping.json");
    IndexService index = createIndex("test");
    client().admin().indices().preparePutMapping("test").setType("person").setSource(mapping, XContentType.JSON).get();
    DocumentMapper docMapper = index.mapperService().documentMapper("person");
    byte[] json = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/dynamictemplate/pathmatch/test-data.json");
    ParsedDocument parsedDoc = docMapper.parse("test", "person", "1", new BytesArray(json));
    client().admin().indices().preparePutMapping("test").setType("person").setSource(parsedDoc.dynamicMappingsUpdate().toString(), XContentType.JSON).get();
    docMapper = index.mapperService().documentMapper("person");
    Document doc = parsedDoc.rootDoc();
    IndexableField f = doc.getField("name");
    assertThat(f.name(), equalTo("name"));
    assertThat(f.stringValue(), equalTo("top_level"));
    assertThat(f.fieldType().stored(), equalTo(false));
    FieldMapper fieldMapper = docMapper.mappers().getMapper("name");
    assertThat(fieldMapper.fieldType().stored(), equalTo(false));
    f = doc.getField("obj1.name");
    assertThat(f.name(), equalTo("obj1.name"));
    assertThat(f.fieldType().stored(), equalTo(true));
    fieldMapper = docMapper.mappers().getMapper("obj1.name");
    assertThat(fieldMapper.fieldType().stored(), equalTo(true));
    f = doc.getField("obj1.obj2.name");
    assertThat(f.name(), equalTo("obj1.obj2.name"));
    assertThat(f.fieldType().stored(), equalTo(false));
    fieldMapper = docMapper.mappers().getMapper("obj1.obj2.name");
    assertThat(fieldMapper.fieldType().stored(), equalTo(false));
    // verify more complex path_match expressions
    fieldMapper = docMapper.mappers().getMapper("obj3.obj4.prop1");
    assertNotNull(fieldMapper);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) BytesArray(org.elasticsearch.common.bytes.BytesArray) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) IndexService(org.elasticsearch.index.IndexService) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) Document(org.elasticsearch.index.mapper.ParseContext.Document) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) FieldMapper(org.elasticsearch.index.mapper.FieldMapper)

Example 74 with DocumentMapper

use of org.elasticsearch.index.mapper.DocumentMapper in project elasticsearch by elastic.

the class JavaMultiFieldMergeTests method testMergeMultiField.

public void testMergeMultiField() throws Exception {
    String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/test-mapping1.json");
    MapperService mapperService = createIndex("test").mapperService();
    DocumentMapper docMapper = mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE, false);
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name.indexed"), nullValue());
    BytesReference json = XContentFactory.jsonBuilder().startObject().field("name", "some name").endObject().bytes();
    Document doc = docMapper.parse("test", "person", "1", json).rootDoc();
    IndexableField f = doc.getField("name");
    assertThat(f, notNullValue());
    f = doc.getField("name.indexed");
    assertThat(f, nullValue());
    mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/test-mapping2.json");
    docMapper = mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE, false);
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name.indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed2"), nullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed3"), nullValue());
    doc = docMapper.parse("test", "person", "1", json).rootDoc();
    f = doc.getField("name");
    assertThat(f, notNullValue());
    f = doc.getField("name.indexed");
    assertThat(f, notNullValue());
    mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/test-mapping3.json");
    docMapper = mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE, false);
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name.indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed2"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed3"), nullValue());
    mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/test-mapping4.json");
    docMapper = mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE, false);
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name.indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed2"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed3"), notNullValue());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) IndexableField(org.apache.lucene.index.IndexableField) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) Document(org.elasticsearch.index.mapper.ParseContext.Document) MapperService(org.elasticsearch.index.mapper.MapperService)

Example 75 with DocumentMapper

use of org.elasticsearch.index.mapper.DocumentMapper in project elasticsearch by elastic.

the class IdFieldMapperTests method testId.

public void testId() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject().string();
    DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
    ParsedDocument doc = docMapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().endObject().bytes());
    assertThat(doc.rootDoc().get(UidFieldMapper.NAME), notNullValue());
    assertThat(doc.rootDoc().get(IdFieldMapper.NAME), nullValue());
}
Also used : ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent)

Aggregations

DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)83 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)49 IndexService (org.elasticsearch.index.IndexService)29 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)27 IndexableField (org.apache.lucene.index.IndexableField)19 FieldMapper (org.elasticsearch.index.mapper.FieldMapper)15 Matchers.containsString (org.hamcrest.Matchers.containsString)15 Settings (org.elasticsearch.common.settings.Settings)13 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)12 MapperService (org.elasticsearch.index.mapper.MapperService)12 BytesReference (org.elasticsearch.common.bytes.BytesReference)11 DocumentMapperParser (org.elasticsearch.index.mapper.DocumentMapperParser)10 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)9 BytesArray (org.elasticsearch.common.bytes.BytesArray)8 Document (org.elasticsearch.index.mapper.ParseContext.Document)8 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 BytesRef (org.apache.lucene.util.BytesRef)6 ParentFieldMapper (org.elasticsearch.index.mapper.ParentFieldMapper)6