2017-04-24 20 views
1

스프링 부트를 사용하여 응용 프로그램을 만들고 Solr에 쿼리하고 문서를 추가하려고합니다. 그래서 저는 Spring 데이터 solr을 사용합니다. 의존 관계는 다음과 같습니다.커스텀 스프링 데이터 솔라 저장소를 구현할 때 SolrOperations 빈 삽입 방법?

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-data-solr</artifactId> 
</dependency> 

그런 다음 Solr 저장소 구성을위한 구성 클래스를 만듭니다. 봄 덕분에 모든 것이 간단하고 잘 작동합니다.

@Configuration 
@EnableSolrRepositories(basePackages = { "xxx.xx.xx.resource.repository.solr" },multicoreSupport = true) 
public class SolrConfiguration { 

    @Bean 
    public SolrClient solrClient(@Value("${solr.host}") String solrHost) { 
     return new HttpSolrClient(solrHost); 
    } 

} 

기본 변환기가 중첩 된 Java 객체를 변환 할 수 없으므로 문서 저장을위한 사용자 정의 함수를 추가하고 싶습니다. 나는 저장을 위해 SolrClient bean 또는 SolrTemplate 빈을 사용할 계획이다.

public class HouseSolrRepositoryImpl extends SimpleSolrRepository<House, String> implements HouseSolrRepository{ 

@Autowired 
private SolrClient solrClient; 

    @Override 
    public House save(House house) throw Exception{ 
     // Do converting 
     solrClient.save(doc); 
     solrClient.commit(); 
     return house;/ 
    } 
} 

그러나 autowire가 SolrClient의 경로는 문서 객체 (@Document(solrCoreName = "gettingstarted"))에서 경로에 solrCoreName을하지 않습니다. 그냥 http://localhost:8983/solr으로 요청하지만 핵심 이름은 http://localhost:8983/solr/gettingstarted이 아닙니다.

초기화 리포지토리 빈 중에는 solrCoreName이 설정되어 있으므로 내 구성에 포함되지 않습니다.

한편 SimpleSolrRepositorySolrOperation 콩도 null이되며 findOne()과 같은 다른 쿼리는 제대로 작동하지 않습니다.

답변

0

스프링 데이터의 개념 중 일부가 오리지널 SolrOperations에서 사용해서는 안되는 것 같습니다.

우리는 SolrJ의 SolrClient를 스프링 데이터와 함께 사용할 수 있습니다. 여기 간단한 해결책이 있습니다.

Solr에 여러 개의 코어가 있으므로 구성 할 때 모든 코어에 대해 SolrClient (이전 버전의 SolrServer를 대체)을 만듭니다.

@Bean 
public SolrClient gettingStartedSolrClient(@Value("${solr.host}") String solrHost){ 
    return new ConcurrentUpdateSolrClient(solrHost+"/gettingstarted"); 
} 

@Bean 
public SolrClient anotherSolrClient(@Value("${solr.host}") String solrHost){ 
    return new ConcurrentUpdateSolrClient(solrHost+"/anotherCore"); 
} 

그런 다음 우리는 자동으로 묶어 SolrClient 콩에 의해 우리의 SOLR의 DAO 클래스에 사용할 수 있습니다.

+0

효과가 있습니까? 제 경우에는 메소드를 호출 할 때마다 새로운 인스턴스를 생성합니다. – Maralc