Search in sources :

Example 26 with CompressedXContent

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

the class BooleanFieldMapperTests method testParsesEs6BooleansStrict.

public void testParsesEs6BooleansStrict() throws IOException {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "boolean").endObject().endObject().endObject().endObject().string();
    DocumentMapper defaultMapper = parser.parse("type", new CompressedXContent(mapping));
    BytesReference source = XContentFactory.jsonBuilder().startObject().field("field", randomFrom("off", "no", "0", "on", "yes", "1")).endObject().bytes();
    MapperParsingException ex = expectThrows(MapperParsingException.class, () -> defaultMapper.parse("test", "type", "1", source));
    assertEquals("failed to parse [field]", ex.getMessage());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 27 with CompressedXContent

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

the class BooleanFieldMapperTests method testSerialization.

public void testSerialization() throws IOException {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "boolean").endObject().endObject().endObject().endObject().string();
    DocumentMapper defaultMapper = parser.parse("type", new CompressedXContent(mapping));
    FieldMapper mapper = defaultMapper.mappers().getMapper("field");
    XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
    mapper.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.endObject();
    assertEquals("{\"field\":{\"type\":\"boolean\"}}", builder.string());
    // now change some parameters
    mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "boolean").field("doc_values", "false").field("null_value", true).endObject().endObject().endObject().endObject().string();
    defaultMapper = parser.parse("type", new CompressedXContent(mapping));
    mapper = defaultMapper.mappers().getMapper("field");
    builder = XContentFactory.jsonBuilder().startObject();
    mapper.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.endObject();
    assertEquals("{\"field\":{\"type\":\"boolean\",\"doc_values\":false,\"null_value\":true}}", builder.string());
}
Also used : CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 28 with CompressedXContent

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

the class BooleanFieldMapperTests method testDefaults.

public void testDefaults() throws IOException {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "boolean").endObject().endObject().endObject().endObject().string();
    DocumentMapper defaultMapper = parser.parse("type", new CompressedXContent(mapping));
    ParsedDocument doc = defaultMapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().field("field", true).endObject().bytes());
    try (Directory dir = new RAMDirectory();
        IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())))) {
        w.addDocuments(doc.docs());
        try (DirectoryReader reader = DirectoryReader.open(w)) {
            final LeafReader leaf = reader.leaves().get(0).reader();
            // boolean fields are indexed and have doc values by default
            assertEquals(new BytesRef("T"), leaf.terms("field").iterator().next());
            SortedNumericDocValues values = leaf.getSortedNumericDocValues("field");
            assertNotNull(values);
            values.setDocument(0);
            assertEquals(1, values.count());
            assertEquals(1, values.valueAt(0));
        }
    }
}
Also used : SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) LeafReader(org.apache.lucene.index.LeafReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) Matchers.containsString(org.hamcrest.Matchers.containsString) RAMDirectory(org.apache.lucene.store.RAMDirectory) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) BytesRef(org.apache.lucene.util.BytesRef) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 29 with CompressedXContent

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

the class BooleanFieldMapperTests method testParsesPreEs6BooleansLenient.

public void testParsesPreEs6BooleansLenient() throws IOException {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field1").field("type", "boolean").endObject().startObject("field2").field("type", "boolean").endObject().endObject().endObject().endObject().string();
    DocumentMapper defaultMapper = preEs6Parser.parse("type", new CompressedXContent(mapping));
    String falsy = randomFrom("false", "off", "no", "0");
    String truthy = randomFrom("true", "on", "yes", "1");
    ParsedDocument parsedDoc = defaultMapper.parse("legacy", "type", "1", XContentFactory.jsonBuilder().startObject().field("field1", falsy).field("field2", truthy).endObject().bytes());
    Document doc = parsedDoc.rootDoc();
    assertEquals("F", doc.getField("field1").stringValue());
    assertEquals("T", doc.getField("field2").stringValue());
    List<String> expectedDeprecationWarnings = new ArrayList<>();
    if (Booleans.isBoolean(falsy) == false) {
        expectedDeprecationWarnings.add("Expected a boolean for property [field1] but got [" + falsy + "]");
    }
    if (Booleans.isBoolean(truthy) == false) {
        expectedDeprecationWarnings.add("Expected a boolean for property [field2] but got [" + truthy + "]");
    }
    if (expectedDeprecationWarnings.isEmpty() == false) {
        assertWarnings(expectedDeprecationWarnings.toArray(new String[1]));
    }
}
Also used : CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) Document(org.elasticsearch.index.mapper.ParseContext.Document)

Example 30 with CompressedXContent

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

the class BooleanFieldMapperTests method testMultiFields.

public void testMultiFields() throws IOException {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "boolean").startObject("fields").startObject("as_string").field("type", "keyword").endObject().endObject().endObject().endObject().endObject().endObject().string();
    DocumentMapper mapper = indexService.mapperService().merge("type", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE, false);
    assertEquals(mapping, mapper.mappingSource().toString());
    BytesReference source = XContentFactory.jsonBuilder().startObject().field("field", false).endObject().bytes();
    ParsedDocument doc = mapper.parse("test", "type", "1", source);
    assertNotNull(doc.rootDoc().getField("field.as_string"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

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