ES学习日记(十一)-------Java操作ES之基本操作

前言

此篇博客还是一些基础操作,没什么可写的,需要的同学直接抄作业进行测试就可以

上一节写了连接和测试新增操作,这一节写java操作ES的基本操作,也就是增删改查,在这里补充一点知识,我们之前用了指定的索引进行指定添加

有一个情况是,如果我们指定了一个不存在的索引时,他会创建一个新的索引,例如我们这个ik1是不存在的,执行添加后也可以添加进去,索引库会新增一个叫ik1的索引,如图

一、查询

注:查询时需要查询存在的索引,不存在会报错,提示not found

java 复制代码
    /**
     * 查询测试
     */
    @Test
    public void testRetrieve() throws IOException {
        GetRequest getRequest = new GetRequest("ik","5");
        GetResponse response = client.get(getRequest,RequestOptions.DEFAULT);
        System.out.println(response);
    }

二、更新

忽略警告

java 复制代码
    /**
     * 更新
     *
     * @throws IOException
     */
    @Test
    public void testUpdate() throws IOException {
        HashMap<String,Object> map = new HashMap<>();
        map.put("name","哈西布鲁根");
        map.put("content","拳皇99");
        UpdateRequest updateRequest = new UpdateRequest("ik","5").doc(map);
        UpdateResponse updateResponse =client.update(updateRequest,RequestOptions.DEFAULT);
        System.out.println(updateRequest);
    }

三、删除

java 复制代码
    /**
     * 删除
     */
    @Test
    public void testDelete() throws IOException {
        DeleteRequest deleteRequest = new DeleteRequest("ik","5");
        DeleteResponse deleteResponse = client.delete(deleteRequest,RequestOptions.DEFAULT);
        System.out.println(deleteRequest);
    }

四、批量增删改

其实就是增加了一个容器BulkRequest,把所有操作放到了这个容器里,然后同意操作

为了查看效果,注释了删除

java 复制代码
    /**
     * 批量增删改
     */
    @Test
    public void testCUD() throws IOException {
        BulkRequest request = new BulkRequest();
        /*
         * 增加IndexRequest
         * XContentType键值对
         */
        request.add(new IndexRequest("ik").id("5").source(XContentType.JSON,"name","罗翔","content","法外狂徒张三"));
        /*
         * 更新UpdateRequest
         */
        request.add(new UpdateRequest("ik","5").doc(XContentType.JSON,"name","罗翔","content","法外狂徒李四"));
        /*
         * 删除DeleteRequest
         */
//        request.add(new DeleteRequest("ik").id("5"));
        BulkResponse bulkResponse = client.bulk(request,RequestOptions.DEFAULT);
        System.out.println(bulkResponse.toString());
    }

五、查询

1.查询全部

查询全部,可变参数可指定多个索引,如果结果超过十条,默认返回十条

java 复制代码
/**
     * 查询
     *
     * @throws IOException
     */
    @Test
    public void testQuery() throws IOException {
        //可以指定多个索引,用逗号隔开
        SearchRequest searchRequest = new SearchRequest("ik","shop");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //查询所有
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = client.search(searchRequest,RequestOptions.DEFAULT);
        System.out.println(response);
    }

查询结果是一个JSON,转了一下格式,看着清晰一点,查出来的内容是不能直接使用的,所以还需要转换格式

取出我们想要的部分,图例说明更清晰一些

有三种方式,根据实际使用情况选择,详见注释

java 复制代码
/**
     * 查询
     *
     * @throws IOException
     */
    @Test
    public void testQuery() throws IOException {
        //可以指定多个索引,用逗号隔开
        SearchRequest searchRequest = new SearchRequest("ik","shop");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //查询所有,如果结果超过10条,默认显示10条
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = client.search(searchRequest,RequestOptions.DEFAULT);
        //调整结构,取出需要的部分
        long value = response.getHits().getTotalHits().value;
        System.out.println(response);
        if(0<value){
            SearchHit[] hits = response.getHits().getHits();
            for(SearchHit hit : hits){
                System.out.println(hit.getIndex());
                System.out.println(hit.getId());
                System.out.println(hit.getScore());
                //三种方式,根据实际使用情况选择
                System.out.println(hit.getSourceAsMap());
                System.out.println(hit.getSourceAsString());
                System.out.println(hit.getSourceRef());
                System.out.println("==========");
            }
        }
    }
2.按照条件查询

按照条件查询可以分为单字段单索引,单字段多索引,多字段单索引,多字段多索引,多个用逗号隔开就行,有点区别,但区别不大,详见代码注释

java 复制代码
/**
     * 根据条件查询
     *
     * @throws IOException
     */
    @Test
    public void testQueryMatch() throws IOException {
        //可以指定多个索引,用逗号隔开
        SearchRequest searchRequest = new SearchRequest("ik","shop");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //单字段匹配-->字段对值
//        searchSourceBuilder.query(QueryBuilders.matchQuery("content","中国"));
        //多字段匹配-->值对多字段
        searchSourceBuilder.query(QueryBuilders.multiMatchQuery("中国","content","name"));
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = client.search(searchRequest,RequestOptions.DEFAULT);
        //调整结构,取出需要的部分
        long value = response.getHits().getTotalHits().value;
        System.out.println(response);
        if(0<value){
            SearchHit[] hits = response.getHits().getHits();
            for(SearchHit hit : hits){
                System.out.println(hit.getIndex());
                System.out.println(hit.getId());
                System.out.println(hit.getScore());
                //三种方式,根据实际使用情况选择
                System.out.println(hit.getSourceAsMap());
                System.out.println(hit.getSourceAsString());
                System.out.println(hit.getSourceRef());
                System.out.println("==========");
            }
        }
    }
3.排序
java 复制代码
  /**
     * 排序
     *
     * @throws IOException
     */
    @Test
    public void testQuerySortHighLevel() throws IOException {
        //可以指定多个索引,用逗号隔开
        SearchRequest searchRequest = new SearchRequest("ik","shop");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //多字段匹配-->值对多字段
        searchSourceBuilder.query(QueryBuilders.multiMatchQuery("中国","content","name"));

        //从第几位开始分几条,分别是索引下标,大小
        //分页查询默认查询倒序
        searchSourceBuilder.from(0).size(5);
//        //正序写法
//        searchSourceBuilder.sort(SortBuilders.scoreSort().order(SortOrder.ASC));
        /*
         * 指定字段排序
         * 注意不是数据库里面的字段,是es里面的字段
         * 默认正序,倒序.order(SortOrder.DESC))
         *
         * 指定字段后,分数排序失效
         */
        searchSourceBuilder.sort(SortBuilders.fieldSort("id"));

        searchRequest.source(searchSourceBuilder);
        SearchResponse response = client.search(searchRequest,RequestOptions.DEFAULT);
        //调整结构,取出需要的部分
        long value = response.getHits().getTotalHits().value;
        System.out.println(response);
        if(0<value){
            SearchHit[] hits = response.getHits().getHits();
            for(SearchHit hit : hits){
                System.out.println(hit.getIndex());
                System.out.println(hit.getId());
                System.out.println(hit.getScore());
                //三种方式,根据实际使用情况选择
                System.out.println(hit.getSourceAsMap());
                System.out.println(hit.getSourceAsString());
                System.out.println(hit.getSourceRef());
                System.out.println("==========");
            }
        }
    }
4.高亮
java 复制代码
/**
     * 排序
     *
     * @throws IOException
     */
    @Test
    public void testQuerySortHighLevel() throws IOException {
        //可以指定多个索引,用逗号隔开
        SearchRequest searchRequest = new SearchRequest("ik","shop");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //多字段匹配-->值对多字段
        searchSourceBuilder.query(QueryBuilders.multiMatchQuery("中国","content","name"));

        //从第几位开始分几条,分别是索引下标,大小
        //分页查询默认查询倒序
        searchSourceBuilder.from(0).size(5);
//        //正序写法
        searchSourceBuilder.sort(SortBuilders.scoreSort().order(SortOrder.ASC));
        /*
         * 指定字段排序
         * 注意不是数据库里面的字段,是es里面的字段
         * 默认正序,倒序.order(SortOrder.DESC))
         *
         * 指定字段后,分数排序失效
         */
//        searchSourceBuilder.sort(SortBuilders.fieldSort("id"));

        //高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder().field("content").preTags("<p style='color:red'>").postTags("</>");
        searchSourceBuilder.highlighter(highlightBuilder);

        searchRequest.source(searchSourceBuilder);
        SearchResponse response = client.search(searchRequest,RequestOptions.DEFAULT);
        //调整结构,取出需要的部分
        long value = response.getHits().getTotalHits().value;
        System.out.println(response);
        if(0<value){
            SearchHit[] hits = response.getHits().getHits();
            for(SearchHit hit : hits){
                System.out.println(hit.getIndex());
                System.out.println(hit.getId());
                System.out.println(hit.getScore());
                //三种方式,根据实际使用情况选择
                System.out.println(hit.getSourceAsMap());
                System.out.println(hit.getSourceAsString());
                System.out.println(hit.getSourceRef());
                //显示高亮.fragments取文本
                System.out.println("hl: "+String.valueOf(hit.getHighlightFields().get("content").fragments()[0]));
                System.out.println("==========");
            }
        }
    }

写在最后

一些基础用法,详见注释,都能用

相关推荐
懒惰的bit2 小时前
基础网络安全知识
学习·web安全·1024程序员节
Natural_yz4 小时前
大数据学习09之Hive基础
大数据·hive·学习
龙中舞王4 小时前
Unity学习笔记(2):场景绘制
笔记·学习·unity
Natural_yz4 小时前
大数据学习10之Hive高级
大数据·hive·学习
love_and_hope5 小时前
Pytorch学习--神经网络--完整的模型训练套路
人工智能·pytorch·python·深度学习·神经网络·学习
CoderJia程序员甲5 小时前
重学SpringBoot3-整合 Elasticsearch 8.x (三)使用Repository
java·大数据·spring boot·elasticsearch
东方巴黎~Sunsiny6 小时前
如何优化Elasticsearch的查询性能?
大数据·elasticsearch·搜索引擎
夜雨星辰4876 小时前
Android Studio 学习——整体框架和概念
android·学习·android studio
奔跑的花短裤6 小时前
少儿编程启蒙学习
学习·青少年编程·机器人·ai编程
VertexGeek6 小时前
Rust学习(一):初识Rust和Rust环境配置
开发语言·学习·rust