Skip to content
This repository was archived by the owner on Sep 19, 2023. It is now read-only.

Commit 7dd08b8

Browse files
DATSOLR-364 - Fix duplicate core names in Solr Url when using MulticoreSolrServerFactory.
We now make sure to use the base url along with the collection callback. This fixes situations where the core name had been falsely appended multiple times.
1 parent 4c49f1f commit 7dd08b8

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/main/java/org/springframework/data/solr/core/SolrTemplate.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
import org.springframework.data.solr.core.schema.SolrPersistentEntitySchemaCreator.Feature;
8585
import org.springframework.data.solr.server.SolrClientFactory;
8686
import org.springframework.data.solr.server.support.HttpSolrClientFactory;
87+
import org.springframework.data.solr.server.support.MulticoreSolrClientFactory;
8788
import org.springframework.util.Assert;
8889
import org.springframework.util.ClassUtils;
8990
import org.springframework.util.CollectionUtils;
@@ -206,8 +207,16 @@ public <T> T execute(String collection, CollectionCallback<T> action) {
206207

207208
try {
208209

209-
SolrClient solrClient = StringUtils.hasText(collection) ? this.solrClientFactory.getSolrClient(collection)
210-
: this.getSolrClient();
210+
SolrClient solrClient = null;
211+
if(StringUtils.hasText(collection)) {
212+
if(this.solrClientFactory instanceof MulticoreSolrClientFactory) {
213+
solrClient = this.solrClientFactory.getSolrClient();
214+
} else {
215+
solrClient = this.solrClientFactory.getSolrClient(collection);
216+
}
217+
} else {
218+
solrClient = this.getSolrClient();
219+
}
211220
return action.doInSolr(solrClient, collection);
212221
} catch (Exception e) {
213222
DataAccessException resolved = getExceptionTranslator().translateExceptionIfPossible(

src/test/java/org/springframework/data/solr/core/ITestSolrTemplate.java

+19
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.apache.solr.client.solrj.SolrClient;
3838
import org.apache.solr.client.solrj.SolrServerException;
3939
import org.apache.solr.client.solrj.beans.Field;
40+
import org.apache.solr.client.solrj.impl.HttpSolrClient;
4041
import org.apache.solr.common.SolrInputDocument;
4142
import org.apache.solr.common.params.FacetParams;
4243
import org.apache.solr.common.params.FacetParams.FacetRangeInclude;
@@ -106,6 +107,7 @@
106107
import org.springframework.data.solr.core.query.result.StatsResult;
107108
import org.springframework.data.solr.core.query.result.TermsFieldEntry;
108109
import org.springframework.data.solr.core.query.result.TermsPage;
110+
import org.springframework.data.solr.server.support.MulticoreSolrClientFactory;
109111
import org.xml.sax.SAXException;
110112

111113
import com.google.common.collect.Lists;
@@ -1259,6 +1261,23 @@ public void testFindByNameWithSpellcheckSeggestion() {
12591261
Assert.assertThat(found.getSuggestions(), Matchers.contains("green"));
12601262
}
12611263

1264+
@Test // DATSOLR-364
1265+
public void shouldUseBaseUrlInCollectionCallbackWhenExecutingCommands() {
1266+
1267+
final HttpSolrClient client = new HttpSolrClient("http://127.0.0.1/solr/");
1268+
1269+
SolrTemplate solrTemplate = new SolrTemplate(new MulticoreSolrClientFactory(client), "collection-1");
1270+
1271+
solrTemplate.execute("collection-1", new CollectionCallback<Object>() {
1272+
@Override
1273+
public Object doInSolr(SolrClient solrClient, String collection) throws SolrServerException, IOException {
1274+
1275+
Assert.assertThat(((HttpSolrClient)solrClient).getBaseURL(), is("http://127.0.0.1/solr"));
1276+
return null;
1277+
}
1278+
});
1279+
}
1280+
12621281
private void executeAndCheckStatsRequest(StatsOptions statsOptions) {
12631282

12641283
ExampleSolrBean bean1 = new ExampleSolrBean("id-1", "one", null);

0 commit comments

Comments
 (0)