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());
}
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());
}
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));
}
}
}
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]));
}
}
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"));
}
Aggregations