You can achieve this setup pretty easily.
You have two instances of Sitecore. For each SOLR core definition you will keep core name (name parameter) and id the same (id attribute).
Normally config would look like this:
<index id="sitecore_master_index" .....>
<param desc="name">$(id)</param>
<param desc="core">$(id)</param>
You will just specify "core" parameter as follows:
<param desc="core">instance1_$(id)</param>
or <param desc="core">instance2_$(id)</param>
as you need based on instance name.
So config in your case would look like this at the end:
<index id="sitecore_master_index" .....>
<param desc="name">$(id)</param>
<param desc="core">instancename_$(id)</param>
For each solution you will have then config files for solr with same file name, solr ids and all the remaining settings. Only difference will be this "core" parameter.
Here is the patch config to apply your instance settings per solr core:
<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:search="http://www.sitecore.net/xmlconfig/search/">
<sitecore search:require="solr">
<contentSearch>
<configuration type="Sitecore.ContentSearch.ContentSearchConfiguration, Sitecore.ContentSearch">
<indexes hint="list:AddIndex">
<index id="sitecore_master_index" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
<param desc="core" patch:instead="param[@desc='core']">instancename_$(id)</param>
</index>
<index id="sitecore_core_index" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
<param desc="core" patch:instead="param[@desc='core']">instancename_$(id)</param>
</index>
<index id="sitecore_web_index" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
<param desc="core" patch:instead="param[@desc='core']">instancename_$(id)</param>
</index>
<index id="sitecore_marketingdefinitions_master" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
<param desc="core" patch:instead="param[@desc='core']">instancename_$(id)</param>
</index>
<index id="sitecore_marketingdefinitions_web" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
<param desc="core" patch:instead="param[@desc='core']">instancename_$(id)</param>
</index>
<index id="sitecore_marketing_asset_index_master" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
<param desc="core" patch:instead="param[@desc='core']">instancename_$(id)</param>
</index>
<index id="sitecore_marketing_asset_index_web" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
<param desc="core" patch:instead="param[@desc='core']">instancename_$(id)</param>
</index>
<index id="sitecore_testing_index" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
<param desc="core" patch:instead="param[@desc='core']">instancename_$(id)</param>
</index>
<index id="sitecore_suggested_test_index" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
<param desc="core" patch:instead="param[@desc='core']">instancename_$(id)</param>
</index>
<index id="sitecore_fxm_master_index" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
<param desc="core" patch:instead="param[@desc='core']">instancename_$(id)</param>
</index>
<index id="sitecore_fxm_web_index" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
<param desc="core" patch:instead="param[@desc='core']">instancename_$(id)</param>
</index>
</indexes>
</configuration>
</contentSearch>
</sitecore>
</configuration>
Do not forget about added SXA indexes also if you will have them in the future and also for CD servers, you need to remove master indexes from the list.
In code you will reuse everything as you have as you are referencing solr index id so you will still reference "sitecore_master_index" or "sitecore_web_index" but based on the deployed configuration in each instance, you will effectively reference different Solr cores based on instance.
To identify Sitecore Solr cores quickly, you can go to ShowConfig admin page and search for <indexes hint="list:AddIndex"
.
You will see all 11 out of the box indexes and their respective configuration. You can quickly identify files in which are patched:
<param desc="core">instance1_$(id)</param>
and for the second instance like this<param desc="core">instance2_$(id)</param>
. Is this what you were looking for?