     {:order {sort-field "desc"}}
     {}))
 
-(defn- build-parsets-agg
-  [terms sub-aggs]
+(defn- build-parsets-agg [{:keys [terms sub-aggs size] :or {size 5}}]
   (reduce (fn [agg-query a-term]
             {:aggs (merge
                     {(keyword a-term)
                      (merge {:terms
-                             (merge {:field a-term :size 5}
+                             (merge {:field a-term :size size}
                                     (sort-sub sub-aggs))}
                             agg-query)}
                     sub-aggs)})
           (build-sub-aggs sub-aggs) terms))
-
-(defn- build-terms-agg [prefix terms sub-aggs]
-  (let [terms-aggs (for [term terms]
-                     {(str prefix (name term)) (merge {:terms (merge {:field (name term) :size 5}
-                                                                     (sort-sub sub-aggs))}
-                                                      (build-sub-aggs sub-aggs))})
-        terms-aggs (reduce merge terms-aggs)]
-    terms-aggs))
 
 (defn- walk-buckets [de-buck bucket prefix steps value-path]
   (map #(de-buck % de-buck prefix (first steps) (rest steps) value-path)


(defcomponent parsets [cursor owner {:keys [agg value-path url height] :or {height 600 value-path ["doc_count"]} :as opts}]
  (init-state [_]
              (let [{:keys [es-settings ref-state]} (om/get-shared owner)
                    prefix-terms "terms_"]
                {:continue? true
                 :comm (chan)
                 :url url
                 :agg-prefix-terms prefix-terms
                 :parsets-agg (->> agg :terms (map keyword))
                 :get-agg-terms #(:parsets-agg (om/get-state owner))
                 :make-agg-query #(->query {:agg-prefix-terms prefix-terms
                                            :agg-terms ((:get-agg-terms (om/get-state owner)))
                                            :sub-aggs (:sub agg)
                                            :show-dimensions? (:show-dimensions? (om/get-state owner))
                                            :filters (ref-state :filters)
                                            :default-filter (get-in (es-settings) [:default :filter])})

  (vec (flatten (walk-buckets <-buckets agg-result {} steps value-path))))

(defn- ->query
  [{:keys [agg-terms sub-aggs show-dimensions? filters default-filter size] :or {size 5}}]
  (let [agg-query (build-parsets-agg {:terms (reverse agg-terms) :sub-aggs sub-aggs :size size})
        agg-query (filtered-agg (merge {:body agg-query}
                                 (merge {:filter default-filter}
                                        (:combined-query filters))))]
    agg-query))

(defn- reset-highlighted [owner]
  (-> js/d3 (.select (om/get-node owner "parsets-dims")) (.selectAll ".mg-bar") (.classed "highlight" false)))

(defn- do-parsets-agg
  [owner]
  (let [{:keys [continue? comm url get-agg-terms make-agg-query value-path]} (om/get-state owner)
        {es-settings :es-settings} (om/get-shared owner)]
    (when continue?
      (go (.log js/console "# running parsets aggregation:" (pr-str (get-agg-terms)))
          (let [{agg-result :aggregations} (<! (es-agg (or url (:url-agg (es-settings))) (make-agg-query)))]
            (let [parsets-data (agg->parsets agg-result (get-agg-terms) value-path)]
              (reset-highlighted owner)
              (om/update-state! owner #(assoc % :parsets-data parsets-data))
