ElasticSearch 7.6.x 版本 2020最新版 JavaRest api

点个关注吧,球球啦!
持续更新中……

前言

      周末闲来无事,到官网学习(fanyi)了下最新版本的ESJavaRest API。
ES版本:7.6.2
Springboot版本:2.2.6

准备工作及注意事项

1. Maven导包

      Springboot 2.2.6默认帮我们导入的是ES 6.8.7的包,所以我们需要更改下版本

<elasticsearch.version>7.6.1</elasticsearch.version>

2. 初始化Client

@Configuration
public class ElasticSearchConfig {

    @Bean
    public RestHighLevelClient client(){
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost",9200,"http")
        ));
        return client;
    }
}

索引(index)

增加索引

    //创建索引
    @Test
    void createIndex() throws IOException {
        CreateIndexRequest request = new CreateIndexRequest("index1");
        CreateIndexResponse createIndexResponse =
                client.indices().create(request, RequestOptions.DEFAULT);
    }

删除索引

    //删除索引
    @Test
    void deleteIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest("index1");
        AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
        boolean isSuccessful = delete.isAcknowledged();

        System.out.println(isSuccessful);
    }

查询索引是否存在

    //返回索引是否存在
    @Test
    void existIndex() throws IOException {
        GetIndexRequest request = new GetIndexRequest("index1");
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);

        System.out.println(exists);
    }

文档(document)

添加文档

    //添加文档
    @Test
    void addDocument() throws IOException {
        User user = new User("dai", 22);
        IndexRequest request = new IndexRequest("index1")
                .id("1")
                .timeout(TimeValue.timeValueSeconds(1));

        IndexRequest source = request.source(JSON.toJSONString(user), XContentType.JSON);

        IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);

        System.out.println(indexResponse.toString());
        System.out.println(indexResponse.status());
    }

删除文档

    //删除文档
    @Test
    void deleteDocument() throws IOException {
        DeleteRequest request = new DeleteRequest("index1", "2");
        request.timeout("1s");
        DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);

        System.out.println(deleteResponse.status());
    }

查询文档是否存在

    //判断文档是否存在
    @Test
    void isExistDocument() throws IOException {
        GetRequest request = new GetRequest("index1", "1");
        //不获取_source内容,提升效率
        request.fetchSourceContext(new FetchSourceContext(false));
        request.storedFields("_none_");

        boolean exists = client.exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

更新文档

    //更新文档
    @Test
    void updateDocument() throws IOException {
        UpdateRequest request = new UpdateRequest("index1", "1");
        request.timeout("1s");

        User user = new User("dai2", 22);
        request.doc(JSON.toJSONString(user), XContentType.JSON);
        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);

        System.out.println(updateResponse.status());
    }

数据(datas)

批量插入数据

    //批量插入数据
    @Test
    void BulkRequest() throws IOException {

        BulkRequest bulkRequest = new BulkRequest()
                .timeout("5s");
        List<User> users = Arrays.asList(new User("dai1", 1), new User("dai2", 2), new User("dai3", 3));

        for (User user : users) {
            bulkRequest.add(new IndexRequest("index1")
                    //.id("xxx")
                    .source(JSON.toJSONString(user), XContentType.JSON));
        }

        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        //是否失败,false表示成功
        System.out.println(bulkResponse.hasFailures());
        System.out.println(bulkResponse.status());
    }

查询

    //查询
    @Test
    void search() throws IOException {
        SearchRequest request = new SearchRequest("index1");

        //构建搜索条件
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        // SearchRequest 搜索请求
        // SearchSourceBuilder 条件构造
        // HighlightBuilder 构建高亮
        // TermQueryBuilder 精确查询
        // MatchAllQueryBuilder .....
        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
        sourceBuilder.query(matchAllQueryBuilder)
                .timeout(new TimeValue(60, TimeUnit.SECONDS));

        request.source(sourceBuilder);

        SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
        System.out.println(JSON.toJSONString(searchResponse.getHits(), true));
        System.out.println("===================================");
        for (SearchHit documentFields : searchResponse.getHits()) {
            System.out.println(documentFields.getSourceAsMap());
        }

    }

点个赞再走,球球啦!

原创不易,白嫖不好,各位的支持和认可,就是我创作的最大动力,我们下篇文章见!

本博客仅发布于CSDN—一个帅到不能再帅的人 Mr_kidBK。转载请标明出处。
https://blog.csdn.net/Mr_kidBK

点赞!收藏!转发!!!么么哒!
点赞!收藏!转发!!!么么哒!
点赞!收藏!转发!!!么么哒!
点赞!收藏!转发!!!么么哒!
点赞!收藏!转发!!!么么哒!
————————————————
在这里插入图片描述

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值