写点什么

SpringBoot 整合 Elasticsearch

  • 2021 年 11 月 11 日
  • 本文字数:1628 字

    阅读完需:约 5 分钟

============================================================================


添加依赖


<dependency>


<groupId>org.projectlombok</groupId>


<artifactId>lombok</artifactId>


</dependency>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-data-elasticsearch</artifactId>


</dependency>


配置


#集群名称


spring.data.elasticsearch.cluster-name=elasticsearch


#es 的地址和端口


spring.data.elasticsearch.cluster-nodes=192.168.7.188:9300


实体类


@Data


@AllArgsConstructor


@NoArgsConstructor


//配置 indexName 索引名称,type 是类型名称,shards 是分片数,replicas 是副本数


@Document(indexName = "goods",type = "_doc",shards = 1,replicas = 0)


public class Goods {


@Id


private Long id;


//配置字段,type 是类型,analyzer 是分词器,index 是否索引


@Field(type = FieldType.Text,analyzer = "ik_max_word")


private String name;


@Field(type = FieldType.Keyword)


private String brand;


@Field(type = FieldType.Keyword)


private String categroy;


@Field(type = FieldType.Double)


private Double price;


}


使用 ElasticsearchTemplate 创建索引,添加映射


@RunWith(SpringJUnit4ClassRunner.class)


@SpringBootTest(classes = SearchServiceApplication.class)


public class ElasticsearchTest {


@Autowired


private ElasticsearchTemplate template;


@Test


public void createIndex(){


//删除索引


template.deleteIndex(Goods.class);


//创建索引


template


【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


.createIndex(Goods.class);


//添加映射,将字段映射到类型上


template.putMapping(Goods.class);


}


}


检查结果



操作索引


==================================================================


使用 ElasticsearchRepository 接口完成索引操作


/**


  • goods 索引操作接口


*/


public interface GoodsRepository extends ElasticsearchRepository<Goods,Long> {


}


测试添加数据


@Autowired


private GoodsRepository goodsRepository;


@Test


public void testSave(){


Goods goods = new Goods(1L, "华为 P30 手机", "Huawei", "手机", 4444.0);


//单条保存,id 存在就更新,不存在就添加


goodsRepository.save(goods);


List<Goods> list = Arrays.asList(


new Goods(2L, "华为 P40 手机", "Huawei", "手机", 6666.0),


new Goods(3L, "小米 11 手机", "MI", "手机", 6666.0),


new Goods(4L, "小米 12 手机", "MI", "手机", 1666.0),


new Goods(5L, "小米 13 手机", "MI", "手机", 2666.0),


new Goods(6L, "OPPOX1 手机", "oppo", "手机", 3666.0),


new Goods(7L, "OPPOX2 手机", "oppo", "手机", 4666.0),


new Goods(8L, "OPPOX3 手机", "oppo", "手机", 2666.0),


new Goods(9L, "VIVOv1 手机", "vivo", "手机", 3666.0),


new Goods(10L, "VIVOv2 手机", "vivo", "手机", 5666.0)


);


//多条保存


goodsRepository.saveAll(list);


}


查询数据


==================================================================


ElasticsearchRepository 提供了非常强大的查询生成功能,按约定的方式在接口中定义查询方法,它来实现查询。


如:


/**


  • goods 索引操作接口


*/


public interface GoodsRepository extends ElasticsearchRepository<Goods,Long> {


//通过名称模糊查询


List<Goods> findByNameLike(String name);


//查询两个价格之间的商品


List<Goods> findByPriceBetween(Double p1,Double p2);


}


可以使用的关键字:


| 关键字 | 示例 |


| --- | --- |


| And | findByNameAndPrice |


| Or | findByNameOrPrice |


| Is | findByName |


| Not | findByNameNot |


| Between | findByPriceBetween |


| LessThan | findByPriceLessThan |


| LessThanEqual | findByPriceLessThanEqual |


| GreaterThan | findByPriceGreaterThan |


| GreaterThanEqual | findByPriceGreaterThan |


| Before | findByPriceBefore |

评论

发布
暂无评论
SpringBoot整合Elasticsearch