The following error message emitted from org.elasticsearch.action.support.replication.ReplicationOperation is misleading:

```
Not enough active copies to meet shard count of [1] (have 1, needed 1)
```

full context:
```
org.elasticsearch.transport.RemoteTransportException: [es-es-index-58f985f8f-pgls6][10.2.12.197:9300][indices:data/write/bulk[s][p]]
Caused by: org.elasticsearch.action.UnavailableShardsException: [.security-7][0] Not enough active copies to meet shard count of [1] (have 1, needed 1). Timeout: [1m], request: [BulkShardRequest [[.security-7][0]] containing [index {[.security][myrole], source[{"enabled":true,"roles":["myrole"],"rules":{"all":[{"field":{"realm.name":"myrealm"}},{"field":{"username":"me"}}]},"metadata":{},"doc_type":"role-mapping"}]}] and a refresh]
```

The message appears to be due to the calculation for wait_for_active_shards only counts search shards, but the message is constructed by counting all shards. In stateless this message can mean that the search shard is not available. 



server/src/main/java/org/elasticsearch/action/support/replication/ReplicationOperation.java
"primary shard [%s] was demoted while failing replica shard",
                        primary.routingEntry()
                    );
                    primary.failShard(message, failure);
                    finishAsFailed(new RetryOnPrimaryException(primary.routingEntry().shardId(), message, failure));
                }
                @Override
                public boolean isForceExecution() {
                    return true;
                }
                @Override
                public void onFailure(Exception e) {
                    e.addSuppressed(failure);
                    assert false : e;
                    logger.error(() -> "unexpected failure while failing primary [" + primary.routingEntry() + "]", e);
                    finishAsFailed(
                        new RetryOnPrimaryException(
                            primary.routingEntry().shardId(),
                            String.format(Locale.ROOT, "unexpected failure while failing primary [%s]", primary.routingEntry()),
                            e
                        )
                    );
                }
            });
        }
    }
    /**
     * Checks whether we can perform a write based on the required active shard count setting.
     * Returns **null* if OK to proceed, or a string describing the reason to stop
     */