springboot整合Elasticsearch

一、在项目中导入坐标依赖

org.springframework.boot
spring-boot-starter-data-elasticsearch

二、添加配置
spring:
data:
elasticsearch:
cluster-name: my-application //ElasticSearch的名字
cluster-nodes: 127.0.0.1:9300
三、索引操作
3.1建立实体类
@Data
@AllArgsConstructor
//第一个参数:索引库名字
//第二个参数:类型名称
@Document(indexName = “skus01”, type = “goods”)
public class Item {
public Item() {
}

@Id
private Long id;
//标题需要分词,所以类型为Text,指定ik分词器
@Field(type = FieldType.Text,analyzer = “ik_max_word”)
private String title;
//分类不需要分词,所以指定为keyword类型,代表只索引,不分词
@Field(type = FieldType.Keyword)
private String category;// 分类
@Field(type = FieldType.Keyword)
private String brand; // 品牌
//价格为Double类型
@Field(type = FieldType.Double)
private Double price; // 价格
//图片地址不需要索引,不需要分词
@Field(type = FieldType.Keyword,index = false)
private String images; // 图片地址
}
3.2建立索引
@RunWith(SpringRunner.class)
@SpringBootTest
public class ElasticsearchTest {

@Autowired
private ElasticsearchTemplate elasticsearchTemplate;

@Test
public void createIndex(){
//创建索引
elasticsearchTemplate.createIndex(Item.class);
//建立映射
elasticsearchTemplate.putMapping(Item.class);
}
}

3.3向ElasticSearch中添加数据:
Spring Data 的强大之处,就在于你不用写任何DAO处理,自动根据方法名或类的信息进行CRUD 操作。只要你定义一个接口,然后继承Repository提供的一些子接口,就能具备各种基本的CRUD功 能。
public interface ItemRepositroy extends ElasticsearchRepository<Item,Long> {}

@RunWith(SpringRunner.class)
@SpringBootTest
public class ElasticsearchTest {

//注入刚刚自定义的接口对象
@Autowired
private ItemRepositroy itemRepositroy;

@Test
public void saveIndex(){
Item item3 = new Item(4l,“小米手机11pro1”,“手机”,“小米”,3999.0,“https://img14.360buyimg.com/n1/s450x450_jfs/t1/65786/38/8555/351173/5d674913E0a3b51f6/8d2e3fadc4cfac3c.jpg”);
Item item4 = new Item(5l,“坚果手机51”,“手机”,“锤子”,999.0,“https://img14.360buyimg.com/n1/s450x450_jfs/t1/65786/38/8555/351173/5d674913E0a3b51f6/8d2e3fadc4cfac3c.jpg”);

itemRepositroy.save(item3);
itemRepositroy.save(item4);
}
}
四、查询数据:
方式一:

@Test
public void selectIndex(){
Iterable all = itemRepositroy.findAll();
Iterator iterator = all.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
方式二:
自定义方法:
public interface ItemRepositroy extends ElasticsearchRepository<Item,Long> {

public List findByTitle(String title);
}

@Test
public void selectIndex(){
List list = itemRepositroy.findByTitle(“手机”);
for (Item item : list) {
System.out.println(item);
}
}
方式三:
自定义查询:

@Test
public void queryIndex(){
//创建查询构建器
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
queryBuilder.withQuery(QueryBuilders.matchQuery(“title”,“小米手机”));
Page search = itemRepositroy.search(queryBuilder.build());
for (Item item : search) {
System.out.println(item);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是SpringBoot整合elasticsearch的步骤: 1. 引入elasticsearch和spring-boot-starter-data-elasticsearch的依赖: ``` <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.12.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> <version>2.4.5</version> </dependency> ``` 2. 配置elasticsearch连接信息: ``` spring.data.elasticsearch.cluster-nodes=localhost:9200 ``` 3. 创建实体类: ``` @Document(indexName = "my_index") public class MyEntity { @Id private String id; private String name; // getter and setter } ``` 4. 创建es的Repository: ``` public interface MyRepository extends ElasticsearchRepository<MyEntity, String> { } ``` 5. 在service中使用Repository: ``` @Service public class MyService { @Autowired private MyRepository myRepository; public void save(MyEntity entity) { myRepository.save(entity); } public List<MyEntity> search(String name) { return myRepository.findByName(name); } } ``` 6. 在controller中调用service: ``` @RestController public class MyController { @Autowired private MyService myService; @PostMapping("/save") public void save(@RequestBody MyEntity entity) { myService.save(entity); } @GetMapping("/search") public List<MyEntity> search(@RequestParam String name) { return myService.search(name); } } ``` 这样就可以通过SpringBoot整合elasticsearch实现数据的增删改查了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值