algoliasearch/src/main/java/com/algolia/search/saas/helpers/DisjunctiveFaceting.java
static private <T extends Collection<String>> JSONObject aggregateDisjunctiveFacetingResults(@NonNull JSONObject answers, @NonNull Collection<String> disjunctiveFacets, @NonNull Map<String, T> refinements) throws AlgoliaException
    {
        Map<String, T> disjunctiveRefinements = filterDisjunctiveRefinements(disjunctiveFacets, refinements);

        // aggregate answers
        // first answer stores the hits + regular facets
        try {
            boolean nonExhaustiveFacetsCount = false;
            JSONArray results = answers.getJSONArray("results");
            JSONObject aggregatedAnswer = results.getJSONObject(0);
            JSONObject disjunctiveFacetsJSON = new JSONObject();
            for (int i = 1; i < results.length(); ++i) {
                if (!results.getJSONObject(i).optBoolean("exhaustiveFacetsCount")) {
                    nonExhaustiveFacetsCount = true;
                }
                JSONObject facets = results.getJSONObject(i).getJSONObject("facets");
                @SuppressWarnings("unchecked")
                Iterator<String> keys = facets.keys();
                while (keys.hasNext()) {
                    String key = keys.next();
                    // Add the facet to the disjunctive facet hash
                    disjunctiveFacetsJSON.put(key, facets.getJSONObject(key));
                    // concatenate missing refinements
                    if (!disjunctiveRefinements.containsKey(key)) {
                        continue;
                    }
                    for (String refine : disjunctiveRefinements.get(key)) {
                        if (!disjunctiveFacetsJSON.getJSONObject(key).has(refine)) {
                            disjunctiveFacetsJSON.getJSONObject(key).put(refine, 0);
                        }
                    }
                }
            }
            aggregatedAnswer.put("disjunctiveFacets", disjunctiveFacetsJSON);
            if (nonExhaustiveFacetsCount) {
                aggregatedAnswer.put("exhaustiveFacetsCount", false);
            }
            return aggregatedAnswer;
        } catch (JSONException e) {
            throw new AlgoliaException("Failed to aggregate results", e);
        }
    }
static private <T extends Collection<String>> JSONObject aggregateDisjunctiveFacetingResults(@NonNull JSONObject answers, @NonNull Collection<String> disjunctiveFacets, @NonNull Map<String, T> refinements) throws AlgoliaException
    {
        Map<String, T> disjunctiveRefinements = filterDisjunctiveRefinements(disjunctiveFacets, refinements);

        // aggregate answers
        // first answer stores the hits + regular facets
        try {
            boolean nonExhaustiveFacetsCount = false;
            JSONArray results = answers.getJSONArray("results");
            JSONObject aggregatedAnswer = results.getJSONObject(0);
            JSONObject disjunctiveFacetsJSON = new JSONObject();
            for (int i = 1; i < results.length(); ++i) {
                if (!results.getJSONObject(i).optBoolean("exhaustiveFacetsCount")) {
                    nonExhaustiveFacetsCount = true;
                }
                JSONObject facets = results.getJSONObject(i).getJSONObject("facets");
                @SuppressWarnings("unchecked")
                Iterator<String> keys = facets.keys();
                while (keys.hasNext()) {
                    String key = keys.next();
                    // Add the facet to the disjunctive facet hash
                    disjunctiveFacetsJSON.put(key, facets.getJSONObject(key));
                    // concatenate missing refinements
                    if (!disjunctiveRefinements.containsKey(key)) {
                        continue;
                    }
                    for (String refine : disjunctiveRefinements.get(key)) {
                        if (!disjunctiveFacetsJSON.getJSONObject(key).has(refine)) {
                            disjunctiveFacetsJSON.getJSONObject(key).put(refine, 0);
                        }
                    }
                }
            }
            aggregatedAnswer.put("disjunctiveFacets", disjunctiveFacetsJSON);
            if (nonExhaustiveFacetsCount) {
                aggregatedAnswer.put("exhaustiveFacetsCount", false);
            }
            return aggregatedAnswer;
        } catch (JSONException e) {
            throw new AlgoliaException("Failed to aggregate results", e);
        }
    }
Aggregate results from multiple queries into disjunctive faceting results.

@param answers The response from the multiple queries.
@param disjunctiveFacets List of disjunctive facets.
@param refinements Facet refinements.
@return The aggregated results.
@throws AlgoliaException when aggregation fails due to a JSONException