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


    继续上个随笔:

    那么我们只需要修改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

    成功:

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

     接口:

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

  • 相关阅读:
    创建windows服务
    Scrum演练(2)
    数据结构链表创建与输出
    C++ XML解析之tinyXML
    解决VS2010启动速度死慢的问题
    标准C++中string类的用法总结
    模版参数编译时检查方法,利用typedef
    static的作用
    数据结构实现链表的反转
    字节对齐
  • 原文地址:https://www.cnblogs.com/cxyxiaobao/p/12044422.html
Copyright © 2020-2023  润新知