public static String assignShard(DocCollection collection, Integer numShards) {
    if (numShards == null) {
      numShards = 1;
    }
    String returnShardId = null;
    Map<String, Slice> sliceMap = collection != null ? collection.getActiveSlicesMap() : null;
    // TODO: now that we create shards ahead of time, is this code needed?  Esp since hash ranges aren't assigned when creating via this method?
    if (sliceMap == null) {
      return "shard1";
    }
    List<String> shardIdNames = new ArrayList<>(sliceMap.keySet());
    if (shardIdNames.size() < numShards) {
      return "shard" + (shardIdNames.size() + 1);
    }
    // TODO: don't need to sort to find shard with fewest replicas!
    // else figure out which shard needs more replicas
    final Map<String, Integer> map = new HashMap<>();
    for (String shardId : shardIdNames) {
      int cnt = sliceMap.get(shardId).getReplicasMap().size();
      map.put(shardId, cnt);
    }
    Collections.sort(shardIdNames, (String o1, String o2) -> {
      Integer one = map.get(o1);
      Integer two = map.get(o2);
      return one.compareTo(two);
    });
    returnShardId = shardIdNames.get(0);
    return returnShardId;
  }
public static String assignShard(DocCollection collection, Integer numShards) {
    if (numShards == null) {
      numShards = 1;
    }
    String returnShardId = null;
    Map<String, Slice> sliceMap = collection != null ? collection.getActiveSlicesMap() : null;
   
    if (sliceMap == null) {
      return "shard1";
    }
    List<String> shardIdNames = new ArrayList<>(sliceMap.keySet());
    if (shardIdNames.size() < numShards) {
      return "shard" + (shardIdNames.size() + 1);
    }
   
   
    final Map<String, Integer> map = new HashMap<>();
    for (String shardId : shardIdNames) {
      int cnt = sliceMap.get(shardId).getReplicasMap().size();
      map.put(shardId, cnt);
    }
    Collections.sort(shardIdNames, (String o1, String o2) -> {
      Integer one = map.get(o1);
      Integer two = map.get(o2);
      return one.compareTo(two);
    });
    returnShardId = shardIdNames.get(0);
    return returnShardId;
  }
public static string assignshard(doccollection collection, integer numshards) { if (numshards == null) { numshards = 1; } string returnshardid = null; map<string, slice> slicemap = collection != null ? collection.getactiveslicesmap() : null; if (slicemap == null) { return "shard1"; } list<string> shardidnames = new arraylist<>(slicemap.keyset()); if (shardidnames.size() < numshards) { return "shard" + (shardidnames.size() + 1); } final map<string, integer> map = new hashmap<>(); for (string shardid : shardidnames) { int cnt = slicemap.get(shardid).getreplicasmap().size(); map.put(shardid, cnt); } collections.sort(shardidnames, (string o1, string o2) -> { integer one = map.get(o1); integer two = map.get(o2); return one.compareto(two); }); returnshardid = shardidnames.get(0); return returnshardid; }