@SuppressForbidden(reason = "Math#abs is trappy") private int generateShardId(ClusterState clusterState, String index, String type, String id, @Nullable String routing) { IndexMetaData indexMetaData = clusterState.metaData().index(index); if (indexMetaData == null) { throw new IndexNotFoundException(index); } final Version createdVersion = indexMetaData.getCreationVersion(); final HashFunction hashFunction = indexMetaData.getRoutingHashFunction(); final boolean useType = indexMetaData.getRoutingUseType(); final int hash; if (routing == null) { if (!useType) { hash = hash(hashFunction, id); } else { hash = hash(hashFunction, type, id); } } else { hash = hash(hashFunction, routing); } if (createdVersion.onOrAfter(Version.V_2_0_0_beta1)) { return MathUtils.mod(hash, indexMetaData.getNumberOfShards()); } else { return Math.abs(hash % indexMetaData.getNumberOfShards()); } }