Skip to content

Commit bc63730

Browse files
committed
Issue #4: recursivity with fieldmappings with an object type
1 parent 6780309 commit bc63730

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

pkg/quickwit/quickwit.go

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,18 @@ type QuickwitDatasource struct {
2626
dsInfo es.DatasourceInfo
2727
}
2828

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+
2936
type QuickwitMapping struct {
3037
IndexConfig struct {
3138
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"`
3841
} `json:"doc_mapping"`
3942
} `json:"index_config"`
4043
}
@@ -56,6 +59,30 @@ func newErrorCreationPayload(statusCode int, message string) error {
5659
return errors.New(string(json))
5760
}
5861

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+
5986
func getTimestampFieldInfos(index string, qwUrl string, cli *http.Client) (string, string, error) {
6087
mappingEndpointUrl := qwUrl + "/indexes/" + index
6188
qwlog.Info("Calling quickwit endpoint: " + mappingEndpointUrl)
@@ -91,22 +118,16 @@ func getTimestampFieldInfos(index string, qwUrl string, cli *http.Client) (strin
91118
}
92119

93120
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)
101122

102-
if timestampFieldFormat == "undef" {
123+
if nil == timestampFieldFormat {
103124
errMsg := fmt.Sprintf("No format found for field: %s", string(timestampFieldName))
104125
qwlog.Error(errMsg)
105126
return timestampFieldName, "", newErrorCreationPayload(statusCode, errMsg)
106127
}
107128

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
110131
}
111132

112133
// Creates a Quickwit datasource.

0 commit comments

Comments
 (0)