diff --git a/src/main/java/org/springframework/data/solr/core/SolrTemplate.java b/src/main/java/org/springframework/data/solr/core/SolrTemplate.java index 4ce54fb91..6ac38e66e 100644 --- a/src/main/java/org/springframework/data/solr/core/SolrTemplate.java +++ b/src/main/java/org/springframework/data/solr/core/SolrTemplate.java @@ -84,6 +84,7 @@ import org.springframework.data.solr.core.schema.SolrPersistentEntitySchemaCreator.Feature; import org.springframework.data.solr.server.SolrClientFactory; import org.springframework.data.solr.server.support.HttpSolrClientFactory; +import org.springframework.data.solr.server.support.MulticoreSolrClientFactory; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.CollectionUtils; @@ -206,8 +207,16 @@ public T execute(String collection, CollectionCallback action) { try { - SolrClient solrClient = StringUtils.hasText(collection) ? this.solrClientFactory.getSolrClient(collection) - : this.getSolrClient(); + SolrClient solrClient = null; + if(StringUtils.hasText(collection)) { + if(this.solrClientFactory instanceof MulticoreSolrClientFactory) { + solrClient = this.solrClientFactory.getSolrClient(); + } else { + solrClient = this.solrClientFactory.getSolrClient(collection); + } + } else { + solrClient = this.getSolrClient(); + } return action.doInSolr(solrClient, collection); } catch (Exception e) { DataAccessException resolved = getExceptionTranslator().translateExceptionIfPossible( diff --git a/src/test/java/org/springframework/data/solr/core/ITestSolrTemplate.java b/src/test/java/org/springframework/data/solr/core/ITestSolrTemplate.java index 3101f8f6e..7ced43283 100644 --- a/src/test/java/org/springframework/data/solr/core/ITestSolrTemplate.java +++ b/src/test/java/org/springframework/data/solr/core/ITestSolrTemplate.java @@ -37,6 +37,7 @@ import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.beans.Field; +import org.apache.solr.client.solrj.impl.HttpSolrClient; import org.apache.solr.common.SolrInputDocument; import org.apache.solr.common.params.FacetParams; import org.apache.solr.common.params.FacetParams.FacetRangeInclude; @@ -106,6 +107,7 @@ import org.springframework.data.solr.core.query.result.StatsResult; import org.springframework.data.solr.core.query.result.TermsFieldEntry; import org.springframework.data.solr.core.query.result.TermsPage; +import org.springframework.data.solr.server.support.MulticoreSolrClientFactory; import org.xml.sax.SAXException; import com.google.common.collect.Lists; @@ -1259,6 +1261,23 @@ public void testFindByNameWithSpellcheckSeggestion() { Assert.assertThat(found.getSuggestions(), Matchers.contains("green")); } + @Test // DATSOLR-364 + public void shouldUseBaseUrlInCollectionCallbackWhenExecutingCommands() { + + final HttpSolrClient client = new HttpSolrClient("http://127.0.0.1/solr/"); + + SolrTemplate solrTemplate = new SolrTemplate(new MulticoreSolrClientFactory(client), "collection-1"); + + solrTemplate.execute("collection-1", new CollectionCallback() { + @Override + public Object doInSolr(SolrClient solrClient, String collection) throws SolrServerException, IOException { + + Assert.assertThat(((HttpSolrClient)solrClient).getBaseURL(), is("http://127.0.0.1/solr")); + return null; + } + }); + } + private void executeAndCheckStatsRequest(StatsOptions statsOptions) { ExampleSolrBean bean1 = new ExampleSolrBean("id-1", "one", null);