springboot整合es详细步骤
时间: 2023-07-05 20:20:27 浏览: 144
好的,下面是springboot整合es的详细步骤:
1. 在pom.xml文件中添加elasticsearch和spring-data-elasticsearch依赖。
```xml
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>版本号</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>版本号</version>
</dependency>
```
2. 在application.yml文件中添加ES配置信息。
```yaml
spring:
elasticsearch:
rest:
uris: http://localhost:9200
username: 用户名
password: 密码
```
3. 创建一个ES实体类,并使用注解来定义索引名、类型名和字段属性等。
```java
@Document(indexName = "blog", type = "article")
public class Article {
@Id
private Long id;
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String title;
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String content;
@Field(type = FieldType.Keyword)
private String author;
@Field(type = FieldType.Date)
private Date createTime;
// getter和setter方法省略
}
```
4. 创建一个ES的Repository接口,继承ElasticsearchRepository,并定义一些自定义查询方法。
```java
public interface ArticleRepository extends ElasticsearchRepository<Article, Long> {
List<Article> findByTitle(String title);
List<Article> findByContent(String content);
List<Article> findByAuthor(String author);
List<Article> findByCreateTimeBetween(Date begin, Date end);
}
```
5. 在Service中调用Repository的方法,实现对ES的CRUD操作。
```java
@Service
public class ArticleService {
@Autowired
private ArticleRepository articleRepository;
public void save(Article article) {
articleRepository.save(article);
}
public void deleteById(Long id) {
articleRepository.deleteById(id);
}
public Optional<Article> findById(Long id) {
return articleRepository.findById(id);
}
public List<Article> findByTitle(String title) {
return articleRepository.findByTitle(title);
}
public List<Article> findByContent(String content) {
return articleRepository.findByContent(content);
}
public List<Article> findByAuthor(String author) {
return articleRepository.findByAuthor(author);
}
public List<Article> findByCreateTimeBetween(Date begin, Date end) {
return articleRepository.findByCreateTimeBetween(begin, end);
}
}
```
6. 测试ES的CRUD操作。
```java
@RunWith(SpringRunner.class)
@SpringBootTest
public class ArticleServiceTest {
@Autowired
private ArticleService articleService;
@Test
public void save() {
Article article = new Article();
article.setId(1L);
article.setTitle("Spring Boot 整合 Elasticsearch");
article.setContent("Spring Boot 整合 Elasticsearch 详细步骤");
article.setAuthor("张三");
article.setCreateTime(new Date());
articleService.save(article);
}
@Test
public void deleteById() {
articleService.deleteById(1L);
}
@Test
public void findById() {
Optional<Article> optional = articleService.findById(1L);
if (optional.isPresent()) {
System.out.println(optional.get().getTitle());
}
}
@Test
public void findByTitle() {
List<Article> list = articleService.findByTitle("Spring Boot");
for (Article article : list) {
System.out.println(article.getTitle());
}
}
@Test
public void findByContent() {
List<Article> list = articleService.findByContent("详细步骤");
for (Article article : list) {
System.out.println(article.getTitle());
}
}
@Test
public void findByAuthor() {
List<Article> list = articleService.findByAuthor("张三");
for (Article article : list) {
System.out.println(article.getTitle());
}
}
@Test
public void findByCreateTimeBetween() {
Date begin = new Date(0L);
Date end = new Date();
List<Article> list = articleService.findByCreateTimeBetween(begin, end);
for (Article article : list) {
System.out.println(article.getTitle());
}
}
}
```
以上就是springboot整合es的详细步骤,希望对你有所帮助。
阅读全文