- 
                Notifications
    You must be signed in to change notification settings 
- Fork 25.6k
          Supporting more timestamp formats in _text_structure/find_structure
          #133745
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            eyalkoren
  merged 15 commits into
  elastic:main
from
eyalkoren:supporting-more-timestamp-formats
  
      
      
   
  Sep 2, 2025 
      
    
  
     Merged
                    Changes from 12 commits
      Commits
    
    
            Show all changes
          
          
            15 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      55cac38
              
                Supporting two new formats
              
              
                eyalkoren 36eb43a
              
                Merge remote-tracking branch 'upstream/main' into supporting-more-tim…
              
              
                eyalkoren e70108b
              
                Testing timestamp format detection
              
              
                eyalkoren 48574a5
              
                Merge remote-tracking branch 'upstream/main' into supporting-more-tim…
              
              
                eyalkoren be57989
              
                Removing yaml test and fixing unit tests
              
              
                eyalkoren 43768a2
              
                Merge remote-tracking branch 'upstream/main' into supporting-more-tim…
              
              
                eyalkoren 46c466c
              
                Adjusting yaml test
              
              
                eyalkoren 200afa3
              
                Support fractional seconds in the YMD formats
              
              
                eyalkoren a93bb8f
              
                Merge remote-tracking branch 'upstream/main' into supporting-more-tim…
              
              
                eyalkoren e0a49cd
              
                Skip 120_grok/Test Grok Patterns Retrieval from compatibility tests
              
              
                eyalkoren b934bac
              
                Merge branch 'main' into supporting-more-timestamp-formats
              
              
                eyalkoren e8544a4
              
                Update docs/changelog/133745.yaml
              
              
                eyalkoren ea222b3
              
                Merge remote-tracking branch 'upstream/main' into supporting-more-tim…
              
              
                eyalkoren 3bc6b70
              
                Simplifying if in test
              
              
                eyalkoren 676db49
              
                Merge remote-tracking branch 'eyalkoren/supporting-more-timestamp-for…
              
              
                eyalkoren File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| pr: 133745 | ||
| summary: Supporting more timestamp formats in `_text_structure/find_structure` | ||
| area: Machine Learning | ||
| type: feature | ||
| issues: [] | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            175 changes: 175 additions & 0 deletions
          
          175 
        
  ...Test/java/org/elasticsearch/xpack/textstructure/rest/TextStructureTimestampFormatsIT.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
| package org.elasticsearch.xpack.textstructure.rest; | ||
|  | ||
| import com.carrotsearch.randomizedtesting.annotations.Name; | ||
| import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; | ||
|  | ||
| import org.apache.http.entity.ContentType; | ||
| import org.apache.http.entity.StringEntity; | ||
| import org.elasticsearch.client.Request; | ||
| import org.elasticsearch.client.Response; | ||
| import org.elasticsearch.test.cluster.ElasticsearchCluster; | ||
| import org.elasticsearch.test.cluster.local.distribution.DistributionType; | ||
| import org.elasticsearch.test.rest.ESRestTestCase; | ||
| import org.junit.ClassRule; | ||
|  | ||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|  | ||
| import static org.hamcrest.Matchers.containsInAnyOrder; | ||
| import static org.hamcrest.Matchers.containsString; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
| import static org.hamcrest.Matchers.hasKey; | ||
|  | ||
| public class TextStructureTimestampFormatsIT extends ESRestTestCase { | ||
|  | ||
| public static final String[] ISO_08601_JAVA_FORMATS = new String[] { "yyyy-MM-dd HH:mm:ss" }; | ||
| public static final String ISO_08601_TIMESTAMP_GROK_PATTERN = "%{TIMESTAMP_ISO8601:timestamp}"; | ||
|  | ||
| public static final String[] TIMESTAMP_YMD_JAVA_FORMATS = new String[] { | ||
| "yyyy/MM/dd HH:mm:ss", | ||
| "yyyy.MM.dd HH:mm:ss", | ||
| "yyyy-MM-dd HH:mm:ss" }; | ||
| public static final String TIMESTAMP_YMD_TIMESTAMP_GROK_PATTERN = "%{TIMESTAMP_YMD:timestamp}"; | ||
|  | ||
| public static final String[] MONTH_EXPLICIT_NAME_JAVA_FORMATS = new String[] { "MMM d, yyyy" }; | ||
|  | ||
| private final String ecsCompatibility; | ||
|  | ||
| @ClassRule | ||
| public static ElasticsearchCluster cluster = ElasticsearchCluster.local() | ||
| .distribution(DistributionType.DEFAULT) | ||
| .module("x-pack-text-structure") | ||
| .setting("xpack.security.enabled", "false") | ||
| .build(); | ||
|  | ||
| public TextStructureTimestampFormatsIT(@Name("ecs_compatibility") String ecsCompatibility) { | ||
| this.ecsCompatibility = ecsCompatibility; | ||
| } | ||
|  | ||
| @Override | ||
| protected String getTestRestCluster() { | ||
| return cluster.getHttpAddresses(); | ||
| } | ||
|  | ||
| @ParametersFactory | ||
| public static Iterable<Object[]> parameters() { | ||
| return Arrays.asList(new Object[] { "v1" }, new Object[] { "disabled" }); | ||
| } | ||
|  | ||
| public void testTimestampYearYmdSlashFormat() throws IOException { | ||
| // use a multi-line sample to ensure we are detecting ndjson format | ||
| Map<String, Object> responseMap = executeAndVerifyRequest(""" | ||
| "2025/07/10 10:30:35" | ||
| "2025/07/10 10:31:42" | ||
| "2025/07/10 10:32:15" | ||
| """, ecsCompatibility); | ||
| verifyTimestampDetected(responseMap, "date"); | ||
| verifyTimestampFormat(responseMap, TIMESTAMP_YMD_TIMESTAMP_GROK_PATTERN, TIMESTAMP_YMD_JAVA_FORMATS); | ||
| } | ||
|  | ||
| public void testTimestampYearYmdSlashFormat_WithDotAndMillis() throws IOException { | ||
| // use a multi-line sample to ensure we are detecting ndjson format | ||
| Map<String, Object> responseMap = executeAndVerifyRequest(""" | ||
| "2025/07/10 10:30:35.123" | ||
| "2025/07/10 10:31:42.123" | ||
| "2025/07/10 10:32:15.123" | ||
| """, ecsCompatibility); | ||
| verifyTimestampDetected(responseMap, "date"); | ||
| verifyTimestampFormat( | ||
| responseMap, | ||
| TIMESTAMP_YMD_TIMESTAMP_GROK_PATTERN, | ||
| "yyyy/MM/dd HH:mm:ss.SSS", | ||
| "yyyy.MM.dd HH:mm:ss.SSS", | ||
| "yyyy-MM-dd HH:mm:ss.SSS" | ||
| ); | ||
| } | ||
|  | ||
| public void testTimestampYearYmdSlashFormat_WithSlashAndNanos() throws IOException { | ||
| // use a multi-line sample to ensure we are detecting ndjson format | ||
| Map<String, Object> responseMap = executeAndVerifyRequest(""" | ||
| "2025/07/10 10:30:35,123456789" | ||
| "2025/07/10 10:31:42,123456789" | ||
| "2025/07/10 10:32:15,123456789" | ||
| """, ecsCompatibility); | ||
| verifyTimestampDetected(responseMap, "date_nanos"); | ||
| verifyTimestampFormat( | ||
| responseMap, | ||
| TIMESTAMP_YMD_TIMESTAMP_GROK_PATTERN, | ||
| "yyyy/MM/dd HH:mm:ss,SSSSSSSSS", | ||
| "yyyy.MM.dd HH:mm:ss,SSSSSSSSS", | ||
| "yyyy-MM-dd HH:mm:ss,SSSSSSSSS" | ||
| ); | ||
| } | ||
|  | ||
| public void testTimestampYearYmdDotFormat() throws IOException { | ||
| // use a multi-line sample to ensure we are detecting ndjson format | ||
| Map<String, Object> responseMap = executeAndVerifyRequest(""" | ||
| "2025.07.10 10:30:35" | ||
| "2025.07.10 10:31:42" | ||
| "2025.07.10 10:32:15" | ||
| """, ecsCompatibility); | ||
| verifyTimestampDetected(responseMap, "date"); | ||
| verifyTimestampFormat(responseMap, TIMESTAMP_YMD_TIMESTAMP_GROK_PATTERN, TIMESTAMP_YMD_JAVA_FORMATS); | ||
| } | ||
|  | ||
| public void testIso08601TimestampFormat() throws IOException { | ||
| // use a multi-line sample to ensure we are detecting ndjson format | ||
| Map<String, Object> responseMap = executeAndVerifyRequest(""" | ||
| "2025-07-10 10:30:35" | ||
| "2025-07-10 10:31:42" | ||
| "2025-07-10 10:32:15" | ||
| """, ecsCompatibility); | ||
| verifyTimestampDetected(responseMap, "date"); | ||
| // ISO_8601 should have higher priority than TIMESTAMP_YMD | ||
| verifyTimestampFormat(responseMap, ISO_08601_TIMESTAMP_GROK_PATTERN, ISO_08601_JAVA_FORMATS); | ||
| } | ||
|  | ||
| public void testMonthExplicitNameFormat() throws IOException { | ||
| // use a multi-line sample to ensure we are detecting ndjson format | ||
| Map<String, Object> responseMap = executeAndVerifyRequest(""" | ||
| "Aug 9, 2025" | ||
| "Aug 10, 2025" | ||
| "Aug 11, 2025" | ||
| """, ecsCompatibility); | ||
| verifyTimestampDetected(responseMap, "date"); | ||
| verifyTimestampFormat(responseMap, "CUSTOM_TIMESTAMP", MONTH_EXPLICIT_NAME_JAVA_FORMATS); | ||
| } | ||
|  | ||
| private static Map<String, Object> executeAndVerifyRequest(String sample, String ecsCompatibility) throws IOException { | ||
| Request request = new Request("POST", "/_text_structure/find_structure"); | ||
| request.addParameter("ecs_compatibility", ecsCompatibility); | ||
| request.setEntity(new StringEntity(sample, ContentType.APPLICATION_JSON)); | ||
| Response response = client().performRequest(request); | ||
| assertOK(response); | ||
| return entityAsMap(response); | ||
| } | ||
|  | ||
| private static void verifyTimestampDetected(Map<String, Object> responseMap, String expectedType) { | ||
| @SuppressWarnings("unchecked") | ||
| Map<String, Object> mappings = (Map<String, Object>) responseMap.get("mappings"); | ||
| assertThat(mappings, hasKey("properties")); | ||
| @SuppressWarnings("unchecked") | ||
| Map<String, Object> properties = (Map<String, Object>) mappings.get("properties"); | ||
| assertThat(properties, hasKey("@timestamp")); | ||
| @SuppressWarnings("unchecked") | ||
| Map<String, Object> timestamp = (Map<String, Object>) properties.get("@timestamp"); | ||
| assertThat(timestamp.get("type"), equalTo(expectedType)); | ||
| } | ||
|  | ||
| private static void verifyTimestampFormat(Map<String, Object> responseMap, String expectedGrokPattern, String... expectedJavaFormats) { | ||
| assertThat(responseMap, hasKey("java_timestamp_formats")); | ||
| @SuppressWarnings("unchecked") | ||
| List<String> javaTimestampFormats = (List<String>) responseMap.get("java_timestamp_formats"); | ||
| assertThat(javaTimestampFormats, containsInAnyOrder(expectedJavaFormats)); | ||
| String grokPattern = (String) responseMap.get("grok_pattern"); | ||
| assertThat(grokPattern, containsString(expectedGrokPattern)); | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.