code: private void processOrderByClause(TermsBuilder termsBuilder, Expression orderByExpression,
              Expression groupByClause, EntityMetadata entityMetadata)
      {
          Expression orderByClause = (OrderByClause) orderByExpression;
          OrderByItem orderByItems = (OrderByItem) ((OrderByClause) orderByClause).getOrderByItems();
  
          if (orderByClause instanceof CollectionExpression
                  || !(orderByItems.getExpression().toParsedText().equalsIgnoreCase(groupByClause.toParsedText())))
          {
              logger.error("Order by and group by on different field are not supported simultaneously");
              throw new UnsupportedOperationException(
                      "Order by and group by on different field are not supported simultaneously");
          }
  
          String ordering = orderByItems.getOrdering().toString();
          termsBuilder.order(Terms.Order.term(!ordering.equalsIgnoreCase(Expression.DESC)));
      }
docs: Process order by clause.
  
  @param termsBuilder
  the terms builder
  @param orderByExpression
  the order by expression
  @param groupByClause
  the group by clause
  @param entityMetadata
  the entity metadata