Search in sources :

Example 61 with DateTimeFormatter

use of org.joda.time.format.DateTimeFormatter in project lucene-solr by apache.

the class ParsingFieldUpdateProcessorsTest method testParseDateExplicitNotInSchemaSelector.

public void testParseDateExplicitNotInSchemaSelector() throws Exception {
    IndexSchema schema = h.getCore().getLatestSchema();
    assertNull(schema.getFieldOrNull("not_in_schema"));
    String dateString = "2010-11-12T13:14:15.168Z";
    DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime();
    DateTime dateTime = dateTimeFormatter.parseDateTime(dateString);
    SolrInputDocument d = processAdd("parse-date-explicit-not-in-schema-selector-no-run-processor", doc(f("id", "88"), f("not_in_schema", dateString)));
    assertNotNull(d);
    assertTrue(d.getFieldValue("not_in_schema") instanceof Date);
    assertEquals(dateTime.getMillis(), ((Date) d.getFieldValue("not_in_schema")).getTime());
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) IndexSchema(org.apache.solr.schema.IndexSchema) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTime(org.joda.time.DateTime) Date(java.util.Date)

Example 62 with DateTimeFormatter

use of org.joda.time.format.DateTimeFormatter in project lucene-solr by apache.

the class ParsingFieldUpdateProcessorsTest method testParseFrenchDate.

public void testParseFrenchDate() throws Exception {
    IndexSchema schema = h.getCore().getLatestSchema();
    assertNull(schema.getFieldOrNull("not_in_schema"));
    String frenchDateString = "le vendredi 15 janvier 2010";
    String dateString = "2010-01-15T00:00:00.000Z";
    DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime();
    DateTime dateTime = dateTimeFormatter.parseDateTime(dateString);
    SolrInputDocument d = processAdd("parse-french-date-UTC-defaultTimeZone-no-run-processor", doc(f("id", "88"), f("not_in_schema", frenchDateString)));
    assertNotNull(d);
    assertTrue(d.getFieldValue("not_in_schema") instanceof Date);
    assertEquals(dateTime.getMillis(), ((Date) d.getFieldValue("not_in_schema")).getTime());
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) IndexSchema(org.apache.solr.schema.IndexSchema) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTime(org.joda.time.DateTime) Date(java.util.Date)

Example 63 with DateTimeFormatter

use of org.joda.time.format.DateTimeFormatter in project lucene-solr by apache.

the class ParsingFieldUpdateProcessorsTest method testParseDateRoundTrip.

public void testParseDateRoundTrip() throws Exception {
    IndexSchema schema = h.getCore().getLatestSchema();
    // should match "*_dt" dynamic field
    assertNotNull(schema.getFieldOrNull("date_dt"));
    String dateString = "2010-11-12T13:14:15.168Z";
    SolrInputDocument d = processAdd("parse-date", doc(f("id", "9"), f("date_dt", dateString)));
    assertNotNull(d);
    DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime();
    DateTime dateTime = dateTimeFormatter.parseDateTime(dateString);
    assertTrue(d.getFieldValue("date_dt") instanceof Date);
    assertEquals(dateTime.getMillis(), ((Date) d.getFieldValue("date_dt")).getTime());
    assertU(commit());
    assertQ(req("id:9"), "//date[@name='date_dt'][.='" + dateString + "']");
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) IndexSchema(org.apache.solr.schema.IndexSchema) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTime(org.joda.time.DateTime) Date(java.util.Date)

Example 64 with DateTimeFormatter

use of org.joda.time.format.DateTimeFormatter in project lucene-solr by apache.

the class ParsingFieldUpdateProcessorsTest method testCascadingParsers.

public void testCascadingParsers() throws Exception {
    IndexSchema schema = h.getCore().getLatestSchema();
    final String fieldName = "not_in_schema";
    assertNull(schema.getFieldOrNull(fieldName));
    SolrInputDocument d = null;
    String chain = "cascading-parsers-no-run-processor";
    Map<Boolean, String> booleans = new HashMap<>();
    booleans.put(true, "truE");
    booleans.put(false, "False");
    d = processAdd(chain, doc(f("id", "341"), f(fieldName, booleans.values())));
    assertNotNull(d);
    for (Object o : d.getFieldValues(fieldName)) {
        assertTrue(o instanceof Boolean);
        booleans.remove(o);
    }
    assertTrue(booleans.isEmpty());
    Map<Integer, String> ints = new HashMap<>();
    ints.put(2, "2");
    ints.put(50928, "50928");
    ints.put(86942008, "86,942,008");
    d = processAdd(chain, doc(f("id", "333"), f(fieldName, ints.values())));
    assertNotNull(d);
    for (Object o : d.getFieldValues(fieldName)) {
        assertTrue(o instanceof Integer);
        ints.remove(o);
    }
    assertTrue(ints.isEmpty());
    Map<Long, String> longs = new HashMap<>();
    longs.put(2L, "2");
    longs.put(50928L, "50928");
    longs.put(86942008987654L, "86,942,008,987,654");
    d = processAdd(chain, doc(f("id", "342"), f(fieldName, longs.values())));
    assertNotNull(d);
    for (Object o : d.getFieldValues(fieldName)) {
        assertTrue(o instanceof Long);
        longs.remove(o);
    }
    assertTrue(longs.isEmpty());
    /*
    // Disabling this test because unlike Integer/Long, Float parsing can perform
    // rounding to make values fit.  See 
    Map<Float,String> floats = new HashMap<Float,String>();
    floats.put(2.0, "2.");
    floats.put(509.28, "509.28");
    floats.put(86942.008, "86,942.008");
    d = processAdd(chain, doc(f("id", "342"), f(fieldName, floats.values())));
    assertNotNull(d);
    for (Object o : d.getFieldValues(fieldName)) {
      assertTrue(o instanceof float);
      longs.remove(o);
    }
    */
    Map<Double, String> doubles = new HashMap<>();
    doubles.put(2.0, "2.");
    doubles.put(509.28, "509.28");
    doubles.put(86942.008, "86,942.008");
    d = processAdd(chain, doc(f("id", "342"), f(fieldName, doubles.values())));
    assertNotNull(d);
    for (Object o : d.getFieldValues(fieldName)) {
        assertTrue(o instanceof Double);
        longs.remove(o);
    }
    DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateOptionalTimeParser().withZoneUTC();
    Map<Date, String> dates = new HashMap<>();
    String[] dateStrings = { "2020-05-13T18:47", "1989-12-14", "1682-07-22T18:33:00.000Z" };
    for (String dateString : dateStrings) {
        dates.put(dateTimeFormatter.parseDateTime(dateString).toDate(), dateString);
    }
    d = processAdd(chain, doc(f("id", "343"), f(fieldName, dates.values())));
    assertNotNull(d);
    for (Object o : d.getFieldValues(fieldName)) {
        assertTrue(o instanceof Date);
        dates.remove(o);
    }
    assertTrue(dates.isEmpty());
    // preserve order
    Map<Double, String> mixedLongsAndDoubles = new LinkedHashMap<>();
    mixedLongsAndDoubles.put(85.0, "85");
    mixedLongsAndDoubles.put(2.94423E-9, "2.94423E-9");
    mixedLongsAndDoubles.put(2894518.0, "2,894,518");
    mixedLongsAndDoubles.put(48794721.937, "48,794,721.937");
    d = processAdd(chain, doc(f("id", "344"), f(fieldName, mixedLongsAndDoubles.values())));
    assertNotNull(d);
    for (Object o : d.getFieldValues(fieldName)) {
        assertTrue(o instanceof Double);
        mixedLongsAndDoubles.remove(o);
    }
    assertTrue(mixedLongsAndDoubles.isEmpty());
    Set<String> mixed = new HashSet<>();
    mixed.add("true");
    mixed.add("1682-07-22T18:33:00.000Z");
    mixed.add("2,894,518");
    mixed.add("308,393,131,379,900");
    mixed.add("48,794,721.937");
    d = processAdd(chain, doc(f("id", "345"), f(fieldName, mixed)));
    assertNotNull(d);
    for (Object o : d.getFieldValues(fieldName)) {
        assertTrue(o instanceof String);
    }
    // preserve order
    Map<Double, Object> mixedDoubles = new LinkedHashMap<>();
    mixedDoubles.put(85.0, "85");
    // Double-typed field value
    mixedDoubles.put(2.94423E-9, 2.94423E-9);
    mixedDoubles.put(2894518.0, "2,894,518");
    mixedDoubles.put(48794721.937, "48,794,721.937");
    d = processAdd(chain, doc(f("id", "3391"), f(fieldName, mixedDoubles.values())));
    assertNotNull(d);
    for (Object o : d.getFieldValues(fieldName)) {
        assertTrue(o instanceof Double);
        mixedDoubles.remove(o);
    }
    assertTrue(mixedDoubles.isEmpty());
    // preserve order
    Map<Integer, Object> mixedInts = new LinkedHashMap<>();
    mixedInts.put(85, "85");
    // Integer-typed field value
    mixedInts.put(294423, 294423);
    mixedInts.put(-2894518, "-2,894,518");
    mixedInts.put(1879472193, "1,879,472,193");
    d = processAdd(chain, doc(f("id", "3392"), f(fieldName, mixedInts.values())));
    assertNotNull(d);
    for (Object o : d.getFieldValues(fieldName)) {
        assertTrue(o instanceof Integer);
        mixedInts.remove(o);
    }
    assertTrue(mixedInts.isEmpty());
    // preserve order
    Map<Long, Object> mixedLongs = new LinkedHashMap<>();
    mixedLongs.put(85L, "85");
    // Long-typed field value
    mixedLongs.put(42944233L, 42944233L);
    mixedLongs.put(2894518L, "2,894,518");
    mixedLongs.put(48794721937L, "48,794,721,937");
    d = processAdd(chain, doc(f("id", "3393"), f(fieldName, mixedLongs.values())));
    assertNotNull(d);
    for (Object o : d.getFieldValues(fieldName)) {
        assertTrue(o instanceof Long);
        mixedLongs.remove(o);
    }
    assertTrue(mixedLongs.isEmpty());
    // preserve order
    Map<Boolean, Object> mixedBooleans = new LinkedHashMap<>();
    mixedBooleans.put(true, "true");
    // Boolean-typed field value
    mixedBooleans.put(false, false);
    mixedBooleans.put(false, "false");
    mixedBooleans.put(true, "true");
    d = processAdd(chain, doc(f("id", "3394"), f(fieldName, mixedBooleans.values())));
    assertNotNull(d);
    for (Object o : d.getFieldValues(fieldName)) {
        assertTrue(o instanceof Boolean);
        mixedBooleans.remove(o);
    }
    assertTrue(mixedBooleans.isEmpty());
    dateTimeFormatter = ISODateTimeFormat.dateOptionalTimeParser().withZoneUTC();
    Map<Date, Object> mixedDates = new HashMap<>();
    dateStrings = new String[] { "2020-05-13T18:47", "1989-12-14", "1682-07-22T18:33:00.000Z" };
    for (String dateString : dateStrings) {
        mixedDates.put(dateTimeFormatter.parseDateTime(dateString).toDate(), dateString);
    }
    Date extraDate = dateTimeFormatter.parseDateTime("2003-04-24").toDate();
    // Date-typed field value
    mixedDates.put(extraDate, extraDate);
    d = processAdd(chain, doc(f("id", "3395"), f(fieldName, mixedDates.values())));
    assertNotNull(d);
    for (Object o : d.getFieldValues(fieldName)) {
        assertTrue(o instanceof Date);
        mixedDates.remove(o);
    }
    assertTrue(mixedDates.isEmpty());
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap) SolrInputDocument(org.apache.solr.common.SolrInputDocument) HashSet(java.util.HashSet) Date(java.util.Date) IndexSchema(org.apache.solr.schema.IndexSchema) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Example 65 with DateTimeFormatter

use of org.joda.time.format.DateTimeFormatter in project lucene-solr by apache.

the class ParseDateFieldUpdateProcessorFactory method getInstance.

@Override
public UpdateRequestProcessor getInstance(SolrQueryRequest req, SolrQueryResponse rsp, UpdateRequestProcessor next) {
    return new AllValuesOrNoneFieldMutatingUpdateProcessor(getSelector(), next) {

        @Override
        protected Object mutateValue(Object srcVal) {
            if (srcVal instanceof CharSequence) {
                String srcStringVal = srcVal.toString();
                for (Map.Entry<String, DateTimeFormatter> format : formats.entrySet()) {
                    DateTimeFormatter parser = format.getValue();
                    try {
                        DateTime dateTime = parser.parseDateTime(srcStringVal);
                        return dateTime.withZone(DateTimeZone.UTC).toDate();
                    } catch (IllegalArgumentException e) {
                        log.debug("value '{}' is not parseable with format '{}'", new Object[] { srcStringVal, format.getKey() });
                    }
                }
                log.debug("value '{}' was not parsed by any configured format, thus was not mutated", srcStringVal);
                return SKIP_FIELD_VALUE_LIST_SINGLETON;
            }
            if (srcVal instanceof Date) {
                return srcVal;
            }
            return SKIP_FIELD_VALUE_LIST_SINGLETON;
        }
    };
}
Also used : LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTime(org.joda.time.DateTime) Date(java.util.Date)

Aggregations

DateTimeFormatter (org.joda.time.format.DateTimeFormatter)209 DateTime (org.joda.time.DateTime)95 Date (java.util.Date)40 Test (org.junit.Test)25 DateTimeZone (org.joda.time.DateTimeZone)19 ArrayList (java.util.ArrayList)16 HashMap (java.util.HashMap)16 SolrInputDocument (org.apache.solr.common.SolrInputDocument)12 IndexSchema (org.apache.solr.schema.IndexSchema)12 DateTimeFormatterBuilder (org.joda.time.format.DateTimeFormatterBuilder)12 CswSourceConfiguration (org.codice.ddf.spatial.ogc.csw.catalog.common.CswSourceConfiguration)10 IOException (java.io.IOException)9 Calendar (java.util.Calendar)8 Map (java.util.Map)8 DatasetConfigDTO (com.linkedin.thirdeye.datalayer.dto.DatasetConfigDTO)7 FormatDateTimeFormatter (org.elasticsearch.common.joda.FormatDateTimeFormatter)7 Test (org.testng.annotations.Test)7 TimeSpec (com.linkedin.thirdeye.api.TimeSpec)6 FilterType (net.opengis.filter.v_1_1_0.FilterType)6 Deployment (org.activiti.engine.test.Deployment)6