public static void addFacetingRequest(
      JsonBuilder.Object jsonQuery, FacetingRequest facetingRequest) {
    String fieldName = facetingRequest.getFieldName();
    if (facetingRequest instanceof DiscreteFacetRequest) {
      JsonObject termsJsonQuery =
          JsonBuilder.object()
              .add(
                  "terms",
                  JsonBuilder.object()
                      .addProperty("field", fieldName)
                      .addProperty(
                          "size",
                          facetingRequest.getMaxNumberOfFacets() == -1
                              ? 0
                              : facetingRequest.getMaxNumberOfFacets())
                      .add("order", fromFacetSortOrder(facetingRequest.getSort()))
                      .addProperty(
                          "min_doc_count", facetingRequest.hasZeroCountsIncluded() ? 0 : 1))
              .build();

      if (isNested(fieldName)) {
        JsonBuilder.Object facetJsonQuery = JsonBuilder.object();
        facetJsonQuery.add(
            "nested",
            JsonBuilder.object().addProperty("path", FieldHelper.getEmbeddedFieldPath(fieldName)));
        facetJsonQuery.add(
            "aggregations",
            JsonBuilder.object().add(facetingRequest.getFacetingName(), termsJsonQuery));
        jsonQuery.add(facetingRequest.getFacetingName(), facetJsonQuery);
      } else {
        jsonQuery.add(facetingRequest.getFacetingName(), termsJsonQuery);
      }
    } else if (facetingRequest instanceof RangeFacetRequest<?>) {
      RangeFacetRequest<?> rangeFacetingRequest = (RangeFacetRequest<?>) facetingRequest;
      for (FacetRange<?> facetRange : rangeFacetingRequest.getFacetRangeList()) {
        JsonBuilder.Object comparisonFragment = JsonBuilder.object();
        if (facetRange.getMin() != null) {
          comparisonFragment.addProperty(
              facetRange.isMinIncluded() ? "gte" : "gt", facetRange.getMin());
        }
        if (facetRange.getMax() != null) {
          comparisonFragment.addProperty(
              facetRange.isMaxIncluded() ? "lte" : "lt", facetRange.getMax());
        }

        JsonObject rangeQuery =
            wrapQueryForNestedIfRequired(
                fieldName,
                JsonBuilder.object()
                    .add("range", JsonBuilder.object().add(fieldName, comparisonFragment))
                    .build());

        jsonQuery.add(
            facetingRequest.getFacetingName() + "-" + facetRange.getIdentifier(),
            JsonBuilder.object().add("filter", rangeQuery));
      }
    } else {
      throw new IllegalArgumentException(
          "Faceting request of type " + facetingRequest.getClass().getName() + " not supported");
    }
  }
 @Override
 public FacetManager enableFaceting(FacetingRequest facetingRequest) {
   facetRequests.put(facetingRequest.getFacetingName(), (FacetingRequestImpl) facetingRequest);
   queryHasChanged();
   return this;
 }