如何使用 Spring Data Solr 为多个内核和存储库实现自定义 Solr 存储库 [英] How to Implement custom Solr Repository using Spring Data Solr for multiple cores and repositories

查看:30
本文介绍了如何使用 Spring Data Solr 为多个内核和存储库实现自定义 Solr 存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 spring-data-solr 在一项服务中访问多个/2 个存储库.从 Spring Data Solr 多核和存储库我知道'多核不幸的是,通过命名空间配置支持是一个悬而未决的问题.

I want to have access to multiple/2 repos in one service using spring-data-solr. From Spring Data Solr multiple cores and repository I know 'multicore support via namespace config is unfortunately an open issue'.

你能帮我看看下面的例子吗,我如何创建自定义 Repos?

Can you please help me with the following example, how can I create custom Repos?

我的 applicationContext.xml 有两个 Solr 模板,定义如下:

My applicationContext.xml has two Solr Templates defined as below:

<!-- Enable Solr repositories and configure repository base package -->
<solr:repositories base-package="com.ay.api.repository"/>

<!-- Configures HTTP Solr server -->
<solr:solr-server id="solrServer" url="${solr.server.url}"/>

<!-- Configures Solr Events template -->
   <bean id="solrEventsTemplate" class="org.springframework.data.solr.core.SolrTemplate">
   <qualifier type="solrEventsTemplate"/>
   <constructor-arg index="0" ref="solrServer"/>
   <constructor-arg index="1" value="${solr.server.events.core.name}"/>
</bean>

<!-- Configures Solr Towns template -->
<bean id="solrTownsTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg index="0" ref="solrServer"/>
<constructor-arg index="1" value="${solr.server.towns.core.name}"/>
</bean>

我有以下回购

@Repository
public class EventDocumentRepositoryImpl implements EventSearchRepository { 


@Resource
@Qualifier("solrEventsTemplate")
private SolrTemplate solrEventsTemplate;

...
}

public interface EventDocumentRepository extends EventSearchRepository, SolrCrudRepository<EventDocument, String> {

}

public interface EventSearchRepository { .... }


@Repository
public class TownRepositoryImpl implements TownSearchRepository { ... 

@Resource
@Qualifier("solrTownsTemplate")
private SolrTemplate solrTownsTemplate;

...
}

public interface TownRepository extends SolrCrudRepository<TownDocument, String>{}
public interface TownSearchRepository { .... }

最后服务如下所示:

 @Service
 public class SearchEventServiceImpl implements SearchEventService {

 @Resource
 private EventDocumentRepository eventRepository;

 @Resource
 private TownRepository townRepository;
 .....
 }

有人可以建议我如何修改我的代码以实现 带有 Solr 4.1 多核的 Spring Data Solr ?因为我无法理解该线程中建议的解决方案.

Can someone please advice how can I modify my code to Implement Custom repositories as mentioned in Spring Data Solr with Solr 4.1 multicores ? as I couldn't understand suggested solution in this thread.

非常感谢.

推荐答案

存储库扫描将查找 solrTemplate 并使用提供的模板创建存储库.由于每个 Solr 核心都需要一个模板,因此您必须手动创建模板和存储库.

Repository scanning will look for solrTemplate and create repository using provided template. As you need one template for each Solr core, you'll have to create both, template and repository manually.

首先创建您的存储库和自定义实现.

First create your repositories and custom implementations.

public interface EventRepositoryCustom {

    Page<Event> findEvent();

}

public interface EventRepository extends EventRepositoryCustom, SolrCrudRepository<Event, String> {

}

public class EventRepositoryImpl implements EventRepositoryCustom {

    private SolrTemplate eventTemplate;

    public EventRepositoryImpl(SolrTemplate eventTemplate) {
        this.eventTemplate = eventTemplate;
    }

    @Override
    public Page<Event> findEvent() {
        return eventTemplate.queryForPage(new SimpleQuery("*:*"), Event.class);
    }

}

对您的 TownRepository 执行相同的操作.

Do the same for your TownRepository.

使用 Java Config 进行配置.使用 XML 也可以做到这一点.

Using Java Config for configuration. Same can be done with XML.

@Configuration
public class SolrContext {

  private static final String PROPERTY_NAME_SOLR_SERVER_URL = "solr.host";

  @Resource
  private Environment environment;

  // Factory creates SolrServer instances for base url when requesting server
  // for specific core. 
  @Bean
  public SolrServerFactory solrServerFactory() {
    return new MulticoreSolrServerFactory(new HttpSolrServer(
            environment.getRequiredProperty(PROPERTY_NAME_SOLR_SERVER_URL)));
  }

  // SolrTemplate for /solrServerUrl/towns
  @Bean
  public SolrTemplate townTemplate() throws Exception {
    SolrTemplate solrTemplate = new SolrTemplate(solrServerFactory());
    solrTemplate.setSolrCore("towns");
    return solrTemplate;
  }

  // SolrTemplate for /solrServerUrl/events
  @Bean
  public SolrTemplate eventTemplate() throws Exception {
    SolrTemplate solrTemplate = new SolrTemplate(solrServerFactory());
    solrTemplate.setSolrCore("events");
    return solrTemplate;
  }

  @Bean
  public EventRepository eventRepository() throws Exception {
    return new SolrRepositoryFactory(eventTemplate())
      .getRepository(EventRepository.class, new EventRepositoryImpl(eventTemplate()));
  }

  @Bean
  public TownRepository townRepository() throws Exception {
    return new SolrRepositoryFactory(townTemplate())
      .getRepository(TownRepository.class, new TownRepositoryImpl(townTemplate()));
  }
}

这篇关于如何使用 Spring Data Solr 为多个内核和存储库实现自定义 Solr 存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆