DateFieldMapperTests.java 文件源码

java
阅读 22 收藏 0 点赞 0 评论 0

项目:elasticsearch_my 作者:
/**
 * Test that time zones are correctly parsed by the {@link DateFieldMapper}.
 * There is a known bug with Joda 2.9.4 reported in https://github.com/JodaOrg/joda-time/issues/373.
 */
public void testTimeZoneParsing() throws Exception {
    final String timeZonePattern = "yyyy-MM-dd" + randomFrom("ZZZ", "[ZZZ]", "'['ZZZ']'");

    String mapping = XContentFactory.jsonBuilder().startObject()
            .startObject("type")
                .startObject("properties")
                    .startObject("field")
                        .field("type", "date")
                        .field("format", timeZonePattern)
                    .endObject()
                .endObject()
            .endObject().endObject().string();

    DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
    assertEquals(mapping, mapper.mappingSource().toString());

    final DateTimeZone randomTimeZone = randomBoolean() ? DateTimeZone.forID(randomFrom("UTC", "CET")) : randomDateTimeZone();
    final DateTime randomDate = new DateTime(2016, 03, 11, 0, 0, 0, randomTimeZone);

    ParsedDocument doc = mapper.parse("test", "type", "1", XContentFactory.jsonBuilder()
            .startObject()
                .field("field", DateTimeFormat.forPattern(timeZonePattern).print(randomDate))
            .endObject()
            .bytes());

    IndexableField[] fields = doc.rootDoc().getFields("field");
    assertEquals(2, fields.length);

    assertEquals(randomDate.withZone(DateTimeZone.UTC).getMillis(), fields[0].numericValue().longValue());
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号