【SpringBoot】+【Elasticsearch--------->文档操作】

一 ,在Kibana中文档的增删改查


#删除文档
DELETE /hotel/_doc/2

#修改文档(局部修改)
POST /heima/_update/1
{
  "doc": {
    "birth":"2001-09-07",
    "imageUrl":"eeeeeeeeeeee"
  }
}

#修改文档(全局修改)
PUT /hotel/_doc/1
{
  "birth":"2002-12-12",
  "brand":"小米",
  "email":"小米@123.com",
  "imageUrl":"ffffffffffffff",
  "info":"小米举世无双",
  "name":{
    "firstName":"军",
    "lastName":"雷"
  }
}

#查看文档
GET /hotel/_doc/36934

#新增文档
POST /hotel/_doc/2
{
  "birth":"2002-12-12",
  "brand":"华为",
  "email":"huawei@123.com",
  "imageUrl":"ffffffffffffff",
  "info":"华为天下无敌",
  "name":{
    "firstName":"正飞",
    "lastName":"任"
  }
}

#查询总记录数
GET /hotel/_count

#查询全部文档数据
GET /hotel/_search
{
  "query":{
    "match_all": {}
  }
}

二、在Java中文档操作

2.1 批量操作:

/**
     * 批量操作文档
     * @throws Exception
     */
    @Test
    public void bulTest() throws Exception{
        //批量操作
        BulkRequest bulkRequest = new BulkRequest();
        //通过查询得到数据库所有数据
        List<Hotel> hotelList = hotelService.list();
        //得到文档数据
        List<HotelDoc> hotelDocList = hotelList.stream().map(hotel -> {
            HotelDoc hotelDoc = new HotelDoc(hotel);
            return hotelDoc;
        }).collect(Collectors.toList());
        //遍历集合添加到索引之中
        for (HotelDoc hotelDoc : hotelDocList) {
            //增加文档操作,可以增删改
            bulkRequest.add(
                    new IndexRequest(ESConstant.HOTEL_INDEX)
                            .id(hotelDoc.getId().toString())
                            .source(JSON.toJSONString(hotelDoc),XContentType.JSON)
            );
        }
        client.bulk(bulkRequest,RequestOptions.DEFAULT);
    }

2.2 新增文档

在这里插入图片描述
代码示例:

/**
     * 增加文档 ---->单条
     * @throws Exception
     */
    @Test
    public void addDocTest() throws Exception{
        IndexRequest indexRequest= new IndexRequest(ESConstant.HOTEL_INDEX);
        //查询数据库得到hotel一条数据
        Hotel hotel = hotelService.getById(36934L);
        //增加文档数据需要文档实体
        HotelDoc hotelDoc = new HotelDoc(hotel);
        //增加文档时需要id
        indexRequest.id(hotelDoc.getId().toString());
        //增加文档数据(变成json格式的数据)-->json串
        String json = JSON.toJSONString(hotelDoc);
        indexRequest.source(json, XContentType.JSON);
        IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println("response.status() = " + response.status());
    }

2.3 查询文档

在这里插入图片描述
代码示例:

/**
     * 根据id查看文档数据  --------需要文档id
     * @throws Exception
     */
    @Test
    public void getDocTest() throws Exception{
        GetRequest getRequest = new GetRequest(ESConstant.HOTEL_INDEX,"36934");
        //id必须得要
        //getRequest.id("36934");
        GetResponse documentFields = client.get(getRequest, RequestOptions.DEFAULT);
        //得到查询到的文档数据转成json串
        String hotelDocJson = documentFields.getSourceAsString();
        HotelDoc hotelDoc = JSON.parseObject(hotelDocJson, HotelDoc.class);
        System.out.println("hotelDoc = " + hotelDoc);
    }

2.4 修改文档

在这里插入图片描述
代码示例:

/**
     * 修改文档----->需要文档id
     * 全量修改,局部修改
     * @throws Exception
     */
    @Test
    public void updateDocTest() throws Exception{
        UpdateRequest updateRequest = new UpdateRequest(ESConstant.HOTEL_INDEX,"36934");
        //1.全量修改方法一
        //从数据库中根据id查询到数据
        Hotel hotel = hotelService.getById(36934);
        //将数据赋值给文档实体
        HotelDoc hotelDoc = new HotelDoc(hotel);
        hotelDoc.setAddress("6666");
        String hotelDocJson = JSON.toJSONString(hotelDoc);
        //修改需要传入的是JSON格式的数据
        updateRequest.doc(hotelDocJson,XContentType.JSON);
        UpdateResponse response = client.update(updateRequest, RequestOptions.DEFAULT);
        //2.修改方法二:
        updateRequest.doc(
                "address","更新了方法二",
                "city","杭州黑马更新了方法二"
        );
        //3.修改方法三:
        Map<String, Object> map = new HashMap<>();
        map.put( "address","更新了方法三");
        map.put( "city","杭州黑马更新了方法三");
    }

2.5 删除文档

代码示例:

   /**
     * 通过id删除文档
     * @throws Exception
     */
    @Test
    public void deleteDocTest() throws Exception{
        DeleteRequest deleteRequest = new DeleteRequest(ESConstant.HOTEL_INDEX,"36934");
        DeleteResponse response = client.delete(deleteRequest, RequestOptions.DEFAULT);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值