当前solr版本:8.8.1
,SpringBoot版本:2.1.18.RELEASE
1.声明
当前内容主要用于本人学习和使用SpringBoot-data方式操作当前的solr,以及记录其中出现的问题
基本问题:
-
java.lang.ClassNotFoundException:org.apache.solr.client.solrj.embedded.EmbeddedSolrServer
解决办法在pom中添加solr-core依赖即可,对应solr版本:<dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-core</artifactId> <version>8.8.1</version> </dependency> ```
-
Caused by: java.lang.ClassNotFoundException: net.minidev.json.writer.JsonReaderI
解决办法:在pom文件中添加json-smart的依赖即可<dependency> <groupId>net.minidev</groupId> <artifactId>json-smart</artifactId> <version>2.3</version> </dependency>
-
一般访问404问题
解决办法:
1.缺少collection,即与实体类对应的,需要手动创建
2.访问url错误,有的时候重复了访问url
添加collection方式:
2.SpringBoot-data操作solr的demo
1.基本pom依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.18.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.solr/solr-core -->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-core</artifactId>
<version>8.8.1</version>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<!-- <version>4.10.4</version> -->
</dependency>
<!-- https://mvnrepository.com/artifact/net.minidev/json-smart -->
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
2.最重要的配置类:SolrConfig.java
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.solr.core.SolrOperations;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.repository.config.EnableSolrRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* @author admin
* @createTime 2021-02-27 17:00:58
* @description 当前内容主要是solr的配置类
*
*/
@Configuration
@EnableSolrRepositories(basePackages = "com.hy.springboot.solr.dao")
@EntityScan(basePackages = "com.hy.springboot.solr.entity")
@EnableTransactionManagement
public class SolrConfig {
@Bean
public SolrClient solrClient() {
HttpSolrClient httpSolrClient = new HttpSolrClient.Builder("http://192.168.1.100:8983/solr").build();
return httpSolrClient;
}
@Bean
public SolrOperations solrTemplate(){
return new SolrTemplate(solrClient());
}
}
这里必须使用:http://XXX:8983/solr方式,实际访问会追加实体类的collection
3.实体类:User.java
import java.util.Date;
import org.apache.solr.client.solrj.beans.Field;
import org.springframework.data.annotation.Id;
import org.springframework.data.solr.core.mapping.SolrDocument;
@SolrDocument(collection = "users")
public class User {
@Id
private Integer id;
@Field
private String name;
@Field
private Date birth;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
}
注意此处,一定要有@Id
,和需要添加到solr的字段@Field如果不加@Field,那么该字段不会写入到solr中存储!
4.实际的dao层:UserRepository.java
import org.springframework.data.solr.repository.SolrCrudRepository;
import org.springframework.data.solr.repository.SolrRepository;
import org.springframework.stereotype.Repository;
import com.hy.springboot.solr.entity.User;
@Repository
public interface UserRepository extends SolrCrudRepository<User, Integer> {
}
5.Controller层:
@RestController
public class UserController {
@Autowired
UserRepository userRepository;
@RequestMapping(value = "/findAllUser")
public Object getAllUser() {
Iterable<User> findAll = userRepository.findAll();
return findAll;
}
@RequestMapping("/saveUser")
public Object getAllUser(User user) {
userRepository.save(user);
return "{\"msg\":\"添加数据成功\"}";
}
}
6.启动类:
/**
* @author admin
* @createTime 2021-02-27 14:44:23
* @description 主要用于测试和使用SpringBoot -data操作Solr
* 当前操作非常成功
*
*/
@SpringBootApplication
public class SpringBootSolrApp
{
public static void main( String[] args )
{
SpringApplication.run(SpringBootSolrApp.class, args);
}
}
3.执行结果
任何执行都成功,并且按照主键Id方式添加和修改数据…
4.总结
- 通过长时间测试,
发现solr中collection就代表一个表,对应实体类的@SolrDocument(collection = "users"),并且需要手动创建这个collection
- 必要的@Field注解,可以让对象的属性写入到solr中
- 小心访问solr的url的问题