def gather_facets_from_response(self, response: QueryResult, facets: FacetsParameters) -> Dict[str, Dict]:
        """Convert a Vespa QueryResult into a Marqo-style facets response.

        Returns a dictionary of the form:
        {
            "field_name": {
                "facet_value_1": { stats... },
                "facet_value_2": { stats... },
                ...
            },
            ...
        }
        """
        if facets is not None:
            facet_field_map = self._build_field_map(facets)
        facets_response = {}
        total_hits = None

        # Process root groups only
        root_groups = (group for group in response.facets if group.id.startswith("group:facet:"))
        for group in root_groups:
            if group.children is None:
                continue
            for field in group.children:
                field_name = self._extract_facet_field_name(field.label, facets)
                if not field_name == self._TOTAL_HITS_GROUP_CONST:
                    facets_response.setdefault(field_name, {})

                for value in field.children:
                    group_type, value_key = self._parse_value_id(value.id)
                    processed_stats = self._process_value_stats(value.fields)

                    if field_name == self._TOTAL_HITS_GROUP_CONST:
                        total_hits = processed_stats["count"]

                    elif facets.fields[field_name].type in ["string", "array"]:
                        if value.id == "group:string:":
                            # Vespa's value for not found
                            continue
                        # values might be nested deeply if user data is stored as a.b.c
                        facets_response[field_name][value_key] = processed_stats
                    elif facets.fields[field_name].type in ["number"]:
                        if any(value in [self._MIN_LONG, 'NaN'] for value in processed_stats.values()):
                            # Vespa's value for null for int and float
                            continue
                        if facets.fields[field_name].ranges is None:
                            # aggregate statistic between int and float
                            facets_response[field_name] = self._combine_number_stats(
                                facets_response.get(field_name, {}), processed_stats
                            )
                        elif group_type != "null": # the case when there is no values in range
                            self._process_range_facets(
                                field_name, value_key, processed_stats,
                                facet_field_map, facets_response
                            )
                    else:
                        raise InternalError(f"Failed to parse facet {field_name} with type {facets.fields[field_name].type}")

        # Sort range facets by upper bound
        if facets is not None:
            for field_name, field_data in facets_response.items():
                if facet_field_map.get(field_name).ranges is not None:
                    facets_response[field_name] = self._sort_range_facets(field_data)

        response = {}
        if total_hits is not None:
            response["totalHits"] = total_hits
        if facets is not None:
            response["facets"] = facets_response
        return response
Convert a Vespa QueryResult into a Marqo-style facets response.

        Returns a dictionary of the form:
        {
            "field_name": {
                "facet_value_1": { stats... },
                "facet_value_2": { stats... },
                ...
            },
            ...
        }
        
src/marqo/core/semi_structured_vespa_index/semi_structured_vespa_index.py