Search in sources :

Example 11 with FormatDateTimeFormatter

use of org.elasticsearch.common.joda.FormatDateTimeFormatter in project elasticsearch by elastic.

the class DocumentParser method createBuilderFromDynamicValue.

private static Mapper.Builder<?, ?> createBuilderFromDynamicValue(final ParseContext context, XContentParser.Token token, String currentFieldName) throws IOException {
    if (token == XContentParser.Token.VALUE_STRING) {
        String text = context.parser().text();
        boolean parseableAsLong = false;
        try {
            Long.parseLong(text);
            parseableAsLong = true;
        } catch (NumberFormatException e) {
        // not a long number
        }
        boolean parseableAsDouble = false;
        try {
            Double.parseDouble(text);
            parseableAsDouble = true;
        } catch (NumberFormatException e) {
        // not a double number
        }
        if (parseableAsLong && context.root().numericDetection()) {
            Mapper.Builder builder = context.root().findTemplateBuilder(context, currentFieldName, XContentFieldType.LONG);
            if (builder == null) {
                builder = newLongBuilder(currentFieldName, Version.indexCreated(context.indexSettings()));
            }
            return builder;
        } else if (parseableAsDouble && context.root().numericDetection()) {
            Mapper.Builder builder = context.root().findTemplateBuilder(context, currentFieldName, XContentFieldType.DOUBLE);
            if (builder == null) {
                builder = newFloatBuilder(currentFieldName, Version.indexCreated(context.indexSettings()));
            }
            return builder;
        } else if (parseableAsLong == false && parseableAsDouble == false && context.root().dateDetection()) {
            // `epoch_millis` or `YYYY`
            for (FormatDateTimeFormatter dateTimeFormatter : context.root().dynamicDateTimeFormatters()) {
                try {
                    dateTimeFormatter.parser().parseMillis(text);
                } catch (IllegalArgumentException e) {
                    // failure to parse this, continue
                    continue;
                }
                Mapper.Builder builder = context.root().findTemplateBuilder(context, currentFieldName, XContentFieldType.DATE);
                if (builder == null) {
                    builder = newDateBuilder(currentFieldName, dateTimeFormatter, Version.indexCreated(context.indexSettings()));
                }
                if (builder instanceof DateFieldMapper.Builder) {
                    DateFieldMapper.Builder dateBuilder = (DateFieldMapper.Builder) builder;
                    if (dateBuilder.isDateTimeFormatterSet() == false) {
                        dateBuilder.dateTimeFormatter(dateTimeFormatter);
                    }
                }
                return builder;
            }
        }
        Mapper.Builder builder = context.root().findTemplateBuilder(context, currentFieldName, XContentFieldType.STRING);
        if (builder == null) {
            builder = new TextFieldMapper.Builder(currentFieldName).addMultiField(new KeywordFieldMapper.Builder("keyword").ignoreAbove(256));
        }
        return builder;
    } else if (token == XContentParser.Token.VALUE_NUMBER) {
        XContentParser.NumberType numberType = context.parser().numberType();
        if (numberType == XContentParser.NumberType.INT || numberType == XContentParser.NumberType.LONG) {
            Mapper.Builder builder = context.root().findTemplateBuilder(context, currentFieldName, XContentFieldType.LONG);
            if (builder == null) {
                builder = newLongBuilder(currentFieldName, Version.indexCreated(context.indexSettings()));
            }
            return builder;
        } else if (numberType == XContentParser.NumberType.FLOAT || numberType == XContentParser.NumberType.DOUBLE) {
            Mapper.Builder builder = context.root().findTemplateBuilder(context, currentFieldName, XContentFieldType.DOUBLE);
            if (builder == null) {
                // no templates are defined, we use float by default instead of double
                // since this is much more space-efficient and should be enough most of
                // the time
                builder = newFloatBuilder(currentFieldName, Version.indexCreated(context.indexSettings()));
            }
            return builder;
        }
    } else if (token == XContentParser.Token.VALUE_BOOLEAN) {
        Mapper.Builder builder = context.root().findTemplateBuilder(context, currentFieldName, XContentFieldType.BOOLEAN);
        if (builder == null) {
            builder = new BooleanFieldMapper.Builder(currentFieldName);
        }
        return builder;
    } else if (token == XContentParser.Token.VALUE_EMBEDDED_OBJECT) {
        Mapper.Builder builder = context.root().findTemplateBuilder(context, currentFieldName, XContentFieldType.BINARY);
        if (builder == null) {
            builder = new BinaryFieldMapper.Builder(currentFieldName);
        }
        return builder;
    } else {
        Mapper.Builder builder = context.root().findTemplateBuilder(context, currentFieldName, XContentFieldType.STRING);
        if (builder != null) {
            return builder;
        }
    }
    // TODO how do we identify dynamically that its a binary value?
    throw new IllegalStateException("Can't handle serializing a dynamic type with content token [" + token + "] and field name [" + currentFieldName + "]");
}
Also used : FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter)

Example 12 with FormatDateTimeFormatter

use of org.elasticsearch.common.joda.FormatDateTimeFormatter in project elasticsearch by elastic.

the class ExtendedBoundsTests method testParseAndValidate.

public void testParseAndValidate() {
    long now = randomLong();
    Settings indexSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1).build();
    SearchContext context = mock(SearchContext.class);
    QueryShardContext qsc = new QueryShardContext(0, new IndexSettings(IndexMetaData.builder("foo").settings(indexSettings).build(), indexSettings), null, null, null, null, null, xContentRegistry(), null, null, () -> now);
    when(context.getQueryShardContext()).thenReturn(qsc);
    FormatDateTimeFormatter formatter = Joda.forPattern("dateOptionalTime");
    DocValueFormat format = new DocValueFormat.DateTime(formatter, DateTimeZone.UTC);
    ExtendedBounds expected = randomParsedExtendedBounds();
    ExtendedBounds parsed = unparsed(expected).parseAndValidate("test", context, format);
    // parsed won't *equal* expected because equal includes the String parts
    assertEquals(expected.getMin(), parsed.getMin());
    assertEquals(expected.getMax(), parsed.getMax());
    parsed = new ExtendedBounds("now", null).parseAndValidate("test", context, format);
    assertEquals(now, (long) parsed.getMin());
    assertNull(parsed.getMax());
    parsed = new ExtendedBounds(null, "now").parseAndValidate("test", context, format);
    assertNull(parsed.getMin());
    assertEquals(now, (long) parsed.getMax());
    SearchParseException e = expectThrows(SearchParseException.class, () -> new ExtendedBounds(100L, 90L).parseAndValidate("test", context, format));
    assertEquals("[extended_bounds.min][100] cannot be greater than [extended_bounds.max][90] for histogram aggregation [test]", e.getMessage());
    e = expectThrows(SearchParseException.class, () -> unparsed(new ExtendedBounds(100L, 90L)).parseAndValidate("test", context, format));
    assertEquals("[extended_bounds.min][100] cannot be greater than [extended_bounds.max][90] for histogram aggregation [test]", e.getMessage());
}
Also used : SearchParseException(org.elasticsearch.search.SearchParseException) FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter) DocValueFormat(org.elasticsearch.search.DocValueFormat) IndexSettings(org.elasticsearch.index.IndexSettings) SearchContext(org.elasticsearch.search.internal.SearchContext) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings)

Example 13 with FormatDateTimeFormatter

use of org.elasticsearch.common.joda.FormatDateTimeFormatter in project elasticsearch by elastic.

the class SimpleJodaTests method testForInvalidDatesInEpochMillis.

public void testForInvalidDatesInEpochMillis() {
    FormatDateTimeFormatter formatter = Joda.forPattern("epoch_millis");
    try {
        formatter.parser().parseDateTime(randomFrom("invalid date", "12345678901234567890"));
        fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("Invalid format"));
    }
}
Also used : FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter)

Example 14 with FormatDateTimeFormatter

use of org.elasticsearch.common.joda.FormatDateTimeFormatter in project elasticsearch by elastic.

the class SimpleJodaTests method testThatEpochsCanBeParsed.

public void testThatEpochsCanBeParsed() {
    boolean parseMilliSeconds = randomBoolean();
    // epoch: 1433144433655 => date: Mon Jun  1 09:40:33.655 CEST 2015
    FormatDateTimeFormatter formatter = Joda.forPattern(parseMilliSeconds ? "epoch_millis" : "epoch_second");
    DateTime dateTime = formatter.parser().parseDateTime(parseMilliSeconds ? "1433144433655" : "1433144433");
    assertThat(dateTime.getYear(), is(2015));
    assertThat(dateTime.getDayOfMonth(), is(1));
    assertThat(dateTime.getMonthOfYear(), is(6));
    // utc timezone, +2 offset due to CEST
    assertThat(dateTime.getHourOfDay(), is(7));
    assertThat(dateTime.getMinuteOfHour(), is(40));
    assertThat(dateTime.getSecondOfMinute(), is(33));
    if (parseMilliSeconds) {
        assertThat(dateTime.getMillisOfSecond(), is(655));
    } else {
        assertThat(dateTime.getMillisOfSecond(), is(0));
    }
}
Also used : FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter) DateTime(org.joda.time.DateTime) LocalDateTime(org.joda.time.LocalDateTime) MutableDateTime(org.joda.time.MutableDateTime)

Example 15 with FormatDateTimeFormatter

use of org.elasticsearch.common.joda.FormatDateTimeFormatter in project elasticsearch by elastic.

the class SimpleJodaTests method testThatEpochParserIsIdempotent.

public void testThatEpochParserIsIdempotent() {
    FormatDateTimeFormatter formatter = Joda.forPattern("epoch_millis");
    DateTime dateTime = formatter.parser().parseDateTime("1234567890123");
    assertThat(dateTime.getMillis(), is(1234567890123L));
    dateTime = formatter.printer().parseDateTime("1234567890456");
    assertThat(dateTime.getMillis(), is(1234567890456L));
    dateTime = formatter.parser().parseDateTime("1234567890789");
    assertThat(dateTime.getMillis(), is(1234567890789L));
    dateTime = formatter.parser().parseDateTime("1234567890123456789");
    assertThat(dateTime.getMillis(), is(1234567890123456789L));
    FormatDateTimeFormatter secondsFormatter = Joda.forPattern("epoch_second");
    DateTime secondsDateTime = secondsFormatter.parser().parseDateTime("1234567890");
    assertThat(secondsDateTime.getMillis(), is(1234567890000L));
    secondsDateTime = secondsFormatter.printer().parseDateTime("1234567890");
    assertThat(secondsDateTime.getMillis(), is(1234567890000L));
    secondsDateTime = secondsFormatter.parser().parseDateTime("1234567890");
    assertThat(secondsDateTime.getMillis(), is(1234567890000L));
    secondsDateTime = secondsFormatter.parser().parseDateTime("1234567890123456");
    assertThat(secondsDateTime.getMillis(), is(1234567890123456000L));
}
Also used : FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter) DateTime(org.joda.time.DateTime) LocalDateTime(org.joda.time.LocalDateTime) MutableDateTime(org.joda.time.MutableDateTime)

Aggregations

FormatDateTimeFormatter (org.elasticsearch.common.joda.FormatDateTimeFormatter)19 DateTime (org.joda.time.DateTime)3 LocalDateTime (org.joda.time.LocalDateTime)3 MutableDateTime (org.joda.time.MutableDateTime)3 Settings (org.elasticsearch.common.settings.Settings)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)2 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 InetAddress (java.net.InetAddress)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 IndexSettings (org.elasticsearch.index.IndexSettings)1 QueryShardContext (org.elasticsearch.index.query.QueryShardContext)1 DocValueFormat (org.elasticsearch.search.DocValueFormat)1 SearchParseException (org.elasticsearch.search.SearchParseException)1 SearchContext (org.elasticsearch.search.internal.SearchContext)1 Instant (org.joda.time.Instant)1