Yes the system auto detects the replica based on index definition.
You can read more details here:
Pre 5.0
https://developer.couchbase.com/documentation/server/4.6/indexes/gsi-for-n1ql.html
5.0
https://developer.couchbase.com/documentation/server/5.0/indexes/index-replication.html