Search in sources :

Example 1 with ReadableDateTime

use of org.joda.time.ReadableDateTime in project elasticsearch by elastic.

the class ScriptDocValuesDatesTests method test.

public void test() {
    long[][] values = new long[between(3, 10)][];
    ReadableDateTime[][] expectedDates = new ReadableDateTime[values.length][];
    for (int d = 0; d < values.length; d++) {
        values[d] = new long[randomBoolean() ? randomBoolean() ? 0 : 1 : between(2, 100)];
        expectedDates[d] = new ReadableDateTime[values[d].length];
        for (int i = 0; i < values[d].length; i++) {
            expectedDates[d][i] = new DateTime(randomNonNegativeLong(), DateTimeZone.UTC);
            values[d][i] = expectedDates[d][i].getMillis();
        }
    }
    Dates dates = wrap(values);
    for (int round = 0; round < 10; round++) {
        int d = between(0, values.length - 1);
        dates.setNextDocId(d);
        assertEquals(expectedDates[d].length > 0 ? expectedDates[d][0] : new DateTime(0, DateTimeZone.UTC), dates.getValue());
        assertEquals(values[d].length, dates.size());
        for (int i = 0; i < values[d].length; i++) {
            assertEquals(expectedDates[d][i], dates.get(i));
        }
        Exception e = expectThrows(UnsupportedOperationException.class, () -> dates.add(new DateTime()));
        assertEquals("doc values are unmodifiable", e.getMessage());
    }
}
Also used : ReadableDateTime(org.joda.time.ReadableDateTime) Dates(org.elasticsearch.index.fielddata.ScriptDocValues.Dates) ReadableDateTime(org.joda.time.ReadableDateTime) DateTime(org.joda.time.DateTime)

Example 2 with ReadableDateTime

use of org.joda.time.ReadableDateTime in project joda-time by JodaOrg.

the class TestConverterManager method testGetPartialConverterOKMultipleMatches.

public void testGetPartialConverterOKMultipleMatches() {
    PartialConverter c = new PartialConverter() {

        public int[] getPartialValues(ReadablePartial partial, Object object, Chronology chrono) {
            return null;
        }

        public int[] getPartialValues(ReadablePartial partial, Object object, Chronology chrono, DateTimeFormatter parser) {
            return null;
        }

        public Chronology getChronology(Object object, DateTimeZone zone) {
            return null;
        }

        public Chronology getChronology(Object object, Chronology chrono) {
            return null;
        }

        public Class getSupportedType() {
            return ReadableDateTime.class;
        }
    };
    try {
        ConverterManager.getInstance().addPartialConverter(c);
        PartialConverter ok = ConverterManager.getInstance().getPartialConverter(new DateTime());
        // ReadableDateTime and ReadablePartial both match, but RI discarded as less specific
        assertEquals(ReadableDateTime.class, ok.getSupportedType());
    } finally {
        ConverterManager.getInstance().removePartialConverter(c);
    }
    assertEquals(PARTIAL_SIZE, ConverterManager.getInstance().getPartialConverters().length);
}
Also used : ReadablePartial(org.joda.time.ReadablePartial) ReadableDateTime(org.joda.time.ReadableDateTime) Chronology(org.joda.time.Chronology) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) ReadableDateTime(org.joda.time.ReadableDateTime)

Example 3 with ReadableDateTime

use of org.joda.time.ReadableDateTime in project elasticsearch by elastic.

the class ScriptDocValuesLongsTests method testDates.

public void testDates() {
    long[][] values = new long[between(3, 10)][];
    ReadableDateTime[][] dates = new ReadableDateTime[values.length][];
    for (int d = 0; d < values.length; d++) {
        values[d] = new long[randomBoolean() ? randomBoolean() ? 0 : 1 : between(2, 100)];
        dates[d] = new ReadableDateTime[values[d].length];
        for (int i = 0; i < values[d].length; i++) {
            dates[d][i] = new DateTime(randomNonNegativeLong(), DateTimeZone.UTC);
            values[d][i] = dates[d][i].getMillis();
        }
    }
    Longs longs = wrap(values);
    for (int round = 0; round < 10; round++) {
        int d = between(0, values.length - 1);
        longs.setNextDocId(d);
        assertEquals(dates[d].length > 0 ? dates[d][0] : new DateTime(0, DateTimeZone.UTC), longs.getDate());
        assertEquals(values[d].length, longs.getDates().size());
        for (int i = 0; i < values[d].length; i++) {
            assertEquals(dates[d][i], longs.getDates().get(i));
        }
        Exception e = expectThrows(UnsupportedOperationException.class, () -> longs.getDates().add(new DateTime()));
        assertEquals("doc values are unmodifiable", e.getMessage());
    }
    assertWarnings("getDate on numeric fields is deprecated. Use a date field to get dates.", "getDates on numeric fields is deprecated. Use a date field to get dates.");
}
Also used : ReadableDateTime(org.joda.time.ReadableDateTime) Longs(org.elasticsearch.index.fielddata.ScriptDocValues.Longs) ReadableDateTime(org.joda.time.ReadableDateTime) DateTime(org.joda.time.DateTime)

Example 4 with ReadableDateTime

use of org.joda.time.ReadableDateTime in project elasticsearch by elastic.

the class SearchFieldsIT method testFieldsPulledFromFieldData.

public void testFieldsPulledFromFieldData() throws Exception {
    createIndex("test");
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("_source").field("enabled", false).endObject().startObject("properties").startObject("text_field").field("type", "text").field("fielddata", true).endObject().startObject("keyword_field").field("type", "keyword").endObject().startObject("byte_field").field("type", "byte").endObject().startObject("short_field").field("type", "short").endObject().startObject("integer_field").field("type", "integer").endObject().startObject("long_field").field("type", "long").endObject().startObject("float_field").field("type", "float").endObject().startObject("double_field").field("type", "double").endObject().startObject("date_field").field("type", "date").endObject().startObject("boolean_field").field("type", "boolean").endObject().startObject("binary_field").field("type", "binary").endObject().startObject("ip_field").field("type", "ip").endObject().endObject().endObject().endObject().string();
    client().admin().indices().preparePutMapping().setType("type1").setSource(mapping, XContentType.JSON).execute().actionGet();
    ReadableDateTime date = new DateTime(2012, 3, 22, 0, 0, DateTimeZone.UTC);
    client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("text_field", "foo").field("keyword_field", "foo").field("byte_field", (byte) 1).field("short_field", (short) 2).field("integer_field", 3).field("long_field", 4L).field("float_field", 5.0f).field("double_field", 6.0d).field("date_field", Joda.forPattern("dateOptionalTime").printer().print(date)).field("boolean_field", true).field("ip_field", "::1").endObject()).execute().actionGet();
    client().admin().indices().prepareRefresh().execute().actionGet();
    SearchRequestBuilder builder = client().prepareSearch().setQuery(matchAllQuery()).addDocValueField("text_field").addDocValueField("keyword_field").addDocValueField("byte_field").addDocValueField("short_field").addDocValueField("integer_field").addDocValueField("long_field").addDocValueField("float_field").addDocValueField("double_field").addDocValueField("date_field").addDocValueField("boolean_field").addDocValueField("ip_field");
    SearchResponse searchResponse = builder.execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    assertThat(searchResponse.getHits().getHits().length, equalTo(1));
    Set<String> fields = new HashSet<>(searchResponse.getHits().getAt(0).getFields().keySet());
    assertThat(fields, equalTo(newHashSet("byte_field", "short_field", "integer_field", "long_field", "float_field", "double_field", "date_field", "boolean_field", "text_field", "keyword_field", "ip_field")));
    assertThat(searchResponse.getHits().getAt(0).getFields().get("byte_field").getValue().toString(), equalTo("1"));
    assertThat(searchResponse.getHits().getAt(0).getFields().get("short_field").getValue().toString(), equalTo("2"));
    assertThat(searchResponse.getHits().getAt(0).getFields().get("integer_field").getValue(), equalTo((Object) 3L));
    assertThat(searchResponse.getHits().getAt(0).getFields().get("long_field").getValue(), equalTo((Object) 4L));
    assertThat(searchResponse.getHits().getAt(0).getFields().get("float_field").getValue(), equalTo((Object) 5.0));
    assertThat(searchResponse.getHits().getAt(0).getFields().get("double_field").getValue(), equalTo((Object) 6.0d));
    assertThat(searchResponse.getHits().getAt(0).getFields().get("date_field").getValue(), equalTo(date));
    assertThat(searchResponse.getHits().getAt(0).getFields().get("boolean_field").getValue(), equalTo((Object) true));
    assertThat(searchResponse.getHits().getAt(0).getFields().get("text_field").getValue(), equalTo("foo"));
    assertThat(searchResponse.getHits().getAt(0).getFields().get("keyword_field").getValue(), equalTo("foo"));
    assertThat(searchResponse.getHits().getAt(0).getFields().get("ip_field").getValue(), equalTo("::1"));
}
Also used : ReadableDateTime(org.joda.time.ReadableDateTime) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) DateTime(org.joda.time.DateTime) ReadableDateTime(org.joda.time.ReadableDateTime) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) Sets.newHashSet(org.elasticsearch.common.util.set.Sets.newHashSet) HashSet(java.util.HashSet)

Example 5 with ReadableDateTime

use of org.joda.time.ReadableDateTime in project joda-time by JodaOrg.

the class TestConverterManager method testGetInstantConverterOKMultipleMatches.

public void testGetInstantConverterOKMultipleMatches() {
    InstantConverter c = new InstantConverter() {

        public long getInstantMillis(Object object, Chronology chrono) {
            return 0;
        }

        public Chronology getChronology(Object object, DateTimeZone zone) {
            return null;
        }

        public Chronology getChronology(Object object, Chronology chrono) {
            return null;
        }

        public Class getSupportedType() {
            return ReadableDateTime.class;
        }
    };
    try {
        ConverterManager.getInstance().addInstantConverter(c);
        InstantConverter ok = ConverterManager.getInstance().getInstantConverter(new DateTime());
        // ReadableDateTime and ReadableInstant both match, but RI discarded as less specific
        assertEquals(ReadableDateTime.class, ok.getSupportedType());
    } finally {
        ConverterManager.getInstance().removeInstantConverter(c);
    }
    assertEquals(6, ConverterManager.getInstance().getInstantConverters().length);
}
Also used : ReadableDateTime(org.joda.time.ReadableDateTime) Chronology(org.joda.time.Chronology) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) ReadableDateTime(org.joda.time.ReadableDateTime)

Aggregations

DateTime (org.joda.time.DateTime)5 ReadableDateTime (org.joda.time.ReadableDateTime)5 Chronology (org.joda.time.Chronology)2 DateTimeZone (org.joda.time.DateTimeZone)2 HashSet (java.util.HashSet)1 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 Sets.newHashSet (org.elasticsearch.common.util.set.Sets.newHashSet)1 Dates (org.elasticsearch.index.fielddata.ScriptDocValues.Dates)1 Longs (org.elasticsearch.index.fielddata.ScriptDocValues.Longs)1 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 ReadablePartial (org.joda.time.ReadablePartial)1 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)1