实体类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
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;
@Document(indexName = "mineok3", type = "item", shards = 1)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Item {
@Field(type = FieldType.Long)
@Id
Long id;
@Field(type = FieldType.Text)
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;
}
Repository
public interface ItemRepository extends ElasticsearchRepository<Item,Integer>{
}
测试类
@SpringBootTest
class ItemTest {
@Resource
ElasticsearchTemplate elasticsearchTemplate;
@Resource
ItemRepository itemRepository;
}
创建索引库
@Test
public void createIndexs() {
elasticsearchTemplate.createIndex(Item.class);
elasticsearchTemplate.putMapping(Item.class);
}
存储数据
@Test
public void saveTest() {
List<Item> list = new ArrayList<>();
list.add(new Item(2L, "坚果手机R1", " 手机", "锤子", 3699.00, "http://image.taobao.com/123.jpg"));
list.add(new Item(3L, "华为META10", " 手机", "华为", 4499.00, "http://image.taobao.com/3.jpg"));
list.add(new Item(4L, "苹果手机", " 手机", "苹果", 9999.00, "http://image.taobao.com/123.jpg"));
itemRepository.saveAll(list);
}
查询数据
@Test
public void queryTest() {
Iterable<Item> all = itemRepository.findAll();
for (Item item : all) {
System.out.println(item);
}
}
条件查询
Repository
public interface ItemRepository extends ElasticsearchRepository<Item,Integer>{
public List<Item> findItemByPriceBetween(Double start,Double end);
}
@Test
public void queryBy() {
List<Item> itemByPriceBetween = itemRepository.findItemByPriceBetween(1000D, 4000D);
for (Item item : itemByPriceBetween) {
System.out.println(item);
}
}
原生查询
@Test
public void queryProperty() {
MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("title", "手机");
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder();
searchQueryBuilder.withSourceFilter(new FetchSourceFilter(new String[]{"title", "price"}, null));
searchQueryBuilder.withSort(SortBuilders.fieldSort("price").order(SortOrder.DESC));
searchQueryBuilder.withPageable(PageRequest.of(0, 2));
searchQueryBuilder.withQuery(queryBuilder);
Iterable<Item> items = itemRepository.search(searchQueryBuilder.build());
for (Item item : items) {
System.out.println(item);
}
System.out.println("总条数:" + ((Page<Item>) items).getTotalElements());
System.out.println("总页数:" + ((Page<Item>) items).getTotalPages());
}
聚合查询
@Test
public void queryProperty2() {
MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("title", "手机");
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder();
searchQueryBuilder.addAggregation(AggregationBuilders.terms("popularBrand").field("brand"));
AggregatedPage<Item> items = elasticsearchTemplate.queryForPage(searchQueryBuilder.build(), Item.class);
Aggregations aggregations = items.getAggregations();
StringTerms popularBrand = aggregations.get("popularBrand");
List<StringTerms.Bucket> buckets = popularBrand.getBuckets();
for (StringTerms.Bucket bucket : buckets) {
System.out.println("品牌分组:" + bucket.getKeyAsString());
System.out.println("该品牌分组下的商品数量:" + bucket.getDocCount());
}
}
GitHub:SpringBoot相关Demo