1. Import dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>2.6.4</version>
</dependency>
2. ll
spring:
elasticsearch:
rest:
uris:
- http://xxxxx:9200
3. Create Beans
- @Document( indexName= xxx) index name of ES
- @Id ES's document ID
- Field mapping for @Field ES
- type = FieldType.Keyword keyword without word segmentation (there is no String in ES, only text (word segmentation) and keyword (word segmentation)
- Whether index can be indexed
- tokenizer used by analyzer
- format format conversion pattern date format
- The FieldType.Nested collection property avoids catching business errors
@Document(indexName = "goods" , shards = 3,replicas = 2)
public class Goods {
// 商品Id skuId
@Id
private Long id;
@Field(type = FieldType.Keyword, index = false)
private String defaultImg;
// es 中能分词的字段,这个字段数据类型必须是 text!keyword 不分词!
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String title;
@Field(type = FieldType.Double)
private Double price;
// @Field(type = FieldType.Date) 6.8.1
@Field(type = FieldType.Date,format = DateFormat.custom,pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime; // 新品
@Field(type = FieldType.Long)
private Long tmId;
@Field(type = FieldType.Keyword)
private String tmName;
@Field(type = FieldType.Keyword)
private String tmLogoUrl;
@Field(type = FieldType.Long)
private Long category1Id;
@Field(type = FieldType.Keyword)
private String category1Name;
@Field(type = FieldType.Long)
private Long category2Id;
@Field(type = FieldType.Keyword)
private String category2Name;
@Field(type = FieldType.Long)
private Long category3Id;
@Field(type = FieldType.Keyword)
private String category3Name;
// 商品的热度! 我们将商品被用户点查看的次数越多,则说明热度就越高!
@Field(type = FieldType.Long)
private Long hotScore = 0L;
// 平台属性集合对象
// Nested 支持嵌套查询
@Field(type = FieldType.Nested)
private List<SearchAttr> attrs;
}
4. Create an interface to inherit the CrudRepository interface
Generics 1: javaBean corresponding to ES
Generic 2: Type of document unique ID
@Repository
public interface GoodsDao extends CrudRepository<Goods,Long> {
}
Note that if you want to implement paging, please implement the PagingAndSortingRepository interface
@Repository
public interface GoodsDao extends PagingAndSortingRepository<Goods,Long> {
}
Add the @Repository annotation to the interface
5. Create service injection interface proxy class object
@Service
public class GoodsServiceImpl implements GoodService {
@Autowired
private GoodsDao goodsDao;
@Override
public boolean onSale(Goods goods) {
Goods save = goodsDao.save(goods);
return !StringUtils.isEmpty(save);
}
}
6. Add @EnableElasticsearchRepositories to the main startup class
@EnableElasticsearchRepositories
@SpringCloudApplication
public class EsListApp {
public static void main(String[] args) {
SpringApplication.run(EsListApp.class);
}
}
7. Write the method name
Tips Write the return value type first, so there are hints
Naming conventions Spring Data Commons - Documentation