From 45e6ab5882728b618529cd9002541062ac43513e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?France=CC=81=20Wilke?= <francewilke@gmail.com>
Date: Wed, 9 Aug 2023 14:31:22 +0200
Subject: [PATCH] OpenSearch - Add checks for the flattened search tag

---
 api_logs/api-logs.go      | 8 ++++----
 opensearch/time_series.go | 7 +++++++
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/api_logs/api-logs.go b/api_logs/api-logs.go
index bfd4ad1..e0e446d 100644
--- a/api_logs/api-logs.go
+++ b/api_logs/api-logs.go
@@ -194,14 +194,14 @@ type ApiLog struct {
 }
 
 type ApiLogRequest struct {
-	Headers         map[string]string `json:"headers,omitempty"`
+	Headers         map[string]string `json:"headers,omitempty"` // NOTE: Once we're on OpenSearch 2.7, we should add search:"flattened" here
 	QueryParameters map[string]string `json:"query_parameters,omitempty"`
 	BodySize        int               `json:"body_size" search:"long"` // set even when body is truncated/omitted
 	Body            string            `json:"body,omitempty"`          // json body as a string
 }
 
 type ApiLogResponse struct {
-	Headers  map[string]string `json:"headers,omitempty"`
-	BodySize int               `json:"body_size"`      // set even when body is truncated/omitted
-	Body     string            `json:"body,omitempty"` // json content as a string
+	Headers  map[string]string `json:"headers,omitempty"` // NOTE: Once we're on OpenSearch 2.7, we should add search:"flattened" here
+	BodySize int               `json:"body_size"`         // set even when body is truncated/omitted
+	Body     string            `json:"body,omitempty"`    // json content as a string
 }
diff --git a/opensearch/time_series.go b/opensearch/time_series.go
index be3e3a0..c2cff48 100644
--- a/opensearch/time_series.go
+++ b/opensearch/time_series.go
@@ -170,6 +170,12 @@ func structMappingProperties(structType reflect.Type) (map[string]MappingPropert
 					fieldMapping = MappingProperty{
 						Properties: subStructProperties,
 					}
+				} else if structField.Type.Kind() == reflect.Map {
+					// For maps, we only want to map if we specified "flattened" in the search tag
+					// If we did not specify the tag, we want to just continue without mapping
+					if structField.Tag.Get("search") != "flattened" {
+						continue
+					}
 				} else {
 					// fieldMapping = MappingProperty{Type: "text"}
 					// unknown value type... we do not specify mapping and let it use dynamic mapping
@@ -195,6 +201,7 @@ func structMappingProperties(structType reflect.Type) (map[string]MappingPropert
 			fieldMapping.Type = "object"
 			fieldMapping.Enabled = false
 		case "flattened":
+			// NOTE: This is only supported from OpenSearch 2.7 and onwards
 			fieldMapping.Type = "flat_object"
 		case "-":
 			// do not include in mapping
-- 
GitLab