【问题标题】:Spring Data Solr multiple cores and repositorySpring Data Solr 多核和存储库
【发布时间】:2013-05-31 14:03:11
【问题描述】:

我有多个内核的 apache solr,例如货币、国家等...所以使用 Spring Data Solr 我可以从一个核心检索信息。我现在有这个 XML 配置查询 'currency' 核心。如果我想查询 'country' 核心,我该如何设置?

<!-- Enable Solr repositories and configure repository base package -->
<solr:repositories base-package="com.acme.repository" solr-template-ref="solrCurrencyTemplate"/>

<solr:solr-server id="solrCurrencyServer" url="http://localhost:8983/solr/currency"/>

<bean id="solrCurrencyTemplate" class="org.springframework.data.solr.core.SolrTemplate">
    <constructor-arg ref="solrCurrencyServer" />
</bean>

并将存储库定义为

@Repository
public interface CurrencyRepository extends SolrCrudRepository<Currency, String> {

}

通过我的服务,我可以做到这一点

@Override
public List<Currency> getCurrencies() {
    Page<Currency> currencies = (Page<Currency>) currencyRepository.findAll();
    return currencies.getContent();
}

我也尝试过使用 @SolrDocument(solrCoreName = "currency"),但这不起作用。

@SolrDocument(solrCoreName = "currency")
public class Currency {
    public static final String FIELD_CURRENCY_NAME = "currency_name";
    public static final String FIELD_CURRENCY_CODE = "currency_code";
    public static final String FIELD_DECIMALS = "decimals";

    @Id
    @Field(value = FIELD_CURRENCY_CODE)
    private String currencyCode;

    //currency_name,decimals
    @Field(value = FIELD_CURRENCY_NAME)
    private String currencyName;

    @Field(value = FIELD_DECIMALS)
    private String decimals;

...
...
...
}

我需要尽快帮助...否则我将不得不回到 RestTemplate 解决方案:-(

希望有人可以提供帮助。 谢谢 通用汽车

【问题讨论】:

    标签: spring spring-data spring-data-solr


    【解决方案1】:

    我想分享一下,我们最近花了很多时间配置多个内核。我们是用 java 做的,而不是 xml。

    作为 spring @configuration 的一部分添加以下内容。

    @Bean(name="solrCore1Template")
    public SolrTemplate solrCore1Template() throws Exception {
        EmbeddedSolrServer embeddedSolrServer = new EmbeddedSolrServer(getCoreContainer(), "core1");
        return new SolrTemplate(embeddedSolrServer);
    }
    
    @Bean(name="solrCore2Template")
    public SolrTemplate solrCore2Template() throws Exception {   
        EmbeddedSolrServer embeddedSolrServer = new EmbeddedSolrServer(getCoreContainer(), "core2");
        return new SolrTemplate(embeddedSolrServer);
    }
    
    @Bean
    @Scope
    public CoreContainer getCoreContainer() throws FileNotFoundException{
        String dir = <path_to_solr_home>;
        System.setProperty("solr.solr.home", dir);
        CoreContainer.Initializer initializer = new CoreContainer.Initializer();
        return initializer.initialize();
    }
    

    并在服务类中使用每个模板,如下所示。

    @Resource
    private SolrTemplate solrCore1Template;
    

    嵌入式服务器可以使用以下代码替换为 HTTP。

    HttpSolrServer httpSolrServer = new HttpSolrServer(getSolrURL());
    return new SolrTemplate(httpSolrServer, "core1");
    

    希望这会有所帮助。我知道对于所提出的问题,这是一个很晚的答复。

    【讨论】:

      【解决方案2】:

      不幸的是,通过命名空间配置支持多核是一个悬而未决的问题。您需要为每个核心拥有一个单独的 SolrTemplate 并手动创建存储库。

      @Autowired 
      @Qualifier("solrCurrencyTemplate")
      private SolrTemplate solrCurrencyTemplate;
      
      @Autowired
      @Qualifier("solrCountryTemplate")
      private SolrTemplate solrCountryTemplate;
      
      //...
      
      CurrencyRepository currencyRepo = new SolrRepositoryFactory(this.solrCurrencyTemplate)
        .getRepository(CurrencyRepository.class);
      
      CountryRepository countryRepo = new SolrRepositoryFactory(this.solrCountryTemplate)
        .getRepository(CountryRepository.class);
      

      【讨论】:

      • 多核还不支持?谢谢。
      • 近期是否有计划增加对多核的支持?
      • 是的,我们计划提供更好的多核支持。作为第一步,MulticoreSolrServerFactory 可用于SolrTempate
      • Spring Data Solr 1.1.0.RC1 通过@EnableSolrRepositories(basePackages = {"org.springframework.data.solr.showcase"}, multicoreSupport=true) 提供扩展的多核支持,它从@SolrDocument 读取核心/集合信息,并与存储库一起创建所需的SolrTemplates。配置中唯一必须存在的是指向 solr 基本路径的 SolrServer,因为将添加集合信息。
      • @ChristophStrobl 您的方法可行,但如何为特定集合自动装配propper solrTemplate?我总是以春季异常“找不到合格的bean”结束......是否创建了solrTemplate bean?谢谢。
      【解决方案3】:

      Spring Data 现在支持多个内核及其各自的存储库。

      @EnableSolrRepositories 注解中的 multicoreSupport 标志需要为真,并且需要告诉相应的文档它们属于哪个核心。喜欢:

      @SolrDocument(solrCoreName = "currency")
      public class Currency
      {
          // attributes
      }
      

      另一个类应该是

      @SolrDocument(solrCoreName = "country")
      public class Country
      {
          // attributes
      }
      

      各自的存储库应该知道他们正在使用什么 pojo。

      public interface CurrencyRepository extends SolrCrudRepository<Currency,String>
      {
      }
      

      public interface CountryRepository extends SolrCrudRepository<Country,String>
      {
      }
      

      配置应该是

      @Configuration
      @EnableSolrRepositories(value = "com.package.name",multicoreSupport = true)
      public class SolrConfig
      {
          @Bean
          public SolrServer solrServer() throws Exception
          {
              HttpSolrServerFactoryBean f = new HttpSolrServerFactoryBean();
              f.setUrl("http://localhost:8983/solr");
              f.afterPropertiesSet();
              return f.getSolrServer();
          }
      
          @Bean
          public SolrTemplate solrTemplate(SolrServer solrServer) throws Exception
          {
              return new SolrTemplate(solrServer());
          }
      }
      

      【讨论】:

        【解决方案4】:

        在 Spring Data Solr 1.1.0.RC1 中,多核工作如 Christoph Strobl 所述,@EnableSolrRepositories。它也适用于通过 set multicore-support="true" 进行的 XML 配置。

        <solr:repositories base-package="your.solr.repo.package" repository-impl-postfix="Impl" multicore-support="true"/>
        
        <solr:solr-server id="solrServer" url="${solr.server.base.connection.url}" />
        
        <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
            <constructor-arg index="0" ref="solrServer" />
        </bean>
        

        【讨论】:

          【解决方案5】:
          <solr:solr-server id="solrServer" timeout="1000" maxConnections="1000" url="${solr.server.1},${solr.server.2}"/>
          
          <bean id="solrServerFactory" class="org.springframework.data.solr.server.support.MulticoreSolrServerFactory">
              <constructor-arg ref="solrServer" />
              <constructor-arg name="cores">
                  <list>
                      <value>${solr.index.customer}</value>
                      <value>${solr.index.task}</value>
                  </list>
              </constructor-arg>
          </bean>
          
          <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
              <constructor-arg ref="solrServerFactory" />
          </bean>
          
          <solr:repositories base-package="com.deve.pig.solr" multicore-support="true" solr-template-ref="solrTemplate" />
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-09-15
            • 2018-08-06
            • 1970-01-01
            • 2016-12-26
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多