3 minutes SpringData integrates ElasticSearch to achieve CRUD super detailed

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

  1. @Document( indexName= xxx) index name of ES
  2. @Id ES's document ID
  3. Field mapping for @Field ES
  4. type = FieldType.Keyword keyword without word segmentation (there is no String in ES, only text (word segmentation) and keyword (word segmentation)
  5. Whether index can be indexed
  6. tokenizer used by analyzer
  7. format format conversion pattern date format
  8. 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

image-20220314200112925

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

idea64_vJZvPkhh48

Naming conventions Spring Data Commons - Documentation

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324123212&siteId=291194637