SpringBoot下使用ElasticSaerch教程(二)

完成的是针对索引Index的增删改查.环境和前文是一样的.这里直接就开始了,前文教程SpringBoot下使用ElasticSearch教程(一).

一:SpringBoot对索引使用有三种方式,json格式(推荐),Map,內建工具(测试使用)的方式.这里使用內建工具吧.

    1. 查询索引数据.

     使用GetResponse来实现.(需求是查询id=3的数据.)

import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;

@RequestMapping("/index")
@RestController
public class IndexController {
    @Autowired
    private TransportClient client;

    @RequestMapping("/get")
    public String get(){
        GetResponse response=client.prepareGet("book","novel","3").get();
        System.out.println(response.getSourceAsString());
        System.out.println("索引名称:"+response.getIndex());
        System.out.println("类型:"+response.getType());
        System.out.println("文档ID:"+response.getId());
        return  "Get Index Success";
    }

  测试结果:

  

 

2. 创建索引数据.(新增id=14的数据)

    @RequestMapping("/create")
    public String create() throws IOException {
        IndexResponse response=client.prepareIndex("book","novel","14").setSource(XContentFactory.jsonBuilder().startObject().field("title","Java面向对象编程").field("author","Jack").field("word_count",700).field("publish_data","2012-10-15").endObject()).get();
        System.out.println("索引名称:"+response.getIndex());
        System.out.println("类型:"+response.getType());
        System.out.println("文档ID:"+response.getId());
        System.out.println("当前实例状态:"+response.status());
        return "Create Index Success";
    }

测试结果:

3. 更新索引数据.(更新id为1的书籍的title)

    @RequestMapping("/update")
    public String update() throws IOException {
        UpdateResponse response=client.prepareUpdate("book","novel","1").setDoc(XContentFactory.jsonBuilder().startObject().field("title","JavaWeb从入门到精通").endObject()).get();
        System.out.println("索引名称:"+response.getIndex());
        System.out.println("类型:"+response.getType());
        System.out.println("文档ID:"+response.getId());
        System.out.println("当前实例状态:"+response.status());
        return "Update Index Success";
    }

测试结果:

4. 删除索引数据.(删除索引为14的数据).

    @RequestMapping("/delete")
    public String delete(){
        DeleteResponse response=client.prepareDelete("book", "novel", "14").get();
        System.out.println("索引名称:"+response.getIndex());
        System.out.println("类型:"+response.getType());
        System.out.println("文档ID:"+response.getId());
        System.out.println("当前实例状态:"+response.status());
        return "Delete Index Success";
    }

测试结果:

5. 注意事项.

注意修改数据的时候的那个jsonBuilder().

 导包: import org.elasticsearch.common.xcontent.XContentFactory;

XContentFactory.jsonBuilder().startObject().field("title","JavaWeb从入门到精通").endObject()).get()

 

 

至此完成了基于SpringBoot的简单的增删改查.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是 SpringBoot 使用 Caffeine 的教程: 1. 引入依赖 在 pom.xml 文件中引入 Caffeine 依赖: ```xml <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> <version>2.9.0</version> </dependency> ``` 2. 配置 Caffeine 缓存 在 SpringBoot 的配置文件中添加以下配置: ```yaml spring.cache.type=caffeine spring.cache.caffeine.spec=maximumSize=100,expireAfterAccess=5m ``` 这里使用了 maximumSize 和 expireAfterAccess 两个参数,maximumSize 表示缓存的最大容量为 100,expireAfterAccess 表示对象的访问时间超过 5 分钟后会被删除。 3. 使用缓存 在 Java 代码中使用缓存,需要使用 SpringBoot 提供的 @Cacheable、@CachePut、@CacheEvict 注解。 例如,以下代码演示了如何使用 @Cacheable 注解: ```java @Service public class UserService { @Cacheable(value = "userCache", key = "#id") public User getUserById(Long id) { // 从数据库中获取用户信息 User user = userDao.getUserById(id); return user; } } ``` 这里定义了一个缓存名为 userCache,并且根据 id 作为缓存的 key 值。 4. 测试缓存 在测试类中进行测试: ```java @SpringBootTest class UserServiceTest { @Autowired private UserService userService; @Test void testCache() { // 第一次查询,从数据库中获取用户信息 User user1 = userService.getUserById(1L); System.out.println(user1); // 第二次查询,从缓存中获取用户信息 User user2 = userService.getUserById(1L); System.out.println(user2); } } ``` 这里定义了一个测试用例,第一次查询会从数据库中获取用户信息,第二次查询会从缓存中获取用户信息。 以上就是 SpringBoot 使用 Caffeine 缓存的教程,希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大道之简

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值