0

I am using spring data solr 3.0.6, and standalone solr server 7.0.0 and I have multiple solr cores in my solr server, and I want to choose one of them dynamically , Here is the configuration I Have

@Configuration
@EnableSolrRepositories(basePackages = "com.solr.repository")
public class SolrConfiguration
{


 @Bean
  public SolrOperations solrTemplate(SolrClient solr) {
    return new SolrTemplate(solr);
  }



  @Bean(name = "solrClient")
  public SolrClient createSolrClient()
  {
     HttpSolrClient.Builder builder = new HttpSolrClient.Builder().withBaseSolrUrl(solrInstanceUrl);
    return builder.build();
}

}

SolrRepository

public interface SolrRepository extends SolrCrudRepository<SolrDocument, Integer>
{
 @Query("name:*?0* OR content:*?0*")
 @Highlight()
  public HighlightPage<SolrDocument> findByQueryAnnotation(String searchTerm, Pageable pageable);
}

SolrDocument (domain class for Solr repository)

@SolrDocument
public class SolrDocument
{
}

ServicePojo (service class)

 public class ServicePojo
    {
        @Autowired
        SolrRepository solrRepository;

        public void findData(int id)
        {
           solrRepository.findById(id);
         }
    }

Now I want to use methods of repository interface like findById() etc, but as I mentioned above, I have different cores, and I want to point a specific core to perform searching, before calling method solrRepository.findById() I need to mention which core it should point. So where we can tell to solr server , which Core to be used ?

If I use annotation @SolrDocument(collectionName="core1"), then it works fine and it points to "core1", but I want this to be dynamic. Please help

user3029929
  • 471
  • 2
  • 7
  • 20

1 Answers1

0

You cannot do this using Repository (SolrRepository). But you can do this using SolrTemplate.

Here is the link to the 3.0.6 SolrTemplate API. If you look at some of the methods like query or getById, it takes an argument collectionName. You can pass coreName here.

pvpkiran
  • 25,582
  • 8
  • 87
  • 134
  • I was doing this stuff in previous version of spring-data-solr (3.0.0.M1) with MulticoreSolrClientFactory, but in the latest version 3.0.6, it doesnt seems possible. if you say its possible in Solrtemplate then please let me know how ? – user3029929 Apr 23 '18 at 09:47
  • the reason i wanted to use solrRepository is because I need to use custom queries (I have updated my above solrRepository class). so if solrTemplate registers collectionName in it or in solrClient, then I can generate solrRepository from SolrRepositoryFactory. But using direct solrTemplate will not not help me SolrRepositoryFactory solrRepositoryFactory = new SolrRepositoryFactory(solrTemplate); solrRepository = solrRepositoryFactory.getRepository(SolrRepository.class); – user3029929 Apr 23 '18 at 09:57
  • Not sure if i understood your point. You can keep your repository class. But you can create a custom repository (https://docs.spring.io/spring-data/solr/docs/current/reference/html/#repositories.custom-implementations) where you Autowire SolrTemplate and write your custom queries. – pvpkiran Apr 23 '18 at 10:00
  • above SolrRepository itself is a custom repository. Query features like "highlights" are being provided by "data.solr.repository" API, so I have to use repositories interfaces to use my custom queries, if i autowire solrTemplate then I can use only methods which are provided by solrTemplate , I wont be able to use other features of reposiories interface which i can get by writing my own queries (just have look a query above in SolrRepository class) – user3029929 Apr 23 '18 at 10:08
  • Everything which can be done by SolrRepository can be done using SolrTemplate. May be u need to write a bit more code. – pvpkiran Apr 23 '18 at 10:08
  • yes, but it become very difficult and combersome, if i need to search something by request paramater then solrTemplate is not that easy to manage, So I was looking for core to be registsred by solrTemplate or solrClient. It seems to be possible with solrTemplate.setSolrConverter() may be , but I am not sure ,, – user3029929 Apr 23 '18 at 10:12
  • Ya. But there is no other way. If you wish to change core name dynamically, u cannot use repository class. It is same for CRUDRepository, MongoRepository, .....etc – pvpkiran Apr 23 '18 at 10:16