引入依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>2.7.18</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.7.18</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.7.18</version>
</dependency>
application.properties
spring.elasticsearch.rest.uris=http://127.0.0.1:9200
spring.data.elasticsearch.repositories.enabled=true
Repository
package com.elastictsearch.repository;
import com.elastictsearch.Article;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ArticleRepository extends ElasticsearchRepository<Article, String> {
// 根据作者名称分页搜索
Page<Article> findByAuthorsName(String name, Pageable pageable);
Page<Article> findArticleByTitle(String title, Pageable pageable);
int deleteArticleByTitle(String title);
}
测试类
package com;
import com.elastictsearch.Article;
import com.elastictsearch.Author;
import com.elastictsearch.repository.ArticleRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Arrays;
@SpringBootTest
@RunWith(SpringRunner.class)
public class EsTest {
@Autowired
private ArticleRepository articleRepository;
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
// 使用repository实现的增删改查
@Test
public void testSave() {
Article article = new Article("Spring Data ElasticSearch");
article.setAuthors(Arrays.asList(new Author("god"), new Author("john")));
articleRepository.save(article);
article = new Article("Spring Data ElasticSearch2");
article.setAuthors(Arrays.asList(new Author("god"), new Author("king")));
articleRepository.save(article);
article = new Article("Spring Data ElasticSearch3");
article.setAuthors(Arrays.asList(new Author("god"), new Author("Bill")));
articleRepository.save(article);
}
@Test
public void testSelect() {
Page<Article> god = articleRepository.findByAuthorsName("god", PageRequest.of(0, 10));
for (Article article: god) {
System.out.println("11111" + article);
for (Author author : article.getAuthors()) {
System.out.println("222222" + author.getName());
}
}
}
@Test
public void testSelect2() {
Page<Article> god = articleRepository.findArticleByTitle("Spring Data ElasticSearch2", PageRequest.of(0, 10));
for (Article article: god) {
System.out.println("11111" + article);
for (Author author : article.getAuthors()) {
System.out.println("222222" + author.getName());
}
}
}
@Test
public void testUpdate() {
Page<Article> god = articleRepository.findArticleByTitle("Spring Data ElasticSearch2", PageRequest.of(0, 10));
Article article1 = god.getContent().get(0);
System.out.println("article1=" + article1);
Author author = new Author("chali");
article1.setAuthors(Arrays.asList(author));
articleRepository.save(article1);
}
@Test
public void testDeletge() {
Page<Article> god = articleRepository.findArticleByTitle("Spring Data ElasticSearch2", PageRequest.of(0, 10));
Article article1 = god.getContent().get(0);
System.out.println("article1=" + article1);
articleRepository.delete(article1);
}
@Test
public void deleteByTitle() {
int i = articleRepository.deleteArticleByTitle("Spring Data ElasticSearch");
System.out.println("deleteByTitle=" + i);
}
}
遇到的问题
报错
- Attribute ‘proxyBeanMethods’ in annotation [```] is declared as an @AliasFor nonexistent attribute
- 类文件具有错误的版本 61.0, 应为 52.0
以上两个问题都是因为版本,第一个百度说是spring-context的版本问题,第二个是springboot版本问题,需要降到3.0以下