Search in sources :

Example 96 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class DocumentParserTests method testDynamicDottedFieldNameObjectWithExistingParent.

public void testDynamicDottedFieldNameObjectWithExistingParent() throws Exception {
    DocumentMapperParser mapperParser = createIndex("test").mapperService().documentMapperParser();
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("foo").field("type", "object").endObject().endObject().endObject().endObject().string();
    DocumentMapper mapper = mapperParser.parse("type", new CompressedXContent(mapping));
    BytesReference bytes = XContentFactory.jsonBuilder().startObject().startObject("foo.bar.baz").field("a", 0).endObject().endObject().bytes();
    ParsedDocument doc = mapper.parse("test", "type", "1", bytes);
    assertEquals(2, doc.rootDoc().getFields("foo.bar.baz.a").length);
    Mapper fooMapper = doc.dynamicMappingsUpdate().root().getMapper("foo");
    assertNotNull(fooMapper);
    assertThat(fooMapper, instanceOf(ObjectMapper.class));
    Mapper barMapper = ((ObjectMapper) fooMapper).getMapper("bar");
    assertNotNull(barMapper);
    assertThat(barMapper, instanceOf(ObjectMapper.class));
    Mapper bazMapper = ((ObjectMapper) barMapper).getMapper("baz");
    assertNotNull(bazMapper);
    assertThat(bazMapper, instanceOf(ObjectMapper.class));
    Mapper aMapper = ((ObjectMapper) bazMapper).getMapper("a");
    assertNotNull(aMapper);
    assertThat(aMapper, instanceOf(NumberFieldMapper.class));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 97 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class DocumentParserTests method testDynamicStrictDottedFieldNameLongArray.

public void testDynamicStrictDottedFieldNameLongArray() throws Exception {
    DocumentMapperParser mapperParser = createIndex("test").mapperService().documentMapperParser();
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").field("dynamic", "strict").endObject().endObject().string();
    DocumentMapper mapper = mapperParser.parse("type", new CompressedXContent(mapping));
    BytesReference bytes = XContentFactory.jsonBuilder().startObject().startArray("foo.bar.baz").value(0).value(1).endArray().endObject().bytes();
    StrictDynamicMappingException exception = expectThrows(StrictDynamicMappingException.class, () -> mapper.parse("test", "type", "1", bytes));
    assertEquals("mapping set to strict, dynamic introduction of [foo] within [type] is not allowed", exception.getMessage());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 98 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class MultiFieldTests method testMultiField.

private void testMultiField(String mapping) throws Exception {
    DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("person", new CompressedXContent(mapping));
    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().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());
    f = doc.getField("object1.multi1");
    assertThat(f.name(), equalTo("object1.multi1"));
    f = doc.getField("object1.multi1.string");
    assertThat(f.name(), equalTo("object1.multi1.string"));
    assertThat(f.binaryValue(), equalTo(new BytesRef("2010-01-01")));
    assertThat(docMapper.mappers().getMapper("name"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name"), instanceOf(TextFieldMapper.class));
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name").fieldType().stored(), equalTo(true));
    assertThat(docMapper.mappers().getMapper("name").fieldType().tokenized(), equalTo(true));
    assertThat(docMapper.mappers().getMapper("name.indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.indexed"), instanceOf(TextFieldMapper.class));
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name.indexed").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name.indexed").fieldType().stored(), equalTo(false));
    assertThat(docMapper.mappers().getMapper("name.indexed").fieldType().tokenized(), equalTo(true));
    assertThat(docMapper.mappers().getMapper("name.not_indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed"), instanceOf(TextFieldMapper.class));
    assertEquals(IndexOptions.NONE, docMapper.mappers().getMapper("name.not_indexed").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name.not_indexed").fieldType().stored(), equalTo(true));
    assertThat(docMapper.mappers().getMapper("name.not_indexed").fieldType().tokenized(), equalTo(true));
    assertThat(docMapper.mappers().getMapper("name.test1"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.test1"), instanceOf(TextFieldMapper.class));
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name.test1").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name.test1").fieldType().stored(), equalTo(true));
    assertThat(docMapper.mappers().getMapper("name.test1").fieldType().tokenized(), equalTo(true));
    assertThat(docMapper.mappers().getMapper("name.test1").fieldType().eagerGlobalOrdinals(), equalTo(true));
    assertThat(docMapper.mappers().getMapper("name.test2"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.test2"), instanceOf(TokenCountFieldMapper.class));
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name.test2").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name.test2").fieldType().stored(), equalTo(true));
    assertThat(docMapper.mappers().getMapper("name.test2").fieldType().tokenized(), equalTo(false));
    assertThat(((TokenCountFieldMapper) docMapper.mappers().getMapper("name.test2")).analyzer(), equalTo("simple"));
    assertThat(((TokenCountFieldMapper) docMapper.mappers().getMapper("name.test2")).analyzer(), equalTo("simple"));
    assertThat(docMapper.mappers().getMapper("object1.multi1"), notNullValue());
    assertThat(docMapper.mappers().getMapper("object1.multi1"), instanceOf(DateFieldMapper.class));
    assertThat(docMapper.mappers().getMapper("object1.multi1.string"), notNullValue());
    assertThat(docMapper.mappers().getMapper("object1.multi1.string"), instanceOf(KeywordFieldMapper.class));
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("object1.multi1.string").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("object1.multi1.string").fieldType().tokenized(), equalTo(false));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) IndexableField(org.apache.lucene.index.IndexableField) DateFieldMapper(org.elasticsearch.index.mapper.DateFieldMapper) BytesArray(org.elasticsearch.common.bytes.BytesArray) KeywordFieldMapper(org.elasticsearch.index.mapper.KeywordFieldMapper) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) TokenCountFieldMapper(org.elasticsearch.index.mapper.TokenCountFieldMapper) Document(org.elasticsearch.index.mapper.ParseContext.Document) BytesRef(org.apache.lucene.util.BytesRef) TextFieldMapper(org.elasticsearch.index.mapper.TextFieldMapper)

Example 99 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class DynamicMappingTests method doTestDefaultFloatingPointMappings.

private void doTestDefaultFloatingPointMappings(DocumentMapper mapper, XContentBuilder builder) throws IOException {
    BytesReference source = builder.startObject().field("foo", // float
    3.2f).field("bar", // double
    3.2d).field("baz", // double that can be accurately represented as a float
    (double) 3.2f).field("quux", // float detected through numeric detection
    "3.2").endObject().bytes();
    ParsedDocument parsedDocument = mapper.parse("index", "type", "id", source);
    Mapping update = parsedDocument.dynamicMappingsUpdate();
    assertNotNull(update);
    assertThat(((FieldMapper) update.root().getMapper("foo")).fieldType().typeName(), equalTo("float"));
    assertThat(((FieldMapper) update.root().getMapper("bar")).fieldType().typeName(), equalTo("float"));
    assertThat(((FieldMapper) update.root().getMapper("baz")).fieldType().typeName(), equalTo("float"));
    assertThat(((FieldMapper) update.root().getMapper("quux")).fieldType().typeName(), equalTo("float"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference)

Example 100 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class JavaMultiFieldMergeTests method testUpgradeFromMultiFieldTypeToMultiFields.

public void testUpgradeFromMultiFieldTypeToMultiFields() 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/upgrade1.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/upgrade2.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/upgrade3.json");
    try {
        mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE, false);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("mapper [name] has different [index] values"));
        assertThat(e.getMessage(), containsString("mapper [name] has different [store] values"));
    }
    // There are conflicts, so the `name.not_indexed3` has not been added
    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());
}
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)

Aggregations

BytesReference (org.elasticsearch.common.bytes.BytesReference)318 Matchers.containsString (org.hamcrest.Matchers.containsString)72 XContentParser (org.elasticsearch.common.xcontent.XContentParser)63 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)61 IOException (java.io.IOException)58 XContentType (org.elasticsearch.common.xcontent.XContentType)50 BytesArray (org.elasticsearch.common.bytes.BytesArray)47 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)37 ArrayList (java.util.ArrayList)30 HashMap (java.util.HashMap)30 Map (java.util.Map)26 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)26 Test (org.junit.Test)25 List (java.util.List)24 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)24 Version (org.elasticsearch.Version)22 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)20 ReleasableBytesReference (org.elasticsearch.common.bytes.ReleasableBytesReference)19 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)18 BytesRef (org.apache.lucene.util.BytesRef)18