Search in sources :

Example 66 with DocumentMapper

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

the class SizeMappingTests method testSizeDisabled.

public void testSizeDisabled() throws Exception {
    IndexService service = createIndex("test", Settings.EMPTY, "type", "_size", "enabled=false");
    DocumentMapper docMapper = service.mapperService().documentMapper("type");
    BytesReference source = XContentFactory.jsonBuilder().startObject().field("field", "value").endObject().bytes();
    ParsedDocument doc = docMapper.parse(SourceToParse.source("test", "type", "1", source, XContentType.JSON));
    assertThat(doc.rootDoc().getField("_size"), nullValue());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) IndexService(org.elasticsearch.index.IndexService) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper)

Example 67 with DocumentMapper

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

the class MultiFieldTests method testBuildThenParse.

public void testBuildThenParse() throws Exception {
    IndexService indexService = createIndex("test");
    DocumentMapper builderDocMapper = new DocumentMapper.Builder(new RootObjectMapper.Builder("person").add(new TextFieldMapper.Builder("name").store(true).addMultiField(new TextFieldMapper.Builder("indexed").index(true).tokenized(true)).addMultiField(new TextFieldMapper.Builder("not_indexed").index(false).store(true))), indexService.mapperService()).build(indexService.mapperService());
    String builtMapping = builderDocMapper.mappingSource().string();
    //        System.out.println(builtMapping);
    // reparse it
    DocumentMapper docMapper = indexService.mapperService().documentMapperParser().parse("person", new CompressedXContent(builtMapping));
    BytesReference json = new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/mapper/multifield/test-data.json"));
    Document doc = docMapper.parse("test", "person", "1", json).rootDoc();
    IndexableField f = doc.getField("name");
    assertThat(f.name(), equalTo("name"));
    assertThat(f.stringValue(), equalTo("some name"));
    assertThat(f.fieldType().stored(), equalTo(true));
    assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions());
    f = doc.getField("name.indexed");
    assertThat(f.name(), equalTo("name.indexed"));
    assertThat(f.stringValue(), equalTo("some name"));
    assertThat(f.fieldType().tokenized(), equalTo(true));
    assertThat(f.fieldType().stored(), equalTo(false));
    assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions());
    f = doc.getField("name.not_indexed");
    assertThat(f.name(), equalTo("name.not_indexed"));
    assertThat(f.stringValue(), equalTo("some name"));
    assertThat(f.fieldType().stored(), equalTo(true));
    assertEquals(IndexOptions.NONE, f.fieldType().indexOptions());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) BytesArray(org.elasticsearch.common.bytes.BytesArray) IndexService(org.elasticsearch.index.IndexService) RootObjectMapper(org.elasticsearch.index.mapper.RootObjectMapper) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) Document(org.elasticsearch.index.mapper.ParseContext.Document) IndexableField(org.apache.lucene.index.IndexableField) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) TextFieldMapper(org.elasticsearch.index.mapper.TextFieldMapper)

Example 68 with DocumentMapper

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

the class NullValueObjectMappingTests method testNullValueObject.

public void testNullValueObject() throws IOException {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("obj1").field("type", "object").endObject().endObject().endObject().endObject().string();
    DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
    ParsedDocument doc = defaultMapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().startObject("obj1").endObject().field("value1", "test1").endObject().bytes());
    assertThat(doc.rootDoc().get("value1"), equalTo("test1"));
    doc = defaultMapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().nullField("obj1").field("value1", "test1").endObject().bytes());
    assertThat(doc.rootDoc().get("value1"), equalTo("test1"));
    doc = defaultMapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().startObject("obj1").field("field", "value").endObject().field("value1", "test1").endObject().bytes());
    assertThat(doc.rootDoc().get("obj1.field"), equalTo("value"));
    assertThat(doc.rootDoc().get("value1"), equalTo("test1"));
}
Also used : ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent)

Example 69 with DocumentMapper

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

the class RoutingFieldMapperTests method testRoutingMapper.

public void testRoutingMapper() 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(SourceToParse.source("test", "type", "1", XContentFactory.jsonBuilder().startObject().field("field", "value").endObject().bytes(), XContentType.JSON).routing("routing_value"));
    assertThat(doc.rootDoc().get("_routing"), equalTo("routing_value"));
    assertThat(doc.rootDoc().get("field"), equalTo("value"));
}
Also used : ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent)

Example 70 with DocumentMapper

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

the class RoutingFieldMapperTests method testIncludeInObjectNotAllowed.

public void testIncludeInObjectNotAllowed() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject().string();
    DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
    try {
        docMapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().field("_routing", "foo").endObject().bytes());
        fail("Expected failure to parse metadata field");
    } catch (MapperParsingException e) {
        assertTrue(e.getMessage(), e.getMessage().contains("Field [_routing] is a metadata field and cannot be added inside a document"));
    }
}
Also used : MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) 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