• 原生java api操作ES数据库


    原生java api操作ES数据库

    该部分也只写了一点点,后面有更简单的做法,就没写完

    有需要再继续写吧...........参考一下就好了

    @SpringBootTest
    class JavaEsApplicationTests {
    
        @Autowired
        private RestHighLevelClient restHighLevelClient;
    
        /**
         * ES索引数据库,提供RestAPI
         * POST 创建
         * PUT 修改
         * DELETE 删除
         * GET 查询
         */
        //创建索引库
        @Test
        void creteIndex() throws IOException {
            //先发送一个创建索引库的请求,test是索引库的名字
            CreateIndexRequest request = new CreateIndexRequest("test");
            //通过核心对象创建索引库
            CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
            //ACK 确认
            System.out.println(response.isAcknowledged());
        }
    
        //判断索引库是否存在
        @Test
        void isExists() throws IOException {
            GetIndexRequest test = new GetIndexRequest("test");
            boolean exists = restHighLevelClient.indices().exists(test, RequestOptions.DEFAULT);
            System.out.println(exists);
        }
    
        //删除索引库
        @Test
        void deleteIndex() throws IOException {
            //发送一个删除索引库的请求,test是索引库的名字
            DeleteIndexRequest request = new DeleteIndexRequest("test");
            //通过核心对象删除索引库
            AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
            System.out.println(delete.isAcknowledged());
        }
    
        /**
         * POST /user/stu/1
         * {
         *     "uid":1,
         *     "uname":"张三",
         *     "upwd":"123456"
         * }
         */
        //针对文档做增删查改操作
        //增加数据
        @Test
        void insertData() throws IOException {
            //先创建一个User对象
            User user = new User(3, "王五", "123456");
            //创建IndexRequest请求的对象,并指定索引库test
            IndexRequest request = new IndexRequest("test");
            //设置类型
            //6.8.7版本的ES,如果不指定文档类型,那么汇报错:org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: type is missing;
            request.type("std");
            //设置id值,如果不指定,则默认给一个UUID
            request.id("2");
            //设置文档数据源,就是保存的数据,数据格式为JSON
            //需要导入fastjson包
            request.source(JSON.toJSONString(user),XContentType.JSON);
            //如果出现:java.net.SocketTimeoutException: 30,000 milliseconds timeout on connection http-outgoing-0 [ACTIVE]
            //如果还是不行就重启ElasticSearch,就解决了
            request.timeout(TimeValue.timeValueSeconds(60));
            //开始插入数据
            IndexResponse index = restHighLevelClient.index(request, RequestOptions.DEFAULT);
            System.out.println(index.status().getStatus()); //查看状态
            System.out.println(index.status().name()); //CREATE
        }
    
        //更新数文档数据
        @Test
        void updateData() throws IOException {
            //指定要修改的索引库,类型和id
            UpdateRequest request = new UpdateRequest("test","std","2");
            //设置数据
            /**
             * 我的ES版本的是6.8.7
             * 原始:uid:3,uname:王五,wpwd:123456
             * (1)只设置user.setUname("赵柳"); ==> 结果:uid:0,uname:赵柳,wpwd:123456
             * (2)只设置user.setUname("赵柳");user.setUpwd("79856"); ==> 结果:uid:0,uname:赵柳,wpwd:79856
             * (3)只设置user.setUpwd("111111"); ==> 结果:uid:0,uname:王五,wpwd:111111
             */
            User user = new User();
            user.setUpwd("111111");
            System.out.println(user);
            //转化为JSON格式的数据
            request.doc(JSON.toJSONString(user),XContentType.JSON);
            //更新数据
            restHighLevelClient.update(request,RequestOptions.DEFAULT);
        }
    
        //删除数据
        @Test
        void deleteData() throws IOException {
            DeleteRequest request = new DeleteRequest("test");
            request.type("std");
            request.id("2");
            DeleteResponse delete = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
            System.out.println(delete.status().name());
        }
    
        //但条件查询+分页
        @Test
        void search() throws IOException {
            String keywords = "";
            SearchRequest searchRequest = new SearchRequest("test");
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            MatchAllQueryBuilder query = QueryBuilders.matchAllQuery();
            searchSourceBuilder.query(query);
            searchSourceBuilder.from(0).size(2);
            searchRequest.source(searchSourceBuilder);
            SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            SearchHit[] hits = search.getHits().getHits();
    //        for (SearchHit )
        }
    
        @Test
        void contextLoads() {
        }
    
    }
  • 相关阅读:
    CMS网站 中最好用的!
    成为优秀设计师的十大条件
    网站变色(黑白)!
    设计师必知的18种服装风格
    HDFS核心类FileSystem的使用
    Hadoop的伪分布式安装和部署的流程
    初学MapReduce离线计算(eclipse实现)
    hdfs的客户端读写流程以及namenode,secondarynamenode,checkpoint原理
    hadoop常用的操作指令
    TableLayoutPanel&SplitContainer 布局
  • 原文地址:https://www.cnblogs.com/IT_CH/p/12813610.html
Copyright © 2020-2023  润新知