def _get_facets_term(self, facets_parameters: FacetsParameters, exclusion_terms: List[str] = None) -> str:
        """
        Build a facets grouping query string from the provided facets_parameters.
        """
        FIELD_TYPES = {
            "int": INT_FIELDS,
            "float": FLOAT_FIELDS,
            "string": SHORT_STRINGS_FIELDS,
            "array": STRING_ARRAY,
        }

        global_max_results = facets_parameters.max_results or 100
        global_sort_order = facets_parameters.order or ""

        def build_group_parameters(field_config) -> str:
            """Build the max and order parameters for a group."""
            parts = []

            # Add max results if specified
            if field_config.max_results is not None:
                parts.append(f"max({field_config.max_results})")
            elif global_max_results:
                parts.append(f"max({global_max_results})")

            # Add sort order if specified
            if field_config.order:
                prefix = '-' if field_config.order == 'desc' else ''
                parts.append(f"order({prefix}count())")
            elif global_sort_order:
                prefix = '-' if global_sort_order == 'desc' else ''
                parts.append(f"order({prefix}count())")
            else:
                parts.append("order(-count())")

            return " ".join(parts)

        def build_group_expression(field_config, field_name, field_id, field_type_overwrite=None) -> str:
            """Build the group expression for a field."""

            field_type = FIELD_TYPES[field_type_overwrite if field_type_overwrite else field_config.type]

            # Handle numeric fields with ranges
            if field_type in [INT_FIELDS, FLOAT_FIELDS] and field_config.ranges:
                buckets = []
                for range_config in field_config.ranges:
                    from_val = range_config.from_ if range_config.from_ is not None else "-inf"
                    to_val = range_config.to_ if range_config.to_ is not None else "inf"
                    buckets.append(f'bucket({from_val}, {to_val})')
                return f'predefined({field_type}{{"{field_name}"}}, {", ".join(buckets)})'

            elif field_type == STRING_ARRAY:
                return f"{STRING_ARRAY}_{field_name}"

            # Handle fields without ranges
            return str(field_id) if field_type in [INT_FIELDS, FLOAT_FIELDS] else f'{field_type}{{"{field_name}"}}'

        def build_field_group(field_config, field_name, field_id, field_type_overwrite=None) -> str:
            """Build the complete group request for a field."""
            group_expr = build_group_expression(field_config, field_name, field_id, field_type_overwrite)
            params = build_group_parameters(field_config)

            # Build output expression
            if field_config.type == "number":
                aggregations = ["sum", "avg", "min", "max"]
                field_type = FIELD_TYPES[field_type_overwrite]
                funcs = [f'{func}({field_type}{{"{field_name}"}})' for func in aggregations]
                funcs.append("count()")
                output = f"each(output({', '.join(funcs)}))"
            else:
                output = "each(output(count()))"

            return f"all(group({group_expr}) {params} {output}) "

        # Start building the overall grouping query.
        grouping_query = "all( "
        any_field = False # when exclusions are present, we need to check if any field is included in the default query
        if facets_parameters.max_depth is not None:
            grouping_query += f"max({facets_parameters.max_depth}) "
            # all(max(n) - state of grouping query

        for field_id, field_data in enumerate(facets_parameters.fields.items()):
            field_name, field_parameters = field_data
            if field_parameters.exclude_terms is not None:
                # We want this field to be in a separate query if any of the exclusions are not in the exclusions list
                if exclusion_terms is None or any([exclusion_term not in exclusion_terms for exclusion_term in field_parameters.exclude_terms]):
                    continue
            elif exclusion_terms is not None:
                continue
            any_field = True
            if field_parameters.type == "number":
                # we build 2 queries for number: flot and int
                grouping_query += build_field_group(field_parameters, field_name, field_id, field_type_overwrite="int")
                grouping_query += build_field_group(field_parameters, field_name, f"-{field_id}", field_type_overwrite="float")
            else:
                if field_parameters.type == "array":
                    if self.get_marqo_index().name_to_string_array_field_map.get(field_name) is None:
                        # Skip array field if it is not in the string array field map
                        continue
                grouping_query += build_field_group(field_parameters, field_name, field_id)

        grouping_query += ")"
        return grouping_query if any_field else None # None if default query is empty (all queries have exclusions)

        Build a facets grouping query string from the provided facets_parameters.
        
src/marqo/core/semi_structured_vespa_index/semi_structured_vespa_index.py