model
package com.finlay.scaffold.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import java.math.BigDecimal;
import java.util.Date;
/**
* @author: Finlay
* @description:
* @date: 2020-07-31 5:20 下午
*/
@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "finlay",type = "book")
public class Book {
@Id
private String id;
@Field(type = FieldType.Text,analyzer = "ik_max_word")
private String name;
@Field(type = FieldType.Double)
private BigDecimal price;
@Field(type = FieldType.Text,analyzer = "ik_max_word")
private String content;
private String author;
private Date pubDate;
}
repository
package com.finlay.scaffold.repository;
import com.finlay.scaffold.model.Book;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
/**
* @author: Finlay
* @description: CRUD, 泛型说明: 1、操作的实体对象类型 2、实体对象中ID字段的类型
* @date: 2020-07-31 5:34 下午
*/
public interface BookRepository extends ElasticsearchRepository<Book,String> {
}
Test
启动后,创建出空的索引
image.png
添加文档
public class Test1 extends ApplicationTest {
@Autowired
private BookRepository bookRepository;
/***
* @description: id存在则更新,不存在则新增:因此先查询再更新,字段值不能为空,否则es 更新为null
* @return: void
**/
@Test
public void saveOrUpdate() {
Book book = new Book();
book.setAuthor("finlay")
.setContent("这是好东西啊~")
.setId("1")
.setName("ElasticSearch")
.setPrice(BigDecimal.TEN)
.setPubDate(new Date());
Book save = bookRepository.save(book);
}
}
image.png
删除文档
@Test
public void delete() {
bookRepository.deleteById("AXOyjx7bCVm4kyqzZpwh");
}
查询全部
@Test
public void selectAll() {
Iterable<Book> all = bookRepository.findAll();
all.forEach(a->{
System.out.println(a);
});
}
自定义扩展查询
public interface BookRepository extends ElasticsearchRepository<Book,String> {
//No property age found for type Book! Did you mean 'name'?
List<Book> findByNameAndPrice(String name, BigDecimal price);
}
/***
* @description: https://docs.spring.io/spring-data/elasticsearch/docs/3.0.7.RELEASE/reference/html/#reference
* @return: void
**/
@Test
public void findNameAndAge(){
List<Book> byNameAndPrice = bookRepository.findByNameAndPrice("ElasticSearch", BigDecimal.TEN);
byNameAndPrice.forEach(a->{
System.out.println(a);
});
}
分页
@Test
public void page() {
//第1页
int page = 1;
//定义查询方式
MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
//创建查询条件
SearchQuery query = new NativeSearchQuery(matchAllQueryBuilder).setPageable(PageRequest.of(page - 1, 1));
Page<Book> search = bookRepository.search(query);
//处理结果
System.out.println(search.getNumber());
System.out.println(search.getTotalElements());
System.out.println(search.getTotalPages());
//直接遍历出page内的元素
search.forEach(a -> {
System.out.println(a);
});
}
复杂查询示例
/***
* @description: 条件查询,排序,分页
**/
@Test
public void pageAndSort() {
//排序字段
FieldSortBuilder price = new FieldSortBuilder("price").order(SortOrder.DESC);
//构建查询条件
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
.withIndices("alibaba")
.withTypes("book")
.withQuery(QueryBuilders.matchAllQuery())
.withSort(price)
.withPageable(PageRequest.of(0, 2))
.build();
AggregatedPage<Book> books = elasticsearchTemplate.queryForPage(searchQuery, Book.class);
//直接遍历出page内的元素
books.forEach(a -> System.out.println(a));
}
image.png
网友评论