diff --git a/quesma/model/bucket_aggregations/terms.go b/quesma/model/bucket_aggregations/terms.go index 36ed72b8e..8a4096f3c 100644 --- a/quesma/model/bucket_aggregations/terms.go +++ b/quesma/model/bucket_aggregations/terms.go @@ -4,7 +4,6 @@ package bucket_aggregations import ( "context" - "fmt" "quesma/logger" "quesma/model" "quesma/util" @@ -33,65 +32,30 @@ func (query Terms) TranslateSqlResponseToJson(rows []model.QueryResultRow) model return model.JsonMap{} } - fmt.Println(rows) - - var keyIsBool bool + var response []model.JsonMap for _, row := range rows { - key := query.key(row) - if key == nil { - continue + docCount := query.docCount(row) + bucket := model.JsonMap{ + "doc_count": docCount, } - - switch query.key(row).(type) { - case bool, *bool: - keyIsBool = true - default: - keyIsBool = false + if query.significant { + bucket["score"] = docCount + bucket["bg_count"] = docCount } - break - } - var response []model.JsonMap - if !keyIsBool { - for _, row := range rows { - docCount := query.docCount(row) - bucket := model.JsonMap{ - "key": query.key(row), - "doc_count": docCount, - } - if query.significant { - bucket["score"] = docCount - bucket["bg_count"] = docCount - } - response = append(response, bucket) + // response for bool keys is different + key := query.key(row) + if boolPtr, isBoolPtr := key.(*bool); isBoolPtr { + key = *boolPtr } - } else { // key is bool - for _, row := range rows { - var ( - key any - keyAsString string - ) - if val, err := util.ExtractBool(query.key(row)); err == nil { - if val { - key = 1 - keyAsString = "true" - } else { - key = 0 - keyAsString = "false" - } - } - docCount := query.docCount(row) - bucket := model.JsonMap{ - "key": key, - "key_as_string": keyAsString, - "doc_count": docCount, - } - if query.significant { - bucket["score"] = docCount - bucket["bg_count"] = docCount - } - response = append(response, bucket) + if keyAsBool, ok := key.(bool); ok { + bucket["key"] = util.BoolToInt(keyAsBool) + bucket["key_as_string"] = util.BoolToString(keyAsBool) + } else { + bucket["key"] = key } + + response = append(response, bucket) } if !query.significant { diff --git a/quesma/queryparser/pancake_sql_query_generation_test.go b/quesma/queryparser/pancake_sql_query_generation_test.go index 0870ac4e3..3f99e3527 100644 --- a/quesma/queryparser/pancake_sql_query_generation_test.go +++ b/quesma/queryparser/pancake_sql_query_generation_test.go @@ -52,10 +52,6 @@ func TestPancakeQueryGeneration(t *testing.T) { t.Skip("Fix filters") } - if i != 104 { - //t.Skip() - } - if test.TestName == "Line, Y-axis: Min, Buckets: Date Range, X-Axis: Terms, Split Chart: Date Histogram(file:kibana-visualize/agg_req,nr:9)" { t.Skip("Date range is broken, fix in progress (PR #971)") } diff --git a/quesma/util/utils.go b/quesma/util/utils.go index 2ae21c4db..34f3c36c2 100644 --- a/quesma/util/utils.go +++ b/quesma/util/utils.go @@ -737,17 +737,18 @@ func ExtractNumeric64(value any) float64 { return asFloat64 } -// ExtractBool returns: -// * (value as bool, nil), if value is either bool or *bool -// * (false, err), otherwise -func ExtractBool(value any) (bool, error) { - switch valueTyped := value.(type) { - case bool: - return valueTyped, nil - case *bool: - return *valueTyped, nil +func BoolToInt(b bool) int { + if b { + return 1 + } + return 0 +} + +func BoolToString(b bool) string { + if b { + return "true" } - return false, fmt.Errorf("ExtractBool, value of incorrect type. Expected (*)bool, received: %v; type: %T", value, value) + return "false" } type sqlMockMismatchSql struct {