新建一个文章类
@Data
@NoArgsConstructor
@Document(indexName = "article", type = "article")
public class Article implements Serializable {
private static final long serialVersionUID = -1656772748378393449L;
@Id
@Field(type = FieldType.Integer)
private Integer id;
@Field(type = FieldType.Text, searchAnalyzer = "ik_smart", analyzer = "ik_smart", store = true)
private String title;
@Field(type = FieldType.Text, searchAnalyzer = "ik_smart", analyzer = "ik_smart")
private String name;
@Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss")
private Date createDateTime;
}
创建 Repository 接口
public interface ArticleRepository extends ElasticsearchRepository<Article,Integer> {
}
application
spring.data.elasticsearch.repositories.enabled=true
spring.elasticsearch.jest.read-timeout=3s
spring.elasticsearch.rest.uris=http://192.168.1.100:9200
spring.elasticsearch.jest.connection-timeout=5s
spring.elasticsearch.jest.multi-threaded=true
测试使用
@SpringBootTest
class EsDemoApplicationTests {
@Autowired
ArticleRepository articleRepository;
//保存
@Test
void saveTest() {
Article article = new Article();
article.setId(1);
article.setTitle("Elasticsearch入门");
article.setName("felix");
article.setCreateDateTime(new Date());
Article save = articleRepository.save(article);
System.out.println(save);
}
@Test
public void updateTest() {
//更新操作
Article article = new Article();
//如果id不存在则新增,存在则执行更新操作
article.setId(1);
article.setTitle("Elasticsearch入门(二)");
article.setName("felix");
article.setCreateDateTime(new Date());
articleRepository.save(article);
}
@Test
public void deleteTest() {
//根据实体删除
Article article = new Article();
article.setId(1);
articleRepository.delete(article);
//根据id删除
articleRepository.deleteById(0);
//删除全部
articleRepository.deleteAll();
List<Article> articles = new ArrayList<>();
articles.add(article);
articleRepository.deleteAll(articles);
}
@Test
public void findTest() {
//统计
long count = articleRepository.count();
System.out.println(count);
//查询所有
Iterable<Article> articles = articleRepository.findAll();
for (Article article : articles) {
System.out.println(article);
}
//根据id查询
Optional<Article> byId = articleRepository.findById(2);
System.out.println("byID" + byId);
}
@Test
public void sortTest() {
//查询并排序
Sort sort = Sort.by(Sort.Order.asc("id"));
Iterable<Article> articles = articleRepository.findAll(sort);
for (Article article : articles) {
System.out.println(article);
}
}
@Test
public void pageTest() {
//分页查询
Pageable pageable = PageRequest.of(0, 5);
Page<Article> articles = articleRepository.findAll(pageable);
System.out.println(articles.getContent());
}
@Test
public void pageAndSortTest(){
//查询分页并且排序
Sort sort=Sort.by(Order.desc("id"));
Pageable pageable=PageRequest.of(0,5,sort);
Page<Article> articles = articleRepository.findAll(pageable);
List<Article> list = articles.getContent();
for (Article article : list) {
System.out.println(article);
}
}
}
Spring Data ElasticSearch 与 ElasticSearch 有对应关系
https://github.com/spring-projects/spring-data-elasticsearch
https://docs.spring.io/spring-data/elasticsearch/docs/3.2.0.RC3/reference/html/#preface.versions
elasticsearch 版本为6.8.4
spring data elasticsearch 3.2.1