From 5c5888f47cac9adf8657370572b524e33ff5b33b Mon Sep 17 00:00:00 2001 From: Lucia Zimmermann <7947215+zimzoom@users.noreply.github.com> Date: Tue, 11 Aug 2026 18:31:39 -0400 Subject: [PATCH 1/2] CMR-11419: fix error handling for ES8 query complexity limit --- .../src/cmr/elastic_utils/search/es_index.clj | 2 +- .../test/cmr/elastic_utils/test/es_index.clj | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/elastic-utils-lib/src/cmr/elastic_utils/search/es_index.clj b/elastic-utils-lib/src/cmr/elastic_utils/search/es_index.clj index a61dd7fcd4..b622e451fc 100644 --- a/elastic-utils-lib/src/cmr/elastic_utils/search/es_index.clj +++ b/elastic-utils-lib/src/cmr/elastic_utils/search/es_index.clj @@ -217,7 +217,7 @@ :payload-too-large "The search is creating more buckets than allowed by CMR. Please narrow your search.")) - (when (re-find #"maxClauseCount is set to 1024" body) + (when (re-find #"(?i)(?:maxClauseCount is set to [0-9]+|too many clauses)" body) (errors/throw-service-error :payload-too-large "The search is creating more clauses than allowed by CMR. Please narrow your search.")) diff --git a/elastic-utils-lib/test/cmr/elastic_utils/test/es_index.clj b/elastic-utils-lib/test/cmr/elastic_utils/test/es_index.clj index 467d424efc..578ca3c590 100644 --- a/elastic-utils-lib/test/cmr/elastic_utils/test/es_index.clj +++ b/elastic-utils-lib/test/cmr/elastic_utils/test/es_index.clj @@ -3,9 +3,44 @@ (:require [clojure.test :refer [deftest is testing]] [cmr.common.services.search.query-model :as qm] + [cmr.elastic-utils.es-helper :as es-helper] [cmr.elastic-utils.search.es-group-query-conditions :as gc] [cmr.elastic-utils.search.es-index :as es-index])) +(def clause-limit-message + "The search is creating more clauses than allowed by CMR. Please narrow your search.") + +(defn- send-query-with-es-error + [body] + (with-redefs [es-index/context->conn (constantly nil) + es-helper/search (fn [& _] + (throw (ex-info "Elasticsearch request failed" + {:status 400 + :body body})))] + (#'es-index/do-send-with-retry + {} + {:index-name "test-index" :type-name "collection"} + {} + 1))) + +(deftest clause-limit-errors-are-payload-too-large + (doseq [[description body] + [["Elasticsearch 7 default clause limit error" + "maxClauseCount is set to 1024"] + ["Elasticsearch 7 configured clause limit error" + "maxClauseCount is set to 4096"] + ["Elasticsearch 8 clause limit error" + (str "{\"error\":{\"root_cause\":[{\"type\":\"illegal_argument_exception\"," + "\"reason\":\"Query rewrite failed: too many clauses\"}]},\"status\":400}")]]] + (testing description + (let [exception (try + (send-query-with-es-error body) + nil + (catch clojure.lang.ExceptionInfo e + e))] + (is (= :payload-too-large (:type (ex-data exception)))) + (is (= [clause-limit-message] (:errors (ex-data exception)))))))) + (deftest test-query->execution-params (let [query->execution-params #'es-index/query->execution-params condition (gc/or-conds (map #(qm/string-conditions :consortiums [%]) From 43ff8913ef0036e075ba582c5b467d209310c148 Mon Sep 17 00:00:00 2001 From: Me Date: Thu, 13 Aug 2026 13:29:54 -0400 Subject: [PATCH 2/2] CMR-11419: add status code check to query complexity error handling --- .../src/cmr/elastic_utils/search/es_index.clj | 3 ++- .../test/cmr/elastic_utils/test/es_index.clj | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/elastic-utils-lib/src/cmr/elastic_utils/search/es_index.clj b/elastic-utils-lib/src/cmr/elastic_utils/search/es_index.clj index b622e451fc..d8cd88e646 100644 --- a/elastic-utils-lib/src/cmr/elastic_utils/search/es_index.clj +++ b/elastic-utils-lib/src/cmr/elastic_utils/search/es_index.clj @@ -217,7 +217,8 @@ :payload-too-large "The search is creating more buckets than allowed by CMR. Please narrow your search.")) - (when (re-find #"(?i)(?:maxClauseCount is set to [0-9]+|too many clauses)" body) + (when (and (= 400 (:status (ex-data e))) + (re-find #"(?i)(?:maxClauseCount is set to [0-9]+|too many clauses)" body)) (errors/throw-service-error :payload-too-large "The search is creating more clauses than allowed by CMR. Please narrow your search.")) diff --git a/elastic-utils-lib/test/cmr/elastic_utils/test/es_index.clj b/elastic-utils-lib/test/cmr/elastic_utils/test/es_index.clj index 578ca3c590..b97b953616 100644 --- a/elastic-utils-lib/test/cmr/elastic_utils/test/es_index.clj +++ b/elastic-utils-lib/test/cmr/elastic_utils/test/es_index.clj @@ -11,11 +11,11 @@ "The search is creating more clauses than allowed by CMR. Please narrow your search.") (defn- send-query-with-es-error - [body] + [status body] (with-redefs [es-index/context->conn (constantly nil) es-helper/search (fn [& _] (throw (ex-info "Elasticsearch request failed" - {:status 400 + {:status status :body body})))] (#'es-index/do-send-with-retry {} @@ -34,13 +34,22 @@ "\"reason\":\"Query rewrite failed: too many clauses\"}]},\"status\":400}")]]] (testing description (let [exception (try - (send-query-with-es-error body) + (send-query-with-es-error 400 body) nil (catch clojure.lang.ExceptionInfo e e))] (is (= :payload-too-large (:type (ex-data exception)))) (is (= [clause-limit-message] (:errors (ex-data exception)))))))) +(deftest clause-limit-errors-require-bad-request-status + (let [exception (try + (send-query-with-es-error 500 "Query rewrite failed: too many clauses") + nil + (catch clojure.lang.ExceptionInfo e + e))] + (is (nil? (:type (ex-data exception)))) + (is (= 500 (:status (ex-data (ex-cause exception))))))) + (deftest test-query->execution-params (let [query->execution-params #'es-index/query->execution-params condition (gc/or-conds (map #(qm/string-conditions :consortiums [%])