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

Commit 56a87bd

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 7e077c3 commit 56a87bd

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
import org.springframework.data.solr.core.schema.SolrPersistentEntitySchemaCreator.Feature;
8787
import org.springframework.data.solr.server.SolrClientFactory;
8888
import org.springframework.data.solr.server.support.HttpSolrClientFactory;
89+
import org.springframework.data.solr.server.support.MulticoreSolrClientFactory;
8990
import org.springframework.util.Assert;
9091
import org.springframework.util.CollectionUtils;
9192
import org.springframework.util.StringUtils;
@@ -206,7 +207,17 @@ public <T> T execute(String collection, CollectionCallback<T> action) {
206207
Assert.notNull(action, "Action must not be null!");
207208

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

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.apache.solr.client.solrj.SolrClient;
4141
import org.apache.solr.client.solrj.SolrServerException;
4242
import org.apache.solr.client.solrj.beans.Field;
43+
import org.apache.solr.client.solrj.impl.HttpSolrClient;
4344
import org.apache.solr.common.SolrInputDocument;
4445
import org.apache.solr.common.params.FacetParams;
4546
import org.apache.solr.common.params.FacetParams.FacetRangeInclude;
@@ -109,6 +110,7 @@
109110
import org.springframework.data.solr.core.query.result.StatsResult;
110111
import org.springframework.data.solr.core.query.result.TermsFieldEntry;
111112
import org.springframework.data.solr.core.query.result.TermsPage;
113+
import org.springframework.data.solr.server.support.MulticoreSolrClientFactory;
112114
import org.xml.sax.SAXException;
113115

114116
import com.google.common.collect.Lists;
@@ -1265,6 +1267,23 @@ public void testFindByNameWithSpellcheckSeggestion() {
12651267
Assert.assertThat(found.getSuggestions(), Matchers.contains("green"));
12661268
}
12671269

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

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

0 commit comments

Comments
 (0)