@Override
  @SuppressWarnings("deprecation")
  public void execute(FunctionContext<RegionConfig.Index> context) {
    final RegionConfig.Index indexInfo = context.getArguments();
    String memberId = null;
    try {
      Cache cache = context.getCache();
      memberId = cache.getDistributedSystem().getDistributedMember().getId();
      QueryService queryService = cache.getQueryService();
      String indexName = indexInfo.getName();
      String indexedExpression = indexInfo.getExpression();
      String fromClause = indexInfo.getFromClause();
      if (indexInfo.isKeyIndex()) {
        queryService.createKeyIndex(indexName, indexedExpression, fromClause);
      } else if ("hash".equals(indexInfo.getType())) {
        queryService.createHashIndex(indexName, indexedExpression, fromClause);
      } else {
        queryService.createIndex(indexName, indexedExpression, fromClause);
      }

      context.getResultSender()
          .lastResult(new CliFunctionResult(memberId, true, "Index successfully created"));

    } catch (IndexExistsException e) {
      String message =
          CliStrings.format(CliStrings.CREATE_INDEX__INDEX__EXISTS, indexInfo.getName());
      context.getResultSender().lastResult(
          new CliFunctionResult(memberId, CliFunctionResult.StatusState.IGNORABLE, message));
    } catch (IndexNameConflictException e) {
      String message =
          CliStrings.format(CliStrings.CREATE_INDEX__NAME__CONFLICT, indexInfo.getName());
      context.getResultSender().lastResult(
          new CliFunctionResult(memberId, CliFunctionResult.StatusState.IGNORABLE, message));
    } catch (RegionNotFoundException e) {
      String message = CliStrings.format(CliStrings.CREATE_INDEX__INVALID__REGIONPATH,
          indexInfo.getFromClause());
      context.getResultSender().lastResult(new CliFunctionResult(memberId, false, message));
    } catch (IndexInvalidException e) {
      context.getResultSender().lastResult(new CliFunctionResult(memberId, e, e.getMessage()));
    } catch (Exception e) {
      String exceptionMessage = CliStrings.format(CliStrings.EXCEPTION_CLASS_AND_MESSAGE,
          e.getClass().getName(), e.getMessage());
      context.getResultSender().lastResult(new CliFunctionResult(memberId, e, exceptionMessage));
    }
  }
Function to create index in a member, based on different arguments passed to it
geode-gfsh/src/main/java/org/apache/geode/management/internal/cli/functions/CreateIndexFunction.java