maven项目集成elasticSearch7.x版本(windows版)

@es学习入门API
学习此技术需要先熟悉es的基本语法,更佳!!!
官网文档地址:https://www.elastic.co/guide/en/elasticsearch/reference/index.html
1、下载ES软件包

es下载地址

ElasticSearch: https://mirrors.huaweicloud.com/elasticsearch/?C=N&O=D
kibana: https://mirrors.huaweicloud.com/kibana/?C=N&O=D

2、配置es的核心开发依赖

<!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.8.0</version>
        </dependency>
        <!--ES客户端依赖-->
        <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.8.0</version>
        </dependency>
        <!--日志-->
        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.8.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.2</version>
        </dependency>
        <!--JSON依赖-->
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.2</version>
        </dependency>
        <!--单元测试依赖-->
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

3、入门核心API

//创建ES客户端
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        //创建索引
//        GetIndexResponse users = restHighLevelClient.indices().create(new GetIndexRequest("users"), RequestOptions.DEFAULT);
                //查询索引
//        GetIndexResponse users = restHighLevelClient.indices().get(new GetIndexRequest("users"), RequestOptions.DEFAULT);
//        删除索引
//        AcknowledgedResponse users1 = restHighLevelClient.indices().delete(new DeleteIndexRequest("users"), RequestOptions.DEFAULT);
//批量插入数据 bulk
//        BulkResponse response=null;
//        for (int i = 0; i < n; i++) {
//            Users users1 = new Users();
//            users1.setName(UUID.randomUUID().toString().substring(1,4));
//            users1.setAge(new Random().nextInt(100));
//            users1.setSex(Integer.toString(new Random().nextInt(2)));
//            users1.setBalance(new BigDecimal(1124));
//            String jsonUsers = new ObjectMapper().writeValueAsString(users1);
//             response = restHighLevelClient.bulk(new BulkRequest().add(new IndexRequest("users").id(Integer.toString(new Random().nextInt(1000))).source(jsonUsers, XContentType.JSON)), RequestOptions.DEFAULT);
//        }
//批量删除 bulk 
//        BulkResponse response=restHighLevelClient.bulk(new BulkRequest().add(new DeleteRequest("users").id("487")),RequestOptions.DEFAULT);

**注:**:这里的id值可以使用以下查询API的查询的数据的id值进行删除,这里我进行写死为了熟悉API

       //全量查询数据
//        SearchResponse response = restHighLevelClient.search(new SearchRequest().indices("users").source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery())),RequestOptions.DEFAULT);
        //条件查询
//        SearchResponse response = restHighLevelClient.search(new SearchRequest().indices("users").source(new SearchSourceBuilder().query(QueryBuilders.termQuery("name","xxx"))),RequestOptions.DEFAULT);
        //组合查询
//        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//        boolQueryBuilder.should(QueryBuilders.matchQuery("name","xxx"));
//        boolQueryBuilder.must(QueryBuilders.matchQuery("age","89"));
//        boolQueryBuilder.must(QueryBuilders.matchQuery("age","65"));
        //范围查询
//        RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
//        rangeQuery.gt(80);
//        rangeQuery.lte(90);
//        rangeQuery.from();
        //高亮查询 highlight
        TermQueryBuilder termQuery = QueryBuilders.termQuery("name", "xxx"); //分组查询
        SearchSourceBuilder builder = new SearchSourceBuilder();
        //对name字段:进行高亮查询显示
        HighlightBuilder highlightBuilder = new HighlightBuilder().preTags("<font color='red'>").postTags("</font>").field("name");
        builder.highlighter(highlightBuilder);
                //分页规则:(当前页码-1)*每页显示数据条数  分页查询
//        builder.from(2);
//        builder.size(5);
                //排序
//        builder.sort("age", SortOrder.DESC);
                //过滤字段
//        builder.fetchSource("name","balance");
//        builder.fetchSource(new String[]{"name","age"},new String[]{"sex","balance"});
        builder.query(termQuery);
        SearchResponse response = restHighLevelClient.search(new SearchRequest().indices("users").source(builder),RequestOptions.DEFAULT);
        List list = new ArrayList<>();
        System.out.println(response.getHits().getTotalHits());
        for (SearchHit hit : response.getHits()) {
            ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>();
            System.out.println(hit.getSourceAsString());
            map.put("index",  hit.getIndex());
            map.put("fields",  hit.getFields());
            map.put("id",  hit.getId());
            map.put("source",  hit.getSourceAsString());
            map.put("score",  hit.getScore());
            map.put("type",  hit.getType());
            map.put("highlightFields",  hit.getHighlightFields());

            list.add(map);
        }
        System.out.println(list);
        System.out.println(list.size());
        System.out.println(response);
        System.out.println(response.getHits());
        System.out.println(response.getTook());
        //关闭es客户端
        restHighLevelClient.close();
上一篇:项目重构后,如何利用Postman快速对比出新旧接口之间的差异


下一篇:MybatisPlus集成Druid导致filter类名为空