索引期间的 SOLR 错误 java.lang.ClassCastException

SOLR error java.lang.ClassCastException during indexing

这是我的 SOLR 数据模型,

@SolrDocument(solrCoreName = "solrData")
public class SolrData {

@Id
@Indexed(name = "id", type = "string")
String id;

@Indexed(name = "name", type = "string")
String name;

这个SOLR配置,

@Configuration
@EnableSolrRepositories(basePackages={"com.ows.repository.solrRepository"}, multicoreSupport=true)
@ComponentScan

public class SolrConfig {
    static final String SOLR_HOST = "http://localhost:8983/solr/";

    @Bean
    public SolrClient solrClient() {
        return new HttpSolrClient.Builder(SOLR_HOST).build();
    }

    @Bean
    public SolrTemplate solrTemplate(SolrClient solrClient) throws Exception {
        return new SolrTemplate(solrClient);
    }
}

存储库,

public interface SolrProductRepository extends SolrCrudRepository<SolrData, String> {
    List<SolrData> findByName(String name);
}

索引控制器,

@Autowired
SolrProductRepository solrProductRepository;

@RequestMapping("/solrindex")
public void solrIndex(Model model) {
        SolrData solrData = new SolrData();
        solrData.setName("You know Who");
        solrProductRepository.save(solrData);
}

POM.xml

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-solr</artifactId>
        <version>2.1.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.apache.solr</groupId>
        <artifactId>solr-common</artifactId>
        <version>1.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.solr</groupId>
        <artifactId>solr-solrj</artifactId>
        <version>6.6.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.10.RELEASE</version>
    </dependency>

使用上面的设置,当我使用它所说的索引控制器进行索引时,

更新了完整的错误消息。

 org.springframework.data.solr.UncategorizedSolrException: org.apache.solr.common.SolrInputDocument cannot be cast to java.util.Map; nested exception is java.lang.ClassCastException: org.apache.solr.common.SolrInputDocument cannot be cast to java.util.Map
    at org.springframework.data.solr.core.SolrTemplate.execute(SolrTemplate.java:224)
    at org.springframework.data.solr.core.SolrTemplate.saveBean(SolrTemplate.java:330)
    at org.springframework.data.solr.core.SolrTemplate.saveBean(SolrTemplate.java:318)
    at org.springframework.data.solr.core.SolrTemplate.saveBean(SolrTemplate.java:300)
    at org.springframework.data.solr.repository.support.SimpleSolrRepository.save(SimpleSolrRepository.java:149)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:504)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:489)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461)

 Caused by: java.lang.ClassCastException: org.apache.solr.common.SolrInputDocument cannot be cast to java.util.Map
    at org.springframework.data.solr.core.convert.MappingSolrConverter.write(MappingSolrConverter.java:62)
    at org.springframework.data.solr.core.SolrTemplate.convertBeanToSolrInputDocument(SolrTemplate.java:1132)
    at org.springframework.data.solr.core.SolrTemplate.doInSolr(SolrTemplate.java:335)
    at org.springframework.data.solr.core.SolrTemplate.doInSolr(SolrTemplate.java:330)
    at org.springframework.data.solr.core.SolrTemplate.execute(SolrTemplate.java:220)
    ... 129 more

已解决

我通过以下设置解决了问题,

唯一的配置文件更改为以下,

@Configuration
@EnableSolrRepositories(basePackages={"com.ows.rokomari.repository.solrRepository"}, multicoreSupport=true)
@ComponentScan
public class SolrConfig {

static final String SOLR_HOST = "http://localhost:8983/solr";

@Bean
public SolrClient solrClient() {

return new HttpSolrClient(SOLR_HOST);
}

@Bean
public SolrTemplate solrTemplate(SolrClient solrClient) throws Exception {
return new SolrTemplate(solrClient);
}
}

pom.xml 文件更改为以下设置,

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-solr</artifactId>
  <version>2.1.6.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.apache.solr</groupId>
  <artifactId>solr-common</artifactId>
  <version>1.3.0</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>4.3.10.RELEASE</version>
</dependency>

现在一切都很好

我包含了 solr-common,因为除此之外项目会抛出错误。由于我的项目是版本 4 上的 spring 和 运行 以及其他一些旧的依赖项。估计是更新后的Solr相关依赖和现有依赖冲突了,这个解决了

我使用了 spring-data-solr,它与使用 Solrj 有点不同。可以找到 Solrj 实现 here