SpringBoot集成操作Elasticsearch

环境集成

        1.导入SpringBoot提整合ES的依赖 spring-boot-starter-data-elasticsearch
<!--SpringBoot-->
<parent>
   <groupId> org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.2.5.RELEASE</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>
        </dependency>
</dependencies>
        2.然后需要在yml中对ES进行配置 , 如果是集群配置增加uri即可
spring:
  elasticsearch:
    rest:
      uris:
        - http://localhost:9200
        3.编写启动类
@SpringBootApplication
public class ESApplication {
    public static void main(String[] args) {
        SpringApplication.run(ESApplication.class, args);
    }
}

创建Document对象

 1.编写Document对象 ,该对象是对存储到ES中的数据的封装,同时文档映射也是通过它来实现
@Data
@Document(indexName = "order", type = "_doc")
public class OrderDoc {

    @Id
    private Long id;

    @Field(type = FieldType.Text, analyzer = "ik_smart", searchAnalyzer = "ik_smart")
    private String title;
    @Field(type = FieldType.Keyword)
    private String brand;
    @Field(type = FieldType.Date)
    private Date createTime;
    @Field(type = FieldType.Integer)
    private Integer state;
    @Field(type = FieldType.Double)
    private BigDecimal price;
}

创建Repository 

@Repository
public interface OrderRepository extends ElasticsearchRepository<OrderDoc,Long> { // 这里的泛型是当前Repository所要管理的实体类,也就是OrderDoc,Long是实体类ID的类型
}

创建索引和映射

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ESApplication.class)
public class ESTest {
    @Autowired
    private ElasticsearchRestTemplate template;

    @Autowired
    private OrderRepository repository;
    @Test
    public void test(){
        // 创建索引库
        boolean index = template.createIndex(OrderDoc.class);
        System.out.println(index);
        // 创建映射关系
        index = template.putMapping(OrderDoc.class);
        System.out.println(index);
    }

    @Test
    public void CRUDTest() {
        //OrderDoc orderDoc = new OrderDoc();
        //for (int i = 1; i < 50; i++) {
        //    orderDoc.setId(Long.valueOf(i));
        //    orderDoc.setBrand(i%2==0?"坤坤":"只因");
        //    orderDoc.setTitle("时尚宠儿"+orderDoc.getBrand()+"唇膏你值得拥有!");
        //    orderDoc.setState(i%3==0?1:0);
        //    orderDoc.setCreateTime(new Date());
        //    orderDoc.setPrice(new BigDecimal(i*2));
        //    repository.save(orderDoc);
        //}
        repository.deleteById(45L);
        Optional<OrderDoc> optional = repository.findById(2L);
        System.out.println(optional);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值