From dcaf11bab6beea5b14d7b576cae8f61a12bbad60 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Thu, 16 May 2024 15:59:57 +0200 Subject: [PATCH] Handle 'allow_no_indices' and 'ignore_unavailable' query params for /field_caps (#128) Fixes data view management/settings panel in Kibana. Since we don't close/open indices, both options behave the same Depends on: - https://github.com/QuesmaOrg/quesma/pull/126 --- quesma/quesma/field_caps.go | 13 +++++++++++++ quesma/quesma/router.go | 5 ++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/quesma/quesma/field_caps.go b/quesma/quesma/field_caps.go index 7a374c7bf..ece38a1bb 100644 --- a/quesma/quesma/field_caps.go +++ b/quesma/quesma/field_caps.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "fmt" "mitmproxy/quesma/clickhouse" "mitmproxy/quesma/elasticsearch" "mitmproxy/quesma/model" @@ -182,6 +183,18 @@ func handleFieldCapsIndex(ctx context.Context, indexes []string, tables clickhou return json.Marshal(fieldCapsResponse) } +func EmptyFieldCapsResponse() []byte { + var response = model.FieldCapsResponse{ + Fields: make(map[string]map[string]model.FieldCapability), + Indices: []string{}, + } + if serialized, err := json.Marshal(response); err != nil { + panic(fmt.Sprintf("Failed to serialize empty field caps response: %v, this should never happen", err)) + } else { + return serialized + } +} + func isInternalColumn(col *clickhouse.Column) bool { return col.Name == clickhouse.AttributesKeyColumn || col.Name == clickhouse.AttributesValueColumn } diff --git a/quesma/quesma/router.go b/quesma/quesma/router.go index 77b326fd2..62de2e355 100644 --- a/quesma/quesma/router.go +++ b/quesma/quesma/router.go @@ -220,10 +220,13 @@ func configureRouter(cfg config.QuesmaConfiguration, lm *clickhouse.LogManager, return elasticsearchQueryResult(string(responseBody), httpOk), nil }) - router.RegisterPathMatcher(routes.FieldCapsPath, []string{"GET", "POST"}, matchedAgainstPattern(cfg), func(ctx context.Context, body string, _ string, params map[string]string, _ http.Header, _ url.Values) (*mux.Result, error) { + router.RegisterPathMatcher(routes.FieldCapsPath, []string{"GET", "POST"}, matchedAgainstPattern(cfg), func(ctx context.Context, body string, _ string, params map[string]string, _ http.Header, queryParams url.Values) (*mux.Result, error) { responseBody, err := handleFieldCaps(ctx, params["index"], []byte(body), lm) if err != nil { if errors.Is(errIndexNotExists, err) { + if queryParams.Get("allow_no_indices") == "true" || queryParams.Get("ignore_unavailable") == "true" { + return elasticsearchQueryResult(string(EmptyFieldCapsResponse()), httpOk), nil + } return &mux.Result{StatusCode: 404}, nil } else { return nil, err