<commit_before>(ns cmr.access-control.data.group-json-results-handler
  "Handles extracting elasticsearch group results and converting them into a JSON search response."
  (:require [cmr.common-app.services.search.elastic-results-to-query-results :as elastic-results]
            [cmr.common-app.services.search.elastic-search-index :as elastic-search-index]
            [cmr.common-app.services.search :as qs]
            [cmr.common.util :as util]
            [cheshire.core :as json]))

(def base-fields
  ["concept-id" "revision-id" "name" "provider-id" "description" "legacy-guid" "member-count"])

(def fields-with-members
  (conj base-fields "members"))

(defn- include-members?
  [query]
  (some #{:include-members} (:result-features query)))

(defmethod elastic-search-index/concept-type+result-format->fields [:access-group :json]
  [concept-type query]
  (if (include-members? query)
    fields-with-members
    base-fields))

(defmethod elastic-results/elastic-result->query-result-item [:access-group :json]
  [context query elastic-result]
  (let [field-values (:fields elastic-result)
        item (util/map-values first (dissoc field-values :members))]
    (if (include-members? query)
      (assoc item :members (:members field-values))
      item)))

(defmethod qs/search-results->response [:access-group :json]
  [context query results]
  (let [results (select-keys results [:hits :took :items])
        converted-items (util/map-keys->snake_case results)]
   (json/generate-string converted-items)))
<commit_msg>CMR-3387: Adding comments to new vars in group json results handler.<commit_after>(ns cmr.access-control.data.group-json-results-handler
  "Handles extracting elasticsearch group results and converting them into a JSON search response."
  (:require [cmr.common-app.services.search.elastic-results-to-query-results :as elastic-results]
            [cmr.common-app.services.search.elastic-search-index :as elastic-search-index]
            [cmr.common-app.services.search :as qs]
            [cmr.common.util :as util]
            [cheshire.core :as json]))

(def base-fields
  "The base set of fields to select from Elasticsearch"
  ["concept-id" "revision-id" "name" "provider-id" "description" "legacy-guid" "member-count"]

(def fields-with-members
  "The fields to select from Elasticsearch if members should be included"
  (conj base-fields "members"))

(defn- include-members?
  "Returns true if the query indicates members should be returned in the response."
  [query]
  (some #{:include-members} (:result-features query)))

(defmethod elastic-search-index/concept-type+result-format->fields [:access-group :json]
  [concept-type query]
  (if (include-members? query)
    fields-with-members
    base-fields))

(defmethod elastic-results/elastic-result->query-result-item [:access-group :json]
  [context query elastic-result]
  (let [field-values (:fields elastic-result)
        item (util/map-values first (dissoc field-values :members))]
    (if (include-members? query)
      (assoc item :members (:members field-values))
      item)))

(defmethod qs/search-results->response [:access-group :json]
  [context query results]
  (let [results (select-keys results [:hits :took :items])
        converted-items (util/map-keys->snake_case results)]
   (json/generate-string converted-items)))

{'fraction_non_alphanumeric': 0.13535414165666265, 'fraction_numerical': 0.0012004801920768306, 'mean_word_length': 3.8799414348462666, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6746382', 'n_tokens_mistral': 1054, 'n_tokens_neox': 1014, 'n_words': 259}