2016-02-13 42 views

回答

4

需要用於彈簧數據Solr的唯一依賴是

<dependency> 
    <groupId>org.springframework.data</groupId> 
    <artifactId>spring-data-solr</artifactId> 
    <version>1.5.2.RELEASE</version> 
</dependency> 

它下載solrj依賴和這不能與更高版本solrj的覆蓋。 另外它總是最好使用HttpSolrServer而不是EmbeddedSolrServer,這是我們將要使用的。

Configuration類應該是這樣的:

@Configuration 
@EnableSolrRepositories(value = "com.package.",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()); 
    } 
} 

文檔實體應包含有關其所屬

@SolrDocument(solrCoreName = "core1") 
public class Document1 
{ 
    @Id 
    @Field 
    private String id; 

    /**other attributes**/ 
} 

其核心信息的其他文件應

@SolrDocument(solrCoreName = "core2") 
public class Document2 
{ 
    @Id 
    @Field 
    private String id; 

    /**other attributes**/ 
} 

現在最好的部分是你已經完成了。只需在普通的舊的方式建立信息庫做的伎倆

public interface SolrCore1Repository extends SolrCrudRepository<Document1,String> 
{ 
    // optional code 
} 

其他回購就像

public interface SolrCore2Repository extends SolrCrudRepository<Document2,String> 
{ 
    // optional code 
} 

一旦Solr的是在指定的網址中運行,並根據POJO有田,你是完成。

相關問題