1.maven
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.4.2</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
</dependencies>
2.pojo对象
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "ppp",shards = 1,createIndex = false)
public class Item {
@Field(type = FieldType.Long)
@Id
Long id;
@Field(type = FieldType.Text,analyzer = "ik_smart")
String title; //标题
@Field(type = FieldType.Keyword)
String category;// 分类
@Field(type = FieldType.Keyword)
String brand; // 品牌
@Field(type = FieldType.Double)
Double price; // 价格
@Field(type = FieldType.Keyword,index = false)
String images; // 图片地址
}
3.yml文件配置elasticsearch
如何搭建ES请 点击此处
spring:
elasticsearch:
rest:
uris: http://192.168.233.128:9200 # 这里改成你自己的es服务器地址
4.测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class EsTest {
@Autowired
ElasticsearchRestTemplate elasticsearchRestTemplate;
@Autowired
ItemRepository itemRepository;
@Test
public void createIndex() throws IOException {
// 1 获取索引对象
IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(Item.class);
// 2 创建索引
indexOperations.create();
// // 3 获取映射
Document mapping = indexOperations.createMapping(Item.class);
// 4 将映射放入索引
indexOperations.putMapping(mapping);
boolean exists = indexOperations.exists();
System.out.println(exists);
// 5 删除索引
// indexOperations.delete();
}
@Test
public void indexList() {
List<Item> list = new ArrayList<>();
list.add(new Item(1L, "小米手机7", "手机", "小米", 3299.00, "http://image.leyou.com/13123.jpg"));
list.add(new Item(2L, "坚果手机R1", "手机", "锤子", 3699.00, "http://image.leyou.com/13123.jpg"));
list.add(new Item(3L, "华为META10", "手机", "华为", 4499.00, "http://image.leyou.com/13123.jpg"));
list.add(new Item(4L, "小米Mix2S", "手机", "小米", 4299.00, "http://image.leyou.com/13123.jpg"));
list.add(new Item(5L, "荣耀V10", "手机", "华为", 2799.00, "http://image.leyou.com/13123.jpg"));
// 接收对象集合,实现批量新增
itemRepository.saveAll(list);
}
@Test
public void find() {
Iterable<Item> all = itemRepository.findAll();
for (Item item : all) {
System.out.println(item);
}
}
@Test
public void findByPriceBetween() {
Iterable<Item> all = itemRepository.findItemByPriceBetween(2000.00, 4299.00);
for (Item item : all) {
System.out.println(item);
}
}
@Test
public void cud() {
List<Item> list = new ArrayList<>();
list.add(new Item(1L, "小米手机7", "手机", "小米", 3299.00, "http://image.leyou.com/13123.jpg"));
list.add(new Item(2L, "坚果手机R1", "手机", "锤子", 3699.00, "http://image.leyou.com/13123.jpg"));
list.add(new Item(3L, "华为META10", "手机", "华为", 4699.00, "http://image.leyou.com/13123.jpg"));
list.add(new Item(4L, "华为META11", "手机", "华为", 4799.00, "http://image.leyou.com/13123.jpg"));
list.add(new Item(5L, "vivo", "手机", "华为", 4099.00, "http://image.leyou.com/13123.jpg"));
list.add(new Item(6L, "OPPO", "手机", "华为", 4499.00, "http://image.leyou.com/13123.jpg"));
list.add(new Item(7L, "中兴META14", "手机", "华为", 4599.00, "http://image.leyou.com/13123.jpg"));
list.add(new Item(8L, "华为META15", "手机", "华为", 4199.00, "http://image.leyou.com/13123.jpg"));
list.add(new Item(9L, "华为META16", "手机", "华为", 4299.00, "http://image.leyou.com/13123.jpg"));
list.add(new Item(10L, "华为META17", "手机", "华为", 4399.00, "http://image.leyou.com/13123.jpg"));
elasticsearchRestTemplate.save(list);
// 根据id删除
// elasticsearchRestTemplate.delete("3", IndexCoordinates.of("phone"));
// 根据查询结果删除
// NativeSearchQuery nativeSearchQueryBuilder = new NativeSearchQueryBuilder()
// .withQuery(QueryBuilders.matchQuery("title","小米"))
// .build();
// elasticsearchRestTemplate.delete(nativeSearchQueryBuilder,Item.class,IndexCoordinates.of("phone"));
}
@Test
public void search() {
NativeSearchQuery nativeSearchQueryBuilder = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.matchQuery("title", "小米"))
.build();
SearchHits<Item> search = elasticsearchRestTemplate.search(nativeSearchQueryBuilder, Item.class);
search.getSearchHits().forEach(System.out::println);
}
/**
* 分页
*/
@Test
public void page() {
NativeSearchQuery nativeSearchQueryBuilder = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.multiMatchQuery("小米OPPO中兴华为", "title"))
.withPageable(PageRequest.of(0, 3))
.build();
SearchHits<Item> search = elasticsearchRestTemplate.search(nativeSearchQueryBuilder, Item.class);
System.out.println("总条数:"+search.getTotalHits());
search.getSearchHits().forEach(System.out::println);
}
/**
* 排序
*/
@Test
public void order() {
NativeSearchQuery nativeSearchQueryBuilder = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.multiMatchQuery("小米OPPO中兴华为", "title"))
.withPageable(PageRequest.of(0, 3))// 分页
.withSourceFilter(new FetchSourceFilter(new String[]{"id","title","price"},null)) //过滤字段
.withSort(SortBuilders.fieldSort("price").order(SortOrder.ASC))
.build();
SearchHits<Item> search = elasticsearchRestTemplate.search(nativeSearchQueryBuilder, Item.class);
search.getSearchHits().forEach(System.out::println);
}
/**
* 聚合
*/
@Test
public void agg() {
String aggName = "popBrand";
NativeSearchQuery nativeSearchQueryBuilder = new NativeSearchQueryBuilder()
// .withQuery(QueryBuilders.multiMatchQuery("小米OPPO中兴华为", "title"))
// .withPageable(PageRequest.of(0, 3))
// .withSort(SortBuilders.fieldSort("price").order(SortOrder.ASC))
.addAggregation(AggregationBuilders.terms(aggName).field("brand"))
.build();
SearchHits<Item> search = elasticsearchRestTemplate.search(nativeSearchQueryBuilder, Item.class);
search.getSearchHits().forEach(System.out::println);
// 解析聚合
Aggregations aggregations = search.getAggregations();
Terms terms = aggregations.get(aggName);
List<? extends Terms.Bucket> buckets = terms.getBuckets();
buckets.forEach(bucket ->
{
System.out.println(bucket.getKey());
System.out.println(bucket.getDocCount());
});
}
}