code: def _build_aggregation(
      self, rule_query: str, labels: Optional[List[str]] = None
  ) -> Dict[str, Any]:
      annotated_records_filter = filters.exists_field("annotated_as")
      rule_query_filter = filters.text_query(rule_query)
      aggr_filters = {
          "covered_records": rule_query_filter,
          "annotated_covered_records": filters.boolean_filter(
              filter_query=annotated_records_filter,
              should_filters=[rule_query_filter],
          ),
      }
  
      if labels is not None:
          for label in labels:
              rule_label_annotated_filter = filters.term_filter(
                  "annotated_as", value=label
              )
              encoded_label = self._encode_label_name(label)
              aggr_filters.update(
                  {
                      f"{encoded_label}.correct_records": filters.boolean_filter(
                          filter_query=annotated_records_filter,
                          should_filters=[
                              rule_query_filter,
                              rule_label_annotated_filter,
                          ],
                          minimum_should_match=2,
                      ),
                      f"{encoded_label}.incorrect_records": filters.boolean_filter(
                          filter_query=annotated_records_filter,
                          must_query=rule_query_filter,
                          must_not_query=rule_label_annotated_filter,
                      ),
                  }
              )
  
      return aggregations.filters_aggregation(aggr_filters)
  
file_path: argilla/src/argilla/server/daos/backend/metrics/text_classification.py
imports: import dataclasses
  from typing import Any, Dict, List, Optional
  from argilla.server.daos.backend.metrics.base import ElasticsearchMetric, TermsAggregation
  from argilla.server.daos.backend.query_helpers import aggregations, filters
  from argilla.server.helpers import unflatten_dict
label_desc: Use image node_id 1 for calling the LabelingRulesMetric obj's underlying member method code with example usage: obj._build_aggregation(rule_query, labels) and returns: aggregations