码迷,mamicode.com
首页 > 编程语言 > 详细

springboot整合es客户端操作elasticsearch(三)

时间:2019-12-15 16:41:22      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:修改   wired   evel   hot   put   hash   return   ota   com   

继续上个随笔:

那么我们只需要修改controller中文件就可以完成相关操作

本次主要是对文档得操作:

更新文档:

package com.cxy.elasticsearch.controller;

import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.snapshots.SnapshotShardsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.transform.Source;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class EsindexController {
    @Autowired
    RestHighLevelClient client;

    @Autowired
    RestClient restClient;

    @RequestMapping(value = "/updatedoc",method = RequestMethod.GET)
    public String updatedoc(){
        UpdateRequest updateRequest = new UpdateRequest("chenxuyou3","doc","ekBpCG8BAmmLmqjtgRwU");
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("name","Ealsticseach学习实战");
        updateRequest.doc(stringStringHashMap);
        UpdateResponse update =null;
        try {
            update= client.update(updateRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        RestStatus status = update.status();
        System.err.println(status);

        return "ok" ;
    }
    @RequestMapping(value = "/deletedoc",method = RequestMethod.GET)
    public String deletedoc(){
       String id ="e0CECG8BAmmLmqjt6hwW";
       DeleteRequest deleteRequest = new DeleteRequest("chenxuyou3", "doc", id);
        DeleteResponse delete = null;
        try {
            delete = client.delete(deleteRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        RestStatus status = delete.status();
        System.err.println(status);

        return "ok" ;
    }

}

调用接口:

localhost:8085/updatedoc

成功:

技术图片

 

 

调用删除接口,删除得是数据第一条:

技术图片

 

 接口:

技术图片

 

 可以看到已经删除了。所以对文档得增加,删除,修改,查询已经完成了,下个随便将会写对文档得精确,模糊,分数查询

springboot整合es客户端操作elasticsearch(三)

标签:修改   wired   evel   hot   put   hash   return   ota   com   

原文地址:https://www.cnblogs.com/cxyxiaobao/p/12044422.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!