                                   (get-potential-matching-query-params query-params field-name))]
      (remove-value-from-query-params-for-hierachical-field query-params potential-qp-matches value))))
 
+(defn- remove-duplicate-params
+  "Removes any parameters for the provided field which are exact subsets of another index for the
+  same field."
+  [query-params base-field]
+  (let [potential-qps (filter (fn [[k _]]
+                                (re-find (re-pattern base-field) k))
+                              query-params)   ;; Eliminate any params we do not care about
+        ;; Group the remaining params by index
+        param-groups-by-index (group-by (fn [[k _]]
+                                          (second (re-find #"\[\d+\]" k)))
+                                        potential-qps)
+        _ (println "Params grouped by index" param-groups-by-index)
+        ;; Iterate (reduce) through each set of parameters
+        indexes-to-remove
+         (set
+          (flatten
+           (for [[idx qps] param-groups-by-index
+                 :let [qps (util/map-keys #(str/replace % #"\[\d+\]" "")
+                                          (into {} qps))
+                       _ (println "CDD: qps are" qps)]]
+             (keep (fn [[matching-index matching-qps]]
+                     (when (and (not= idx matching-index)
+                                ;; remove the index
+                                (let [matching-qps (util/map-keys #(str/replace % #"\[\d+\]" "")
+                                                                  (into {} matching-qps))
+                                      ;; TODO change matching-qps so that all the keys are compared by ignoring the index
+                                      _ (println "CDD: matching qps are:" matching-qps)
+                                      _ (println "IDX" (type idx) "Matching-index" (type matching-index))]
+                                  ;; another group of params fully contains this group of params
+                                  (and (= qps (select-keys matching-qps (keys qps)))
+                                       (or (not= qps matching-qps)
+                                           (< (Integer/parseInt (format "%s" idx))
+                                              (Integer/parseInt (format "%s" matching-index)))))))
+                       idx))
+                   param-groups-by-index))))
+        query-keys-to-remove (mapcat #(keys (get param-groups-by-index %)) indexes-to-remove)]
+    (if (seq query-keys-to-remove)
+      (do (println "Wow I am in here and need to remove" query-keys-to-remove)
+          (apply dissoc query-params query-keys-to-remove))
+      query-params)))
+
+  ;; if select-keys of another group of parameters contains all of these parameters then this one
+  ;; is no longer needed.
+
 (defn create-remove-link-for-hierarchical-field
   "Create a link that will modify the current search to no longer filter on the given hierarchical
   field-name and value. Looks for matches case insensitively.

                                    (map (fn [qp]
                                           (str/replace-first qp #"\]\[.*\]"
                                                              (format "][%s]" subfield))))
                                    (select-keys query-params))]
          ; matching-indexes (map keys matching-indexes)
          ; matching-indexes (map (fn [ancestor])

                                ; matching-indexes)
                              ;  (map #(first (keys %))))
          ; potential-qp-matches nil]
          ; potential-qp-matches (when ancestors-found?
          ;                        (->> ancestors-found
          ;                             (filter #(= (count (first ancestors-to-match)) (count %)))))]

                                      ; (map #(select-keys % [subfield]))
                                      ; (into {})))]
          ; potential-qp-matches (into {}
          ;                            (map #(for [[k v] %
          ;                                        :when (re-find (re-pattern subfield) k)]
          ;                                    [k v])
          ;                                 potential-qp-matches))]

        ; (println "matching indexes are" matching-indexes)
        (println "potential-qp-matches are" potential-qp-matches)
          ; exact-query-params (map #(str/replace-first field-name "[0]" (format "[%s]" %)) parent-indexes)
          ; _ (println "Exact query params are" exact-query-params)]
          ; potential-query-params (into {} (filter (fn [[k _]]
          ;                                           (seq (filter #(= % k) exact-query-params)))
          ;                                         query-params))
          ; _ (when (= value "Level2-2") (println "Potential query params for" value ":" potential-query-params))
          ; value-exists? (or (seq (get-keys-to-remove potential-query-params value))
          ;                   (seq (get-keys-to-update potential-query-params value)))]
      ; (println "Value exists is" value-exists?)
          ;                                                      (format "[%d]" parent-indexes))]
          ;             (println "Exact query-param is: " exact-query-param "and value is" value "and found existing query-param"
          ;                      (get query-params exact-query-param))
          ;             ; (throw "Failed")
          ;             (filter (fn [[k _]] (= exact-query-param k)) query-params))
      (if ancestors-found?
        (println "Found all ancestors!! for" field-name "and" value)
        (println "Did not find ancestors" ancestors-to-match ancestors-found))

   (let [[base-field subfield] (str/split field-name #"\[0\]")
         updated-params (reduce (partial process-removal-for-field-value-tuple base-field potential-qp-matches)
                                query-params
                                (conj applied-children-tuples [subfield value]))
         ;; TODO Remove any extraneous parameters
         updated-params (remove-duplicate-params updated-params base-field)]
     {:remove (generate-query-string base-url updated-params)})))

(defn create-link-for-hierarchical-field
  "Creates either a remove or an apply link based on whether this particular value is already
