@@ -19,12 +19,14 @@ import org.opensearch.action.search.SearchRequest
19
19
import org.opensearch.action.search.SearchResponse
20
20
import org.opensearch.client.Client
21
21
import org.opensearch.common.Rounding
22
+ import org.opensearch.common.time.DateFormatter
23
+ import org.opensearch.common.time.DateFormatters
22
24
import org.opensearch.common.unit.TimeValue
23
25
import org.opensearch.common.xcontent.LoggingDeprecationHandler
24
- import org.opensearch.core.xcontent.NamedXContentRegistry
25
26
import org.opensearch.common.xcontent.XContentFactory
26
27
import org.opensearch.common.xcontent.XContentHelper
27
28
import org.opensearch.common.xcontent.XContentType
29
+ import org.opensearch.core.xcontent.NamedXContentRegistry
28
30
import org.opensearch.index.query.MatchAllQueryBuilder
29
31
import org.opensearch.indexmanagement.IndexManagementPlugin
30
32
import org.opensearch.indexmanagement.common.model.dimension.DateHistogram
@@ -34,7 +36,7 @@ import org.opensearch.indexmanagement.rollup.model.ContinuousMetadata
34
36
import org.opensearch.indexmanagement.rollup.model.Rollup
35
37
import org.opensearch.indexmanagement.rollup.model.RollupMetadata
36
38
import org.opensearch.indexmanagement.rollup.model.RollupStats
37
- import org.opensearch.indexmanagement.rollup.util.DATE_FIELD_EPOCH_MILLIS_FORMAT
39
+ import org.opensearch.indexmanagement.rollup.util.DATE_FIELD_STRICT_DATE_OPTIONAL_TIME_FORMAT
38
40
import org.opensearch.indexmanagement.util.NO_ID
39
41
import org.opensearch.search.aggregations.bucket.composite.InternalComposite
40
42
import org.opensearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder
@@ -181,7 +183,7 @@ class RollupMetadataService(val client: Client, val xContentRegistry: NamedXCont
181
183
.sort(dateHistogram.sourceField, SortOrder .ASC ) // TODO: figure out where nulls are sorted
182
184
.trackTotalHits(false )
183
185
.fetchSource(false )
184
- .docValueField(dateHistogram.sourceField, DATE_FIELD_EPOCH_MILLIS_FORMAT )
186
+ .docValueField(dateHistogram.sourceField, DATE_FIELD_STRICT_DATE_OPTIONAL_TIME_FORMAT )
185
187
val searchRequest = SearchRequest (rollup.sourceIndex)
186
188
.source(searchSourceBuilder)
187
189
.allowPartialSearchResults(false )
@@ -194,10 +196,12 @@ class RollupMetadataService(val client: Client, val xContentRegistry: NamedXCont
194
196
195
197
// Get the doc value field of the dateHistogram.sourceField for the first search hit converted to epoch millis
196
198
// If the doc value is null or empty it will be treated the same as empty doc hits
197
- val firstHitTimestamp = response.hits.hits.first().field(dateHistogram.sourceField).getValue<String >()?.toLong ()
199
+ val firstHitTimestampAsString : String? = response.hits.hits.first().field(dateHistogram.sourceField).getValue<String >()
198
200
? : return StartingTimeResult .NoDocumentsFound
199
-
200
- return StartingTimeResult .Success (getRoundedTime(firstHitTimestamp, dateHistogram))
201
+ // Parse date and extract epochMillis
202
+ val formatter = DateFormatter .forPattern(DATE_FIELD_STRICT_DATE_OPTIONAL_TIME_FORMAT )
203
+ val epochMillis = DateFormatters .from(formatter.parse(firstHitTimestampAsString), formatter.locale()).toInstant().toEpochMilli()
204
+ return StartingTimeResult .Success (getRoundedTime(epochMillis, dateHistogram))
201
205
} catch (e: RemoteTransportException ) {
202
206
val unwrappedException = ExceptionsHelper .unwrapCause(e) as Exception
203
207
logger.debug(" Error when getting initial start time for rollup [${rollup.id} ]: $unwrappedException " )
0 commit comments