ElasticSearch入门之HTTP和JavaAPI操作

ElasticSearch

1. 安装

  • 华为镜像下载,本次下载的是windows下的7.8.0版本。
    ElasticSearch入门之HTTP和JavaAPI操作_第1张图片

  • 下载完成之后解压:
    ElasticSearch入门之HTTP和JavaAPI操作_第2张图片

  • 启动:双击bin/elasticsearch.bat文件。
    如图所示:9300为内部集群通信端口,9200为RESful端口。
    ElasticSearch入门之HTTP和JavaAPI操作_第3张图片

  • 访问http://localhost:9200/,出现如下界面,就表示访问成功。
    ElasticSearch入门之HTTP和JavaAPI操作_第4张图片

2. 数据格式

es是面向文档型数据库,一条数据就是一个文档。

  • 与关系型数据库概念的类比:
    需要注意的是在7.6版本之后types的概念已经被删除了。
    ElasticSearch入门之HTTP和JavaAPI操作_第5张图片

3. HTTP操作es

3.1 索引

  • 创建索引:发送put请求到es
    ElasticSearch入门之HTTP和JavaAPI操作_第6张图片
  • 获取索引:发送get请求到es
    ElasticSearch入门之HTTP和JavaAPI操作_第7张图片
  • 获取全部索引:发送get请求到http://localhost:9200/_cat/indices?v
    ElasticSearch入门之HTTP和JavaAPI操作_第8张图片
  • 删除索引:发送delete请求
    ElasticSearch入门之HTTP和JavaAPI操作_第9张图片

3.2 文档操作

  • 创建文档(类比关系性数据库表数据):向es发送post请求http://localhost:9200/new_index/_doc
    这里使用postman了,apipost不行。。
    ElasticSearch入门之HTTP和JavaAPI操作_第10张图片
  • 携带id新建文档:http://localhost:9200/new_index/_doc/1001ElasticSearch入门之HTTP和JavaAPI操作_第11张图片
  • 文档查询:发送get请求即可ElasticSearch入门之HTTP和JavaAPI操作_第12张图片
  • 查询当前索引下所有文档:http://localhost:9200/new_index/_doc/_search
    ElasticSearch入门之HTTP和JavaAPI操作_第13张图片
  • 全量数据修改文档:使用幂等性的put请求,只传要修改的参数。http://localhost:9200/new_index/_doc/1001
    ElasticSearch入门之HTTP和JavaAPI操作_第14张图片
  • 局部更新文档:需要使用post请求;http://localhost:9200/new_index/_update/1001
    ElasticSearch入门之HTTP和JavaAPI操作_第15张图片
  • 删除文档:发送delete请求,http://localhost:9200/new_index/_doc/1001
    ElasticSearch入门之HTTP和JavaAPI操作_第16张图片

3.3 文档高级查询

  • 查询category为分类的全部文档:http://localhost:9200/new_index/_search?q=category:分类
    ElasticSearch入门之HTTP和JavaAPI操作_第17张图片
  • 使用json方式查询与上方相同的内容。
    ElasticSearch入门之HTTP和JavaAPI操作_第18张图片
  • 分页查询:
    ElasticSearch入门之HTTP和JavaAPI操作_第19张图片
  • 过滤响应数据:
    ElasticSearch入门之HTTP和JavaAPI操作_第20张图片
  • 数据排序:
    ElasticSearch入门之HTTP和JavaAPI操作_第21张图片
  • 多条件查询——同时成立
    ElasticSearch入门之HTTP和JavaAPI操作_第22张图片
  • 多条件查询——满足任意一个
    ElasticSearch入门之HTTP和JavaAPI操作_第23张图片
  • 范围查询:
    ElasticSearch入门之HTTP和JavaAPI操作_第24张图片
  • 全文检索:es会将文字拆解成一个个的关键字,然后进行倒排索引的匹配,匹配其中条件的数据全部列出来(类似于模糊查询)。
    ElasticSearch入门之HTTP和JavaAPI操作_第25张图片
  • 完全匹配:
    ElasticSearch入门之HTTP和JavaAPI操作_第26张图片
  • 高亮查询:ElasticSearch入门之HTTP和JavaAPI操作_第27张图片
  • 聚合查询:ElasticSearch入门之HTTP和JavaAPI操作_第28张图片
  • 映射关系:在创建文档时指定。
    ElasticSearch入门之HTTP和JavaAPI操作_第29张图片

4. Java API操作es

4.1 搭建环境

  • 引入依赖:

    <dependencies>
            <dependency>
                <groupId>org.elasticsearchgroupId>
                <artifactId>elasticsearchartifactId>
                <version>7.8.0version>
            dependency>
            <dependency>
                <groupId>org.elasticsearch.clientgroupId>
                <artifactId>elasticsearch-rest-high-level-clientartifactId>
                <version>7.8.0version>
            dependency>
            <dependency>
                <groupId>org.apache.logging.log4jgroupId>
                <artifactId>log4j-apiartifactId>
                <version>2.17.2version>
            dependency>
            <dependency>
                <groupId>org.apache.logging.log4jgroupId>
                <artifactId>log4j-coreartifactId>
                <version>2.13.3version>
            dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.coregroupId>
                <artifactId>jackson-databindartifactId>
                <version>2.13.1version>
            dependency>
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>4.13version>
            dependency>
            <dependency>
            	<groupId>org.projectlombokgroupId>
            	<artifactId>lombokartifactId>
            	<version>1.18.24version>
        	 dependency>
        dependencies>
    
  • 构建es操作客户端

    /**
     * @Description es客户端
     * @date 2022/7/14 9:33
     */
    public class ES_Client {
    
        private static final String HOST_NAME = "localhost";
    
        private static final Integer PORT = 9200;
    
        private static final String SCHEME = "http";
    
        public static void main(String[] args) throws IOException {
            // 创建es客户端
            RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost(HOST_NAME,PORT,SCHEME)));
    
            // 关闭es客户端
            esClient.close();
        }
    

4.2 封装连接工具类

/**
 * @Description
 * @date 2022/7/14 14:42
 */
public class ConnESUtil {
    private static final String HOST_NAME = "localhost";

    private static final Integer PORT = 9200;

    private static final String SCHEME = "http";

    public static RestHighLevelClient getEsClient(){
        return new RestHighLevelClient(RestClient.builder(new HttpHost(HOST_NAME, PORT, SCHEME)));
    }
}

4.3 索引操作

  • 创建索引:

        /**
         * 创建索引
         */
        private static void createIndices() throws IOException {
            CreateIndexRequest user = new CreateIndexRequest("user_2");
            CreateIndexResponse response = esClient.indices().create(user, RequestOptions.DEFAULT);
            System.out.println(response.isAcknowledged());
        }
    
  • 查询索引:

        /**
         * 查询索引信息
         * @throws IOException
         */
        private static void getIndices() throws IOException {
            GetIndexRequest request = new GetIndexRequest("user_2");
            GetIndexResponse response = ES_Client.esClient.indices().get(request, RequestOptions.DEFAULT);
            // 获取当前索引的信息
            System.out.println(response.getAliases());
            System.out.println(response.getMappings());
            System.out.println(response.getSettings() );
        }
    
  • 删除索引:

        /**
         * 删除索引
         * @throws IOException
         */
        private static void delIndices() throws IOException {
            DeleteIndexRequest request = new DeleteIndexRequest("user_2");
            AcknowledgedResponse response = esClient.indices().delete(request, RequestOptions.DEFAULT);
            System.out.println(response.isAcknowledged());
        }
    

4.4 文档基本操作

  • 封装数据模型:

    @Data
    @ToString
    @AllArgsConstructor
    @NoArgsConstructor
    public class User3 {
        private String name;
    
        private String sex;
    
        private Integer age;
    }
    
  • 新增文档:

        /**
         * 添加文档
         * @throws IOException
         */
        private static void addDoc() throws IOException {
            IndexRequest request = new IndexRequest();
            // 指定索引名称 和 当前文档的id
            request.index("user3").id("1001");
            User3 user = new User3("a", "男" ,30);
    
            // 对象转json
            ObjectMapper mapper = new ObjectMapper();
            String jsonUser = mapper.writeValueAsString(user);
    
            // 指定请求传入数据 和 数据类型
            request.source(jsonUser, XContentType.JSON);
    
            // 创建
            IndexResponse res = esClient.index(request, RequestOptions.DEFAULT);
            System.out.println(res.getResult());
        }
    

    添加之后去查询,可以看到新增的数据:
    ElasticSearch入门之HTTP和JavaAPI操作_第30张图片

  • 修改文档:

        /**
         * 修改文档
         * @throws IOException
         */
        private static void updateDoc() throws IOException {
            UpdateRequest request = new UpdateRequest();
            // 指定要修改的索引 和 id
            request.index("user3").id("1001");
    
            // 传入要修改的数据
            request.doc(XContentType.JSON, "sex", "女");
            UpdateResponse res = esClient.update(request, RequestOptions.DEFAULT);
            System.out.println(res.getResult());
        }
    

ElasticSearch入门之HTTP和JavaAPI操作_第31张图片

  • 查询文档:

        /**
         * 查询文档
         * @throws IOException
         */
        private static void getDoc() throws IOException {
            GetRequest request = new GetRequest();
            // 指定索引 和 id
            request.index("user3").id("1001");
            GetResponse res = esClient.get(request, RequestOptions.DEFAULT);
            System.out.println(res.getSourceAsString());
        }
    

在这里插入图片描述

  • 删除文档:

        /**
         * 删除文档
         * @throws IOException
         */
        private static void delDoc() throws IOException {
            DeleteRequest request = new DeleteRequest();
            // 指定索引 和 id
            request.index("user3").id("1001");
            DeleteResponse res = esClient.delete(request, RequestOptions.DEFAULT);
        }
    

在这里插入图片描述

  • 批量新增:

        /**
         * 批量文档新增
         * @throws IOException
         */
        private static void addDocs() throws IOException {
            BulkRequest request = new BulkRequest();
            // 添加多个请求,同时发送
            // 这里的 source 方法,必须要使用 v : k 的形式传入一个或者多个,不能使用对象转json字符串方法。
            request.add(new IndexRequest().index("user3").id("1002").source(XContentType.JSON, "name", "b"));
            request.add(new IndexRequest().index("user3").id("1003").source(XContentType.JSON, "name", "c"));
            request.add(new IndexRequest().index("user3").id("1004").source(XContentType.JSON, "name", "d"));
            BulkResponse res = esClient.bulk(request, RequestOptions.DEFAULT);
    
            // 查看耗时
            System.out.println(res.getTook());
        }
    
  • 批量删除:

        /**
         * 批量删除文档
         * @throws IOException
         */
        private static void delDocs() throws IOException {
            BulkRequest request = new BulkRequest();
            request.add(new DeleteRequest().index("user3").id("1002"));
            request.add(new DeleteRequest().index("user3").id("1003"));
            request.add(new DeleteRequest().index("user3").id("1004"));
            BulkResponse res = esClient.bulk(request, RequestOptions.DEFAULT);
            // 查看耗时
            System.out.println(res.getTook());
        }
    

4.5 文档高级查询

  • 数据准备,以上方批量插入的形式插入多条可供测试的数据到es:

    		BulkRequest request = new BulkRequest();
            // 添加多个请求,同时发送
            // 这里的 source 方法,必须要使用 v : k 的形式传入一个或者多个,不能使用对象转json字符串方法。
            request.add(new IndexRequest().index("user3").id("1001").source(XContentType.JSON, "name", "zhangsan", "age", "30", "sex", "男"));
            request.add(new IndexRequest().index("user3").id("1002").source(XContentType.JSON, "name", "lisi", "age", "30", "sex", "女"));
            request.add(new IndexRequest().index("user3").id("1003").source(XContentType.JSON, "name", "wangwu", "age", "40", "sex", "男"));
            request.add(new IndexRequest().index("user3").id("1004").source(XContentType.JSON, "name", "wangwu1", "age", "40", "sex", "女"));
            request.add(new IndexRequest().index("user3").id("1005").source(XContentType.JSON, "name", "wangwu2", "age", "50", "sex", "男"));
            request.add(new IndexRequest().index("user3").id("1006").source(XContentType.JSON, "name", "wangwu3", "age", "50", "sex", "男"));
            BulkResponse res = esClient.bulk(request, RequestOptions.DEFAULT);
    
            // 查看耗时
            System.out.println(res.getTook());
    
  • 查询索引下全部文档:

        /**
         * 查询指定索引下全部文档信息
         * @throws IOException
         */
        private static void list() throws IOException {
            SearchRequest request = new SearchRequest();
            request.indices("user3");
    
            // 构建查询参数
            request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
            // 执行查询
            SearchResponse res = esClient.search(request, RequestOptions.DEFAULT);
    
            // 返回数据
            SearchHits hits = res.getHits();
            // 查看总条数
            System.out.println(hits.getTotalHits());
    
            // 遍历返回信息
            for (SearchHit hit : hits) {
                System.out.println(hit.getSourceAsString());
            }
        }
    
  • 根据传入参数查询:

        /**
         * 根据查询文档信息
         * @param age
         * @throws IOException
         */
        private static void getByAge(String age) throws IOException {
            SearchRequest request = new SearchRequest();
            request.indices("user3");
    
            // 构建查询参数
            request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age", age)));
    
            // 执行查询
            SearchResponse res = esClient.search(request, RequestOptions.DEFAULT);
    
            // 返回数据
            SearchHits hits = res.getHits();
            // 查看总条数
            System.out.println(hits.getTotalHits());
    
            // 遍历返回信息
            for (SearchHit hit : hits) {
                System.out.println(hit.getSourceAsString());
            }
        }
    
  • 分页查询:

        /**
         * 分页查询
         * @param page
         * @param size
         * @throws IOException
         */
        private static void list(Integer page, Integer size) throws IOException {
            SearchRequest request = new SearchRequest();
            request.indices("user3");
    
            // 构建查询参数
            SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
            query.from(page);
            query.size(size);
            request.source(query);
    
            // 执行查询
            SearchResponse res = esClient.search(request, RequestOptions.DEFAULT);
    
            // 返回数据
            SearchHits hits = res.getHits();
            // 查看总条数
            System.out.println(hits.getTotalHits());
    
            // 遍历返回信息
            for (SearchHit hit : hits) {
                System.out.println(hit.getSourceAsString());
            }
        }
    
  • 排序查出的数据:

        /**
         * 排序查询
         * @param sortedBy
         * @param order
         * @throws IOException
         */
        private static void list(String sortedBy, SortOrder order) throws IOException{
            SearchRequest request = new SearchRequest();
            request.indices("user3");
    
            // 构建查询参数
            SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
            query.sort(sortedBy, order);
            request.source(query);
    
            // 执行查询
            SearchResponse res = esClient.search(request, RequestOptions.DEFAULT);
    
            // 返回数据
            SearchHits hits = res.getHits();
            // 查看总条数
            System.out.println(hits.getTotalHits());
    
            // 遍历返回信息
            for (SearchHit hit : hits) {
                System.out.println(hit.getSourceAsString());
            }
        }
    
  • 过滤字段:

        /**
         * 过滤字段
         * @param excludes 要排除的字段
         * @param includes 包含的字段
         * @throws IOException
         */
        private static void filterCol(String[] excludes, String[] includes) throws IOException{
            SearchRequest request = new SearchRequest();
            request.indices("user3");
    
            // 构建查询参数
            SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
            query.fetchSource(excludes, includes);
            request.source(query);
    
            // 执行查询
            SearchResponse res = esClient.search(request, RequestOptions.DEFAULT);
    
            // 返回数据
            SearchHits hits = res.getHits();
            // 查看总条数
            System.out.println(hits.getTotalHits());
    
            // 遍历返回信息
            for (SearchHit hit : hits) {
                System.out.println(hit.getSourceAsString());
            }
        }
    
  • 多条件查询:

        /**
         * 多字段组合查询
         * @throws IOException
         */
        private static void getByInfo() throws IOException {
            SearchRequest request = new SearchRequest();
            request.indices("user3");
    
            SearchSourceBuilder builder = new SearchSourceBuilder();
            BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
            boolQuery.must(QueryBuilders.matchQuery("age", "30"));
            boolQuery.must(QueryBuilders.matchQuery("sex", "男"));
    
    //        boolQuery.should(QueryBuilders.matchQuery("age", "30"));
    //        boolQuery.should(QueryBuilders.matchQuery("sex", "男"));
    
            // 构建查询参数
            builder.query(boolQuery);
    
            request.source(builder);
    
            // 执行查询
            SearchResponse res = esClient.search(request, RequestOptions.DEFAULT);
    
            // 返回数据
            SearchHits hits = res.getHits();
            // 查看总条数
            System.out.println(hits.getTotalHits());
    
            // 遍历返回信息
            for (SearchHit hit : hits) {
                System.out.println(hit.getSourceAsString());
            }
        }
    
  • 范围查询:

        /**
         * 范围查询
         * @throws IOException
         */
        private static void getByRange() throws IOException{
            SearchRequest request = new SearchRequest();
            request.indices("user3");
    
            SearchSourceBuilder builder = new SearchSourceBuilder();
            RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
            builder.query(rangeQuery);
            rangeQuery.gt("30");
    
            request.source(builder);
    
            // 执行查询
            SearchResponse res = esClient.search(request, RequestOptions.DEFAULT);
    
            // 返回数据
            SearchHits hits = res.getHits();
            // 查看总条数
            System.out.println(hits.getTotalHits());
    
            // 遍历返回信息
            for (SearchHit hit : hits) {
                System.out.println(hit.getSourceAsString());
            }
        }
    
  • 模糊查询:

        /**
         * 模糊查询
         * @throws IOException
         */
        private static void getLike() throws IOException{
            SearchRequest request = new SearchRequest();
            request.indices("user3");
    
            SearchSourceBuilder builder = new SearchSourceBuilder();
            FuzzyQueryBuilder fuzzyQuery =
                    QueryBuilders.fuzzyQuery("name","wangwu")
                            .fuzziness(Fuzziness.ONE); //指定模糊查询精度:相差小于等于几个
            builder.query(fuzzyQuery);
            request.source(builder);
    
            // 执行查询
            SearchResponse res = esClient.search(request, RequestOptions.DEFAULT);
    
            // 返回数据
            SearchHits hits = res.getHits();
            // 查看总条数
            System.out.println(hits.getTotalHits());
    
            // 遍历返回信息
            for (SearchHit hit : hits) {
                System.out.println(hit.getSourceAsString());
            }
        }
    
  • 高亮查询:

        /**
         * 高亮查询
         * @throws IOException
         */
        private static void getHighLight() throws IOException{
            SearchRequest request = new SearchRequest();
            request.indices("user3");
    
            SearchSourceBuilder builder = new SearchSourceBuilder();
            TermQueryBuilder queryBuilder =
                    QueryBuilders
                            .termQuery("name","zhangsan");// 指定高亮字段和对应值
    
            // 高亮builder
            HighlightBuilder highlightBuilder = new HighlightBuilder();
            // 指定前缀和后缀标签
            highlightBuilder.preTags("");
            highlightBuilder.postTags("");
            // 指定高亮字段
            highlightBuilder.field("name");
    
            builder.highlighter(highlightBuilder);
            builder.query(queryBuilder);
    
            request.source(builder);
    
            // 执行查询
            SearchResponse res = esClient.search(request, RequestOptions.DEFAULT);
    
            // 返回数据
            SearchHits hits = res.getHits();
            // 查看总条数
            System.out.println(hits.getTotalHits());
            System.out.println(res);
        }
    

    在这里插入图片描述

  • 聚合查询(获取最大值):数据都在res对象中

        /**
         * 获取最大年龄数据
         * @throws IOException
         */
        private static void getMax() throws IOException{
            SearchRequest request = new SearchRequest();
            request.indices("user3");
    
            SearchSourceBuilder builder = new SearchSourceBuilder();
    
    
            // 指定聚合要求的内容 和 字段
            AggregationBuilder aggregationBuilder = AggregationBuilders.max("maxAge").field("age");
    
            builder.aggregation(aggregationBuilder);
            request.source(builder);
    
            // 执行查询
            SearchResponse res = esClient.search(request, RequestOptions.DEFAULT);
    
            // 返回数据
            SearchHits hits = res.getHits();
            // 查看总条数
            System.out.println(hits.getTotalHits());
            System.out.println(res);
        }
    

    ElasticSearch入门之HTTP和JavaAPI操作_第32张图片

  • 分组查询:

        /**
         * 分组查询
         * @throws IOException
         */
        private static void getGroupByAge() throws IOException{
            SearchRequest request = new SearchRequest();
            request.indices("user3");
    
            SearchSourceBuilder builder = new SearchSourceBuilder();
    
            // 指定聚合要求的内容 和 字段
            AggregationBuilder aggregationBuilder = AggregationBuilders.terms("count").field("age");
    
            builder.aggregation(aggregationBuilder);
            request.source(builder);
    
            // 执行查询
            SearchResponse res = esClient.search(request, RequestOptions.DEFAULT);
    
            // 返回数据
            SearchHits hits = res.getHits();
            // 查看总条数
            System.out.println(hits.getTotalHits());
            System.out.println(res);
        }
    

    在这里插入图片描述

你可能感兴趣的:(#,elasticsearch,2022,elasticsearch,http,大数据)