@@ -26,15 +26,18 @@ type QuickwitDatasource struct {
26
26
dsInfo es.DatasourceInfo
27
27
}
28
28
29
+ type FieldMappings struct {
30
+ Name string `json:"name"`
31
+ Type string `json:"type"`
32
+ OutputFormat * string `json:"output_format,omitempty"`
33
+ FieldMappings []FieldMappings `json:"field_mappings,omitempty"`
34
+ }
35
+
29
36
type QuickwitMapping struct {
30
37
IndexConfig struct {
31
38
DocMapping struct {
32
- TimestampField string `json:"timestamp_field"`
33
- FieldMappings []struct {
34
- Name string `json:"name"`
35
- Type string `json:"type"`
36
- OutputFormat * string `json:"output_format,omitempty"`
37
- } `json:"field_mappings"`
39
+ TimestampField string `json:"timestamp_field"`
40
+ FieldMappings []FieldMappings `json:"field_mappings"`
38
41
} `json:"doc_mapping"`
39
42
} `json:"index_config"`
40
43
}
@@ -56,6 +59,30 @@ func newErrorCreationPayload(statusCode int, message string) error {
56
59
return errors .New (string (json ))
57
60
}
58
61
62
+ func findTimeStampFormat (timestampFieldName string , parentName * string , fieldMappings []FieldMappings ) * string {
63
+ if nil == fieldMappings {
64
+ return nil
65
+ }
66
+
67
+ for _ , field := range fieldMappings {
68
+ fieldName := field .Name
69
+ if nil != parentName {
70
+ fieldName = fmt .Sprintf ("%s.%s" , * parentName , fieldName )
71
+ }
72
+
73
+ if field .Type == "datetime" && fieldName == timestampFieldName && nil != field .OutputFormat {
74
+ return field .OutputFormat
75
+ } else if field .Type == "object" && nil != field .FieldMappings {
76
+ format := findTimeStampFormat (timestampFieldName , & field .Name , field .FieldMappings )
77
+ if nil != format {
78
+ return format
79
+ }
80
+ }
81
+ }
82
+
83
+ return nil
84
+ }
85
+
59
86
func getTimestampFieldInfos (index string , qwUrl string , cli * http.Client ) (string , string , error ) {
60
87
mappingEndpointUrl := qwUrl + "/indexes/" + index
61
88
qwlog .Info ("Calling quickwit endpoint: " + mappingEndpointUrl )
@@ -91,22 +118,16 @@ func getTimestampFieldInfos(index string, qwUrl string, cli *http.Client) (strin
91
118
}
92
119
93
120
timestampFieldName := payload .IndexConfig .DocMapping .TimestampField
94
- timestampFieldFormat := "undef"
95
- for _ , field := range payload .IndexConfig .DocMapping .FieldMappings {
96
- if field .Type == "datetime" && field .Name == timestampFieldName && nil != field .OutputFormat {
97
- timestampFieldFormat = * field .OutputFormat
98
- break
99
- }
100
- }
121
+ timestampFieldFormat := findTimeStampFormat (timestampFieldName , nil , payload .IndexConfig .DocMapping .FieldMappings )
101
122
102
- if timestampFieldFormat == "undef" {
123
+ if nil == timestampFieldFormat {
103
124
errMsg := fmt .Sprintf ("No format found for field: %s" , string (timestampFieldName ))
104
125
qwlog .Error (errMsg )
105
126
return timestampFieldName , "" , newErrorCreationPayload (statusCode , errMsg )
106
127
}
107
128
108
- qwlog .Info (fmt .Sprintf ("Found timestampFieldName = %s, timestampFieldFormat = %s" , timestampFieldName , timestampFieldFormat ))
109
- return timestampFieldName , timestampFieldFormat , nil
129
+ qwlog .Info (fmt .Sprintf ("Found timestampFieldName = %s, timestampFieldFormat = %s" , timestampFieldName , * timestampFieldFormat ))
130
+ return timestampFieldName , * timestampFieldFormat , nil
110
131
}
111
132
112
133
// Creates a Quickwit datasource.
0 commit comments