Elasticsearch——使用Java API实现ES中的索引、映射、文档操作(下)

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: Elasticsearch——使用Java API实现ES中的索引、映射、文档操作(下)

文章目录:


2.10 批量删除文档

2.11 全量查询

2.12 条件查询

2.13 分页查询

2.14 排序查询

2.15 过滤字段查询

2.16 组合条件查询

2.17 范围查询

2.18 模糊查询

2.19 聚合查询

2.20 分组查询


2.10 批量删除文档


package com.szh.es;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import java.io.IOException;
/**
 *
 */
public class ESTestDocDeleteBatch {
    public static void main(String[] args) throws IOException {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        //批量删除文档 --- 请求对象
        BulkRequest request = new BulkRequest();
        //将要删除的文档id存入请求体中
        request.add(new DeleteRequest().index("user").id("1001"));
        request.add(new DeleteRequest().index("user").id("1002"));
        request.add(new DeleteRequest().index("user").id("1003"));
        //发送请求 --- 获取响应
        BulkResponse response = esClient.bulk(request,RequestOptions.DEFAULT);
        System.out.println(response.getTook());
        System.out.println(response.getItems());
        //关闭ES客户端
        esClient.close();
    }
}


2.11 全量查询

因为上面两个代码案例分别进行了批量创建、批量删除。所以这里首先执行一次批量创建的代码,确保索引中有多条数据供我们查询。

package com.szh.es;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
/**
 *
 */
public class ESTestDocInsertBatch {
    public static void main(String[] args) throws IOException {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        //批量新增文档 --- 请求对象
        BulkRequest request = new BulkRequest();
        //以JSON格式批量新增文档 --- 存入请求体中
        request.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON, "name", "张起灵","sex","boy","age",21));
        request.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON, "name", "小哥","sex","boy","age",18));
        request.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON, "name", "小宋","sex","boy","age",20));
        request.add(new IndexRequest().index("user").id("1004").source(XContentType.JSON, "name", "冷少","sex","boy","age",25));
        request.add(new IndexRequest().index("user").id("1005").source(XContentType.JSON, "name", "Java软件工程师","sex","girl","age",40));
        //发送请求 --- 获取响应
        BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
        System.out.println(response.getTook());
        //关闭ES客户端
        esClient.close();
    }
}



下面首先进行全量查询操作。

package com.szh.es;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import java.io.IOException;
/**
 *
 */
public class ESTestDocQuery {
    public static void main(String[] args) throws IOException {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        //1.查询索引中的全部文档 --- matchAllQuery 全量查询
        //创建搜索请求对象
        SearchRequest request = new SearchRequest();
        //设置参数 --- 表示查询哪个索引中的文档内容
        request.indices("user");
        //构建查询的请求体 --- 存入搜索请求对象中
        request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
        //发送请求 --- 获取响应
        SearchResponse response = esClient.search(request,RequestOptions.DEFAULT);
        //获取查询到的结果集
        SearchHits hits = response.getHits();
        System.out.println(hits.getTotalHits()); //结果集的条数
        System.out.println(response.getTook());  //总耗时
        //遍历结果集
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
        //关闭ES客户端
        esClient.close();
    }
}


2.12 条件查询

做匹配查询,查询年龄age=21的文档内容。

package com.szh.es;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import java.io.IOException;
/**
 *
 */
public class ESTestDocQuery {
    public static void main(String[] args) throws IOException {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        //2.条件查询--- termQuery age=21
        //创建搜索请求对象
        SearchRequest request = new SearchRequest();
        //设置参数 --- 表示查询哪个索引中的文档内容
        request.indices("user");
        //构建查询的请求体 --- 存入搜索请求对象中
        request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age",21)));
        //发送请求 --- 获取响应
        SearchResponse response = esClient.search(request,RequestOptions.DEFAULT);
        //获取查询到的结果集
        SearchHits hits = response.getHits();
        System.out.println(hits.getTotalHits()); //结果集的条数
        System.out.println(response.getTook());  //总耗时
        //遍历结果集
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
        //关闭ES客户端
        esClient.close();
    }
}


2.13 分页查询

做全量查询,对查询结果进行分页显示,每页2条数据,查询第1页。

查看第几页:(页码 - 1*每页条数

package com.szh.es;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import java.io.IOException;
/**
 *
 */
public class ESTestDocQuery {
    public static void main(String[] args) throws IOException {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        //3.分页查询
        //创建搜索请求对象
        SearchRequest request = new SearchRequest();
        //设置参数 --- 表示查询哪个索引中的文档内容
        request.indices("user");
        //构建查询的请求体
        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        builder.from(0);
        builder.size(2);
        //将构建好的查询请求体存入搜索请求对象中
        request.source(builder);
        //发送请求 --- 获取响应
        SearchResponse response = esClient.search(request,RequestOptions.DEFAULT);
        //获取查询到的结果集
        SearchHits hits = response.getHits();
        System.out.println(hits.getTotalHits()); //结果集的条数
        System.out.println(response.getTook());  //总耗时
        //遍历结果集
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
        //关闭ES客户端
        esClient.close();
    }
}


2.14 排序查询

做全量查询,对查询结果中的年龄age字段做降序排序。

package com.szh.es;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import java.io.IOException;
/**
 *
 */
public class ESTestDocQuery {
    public static void main(String[] args) throws IOException {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        //4.对查询结果进行排序
        //创建搜索请求对象
        SearchRequest request = new SearchRequest();
        //设置参数 --- 表示查询哪个索引中的文档内容
        request.indices("user");
        //构建查询的请求体
        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        builder.sort("age", SortOrder.DESC);
        //将构建好的查询请求体存入搜索请求对象中
        request.source(builder);
        //发送请求 --- 获取响应
        SearchResponse response = esClient.search(request,RequestOptions.DEFAULT);
        //获取查询到的结果集
        SearchHits hits = response.getHits();
        System.out.println(hits.getTotalHits()); //结果集的条数
        System.out.println(response.getTook());  //总耗时
        //遍历结果集
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
        //关闭ES客户端
        esClient.close();
    }
}


2.15 过滤字段查询

做全量查询,同时排除性别sex字段。

package com.szh.es;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import java.io.IOException;
/**
 *
 */
public class ESTestDocQuery {
    public static void main(String[] args) throws IOException {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        //5.过滤字段
        //创建搜索请求对象
        SearchRequest request = new SearchRequest();
        //设置参数 --- 表示查询哪个索引中的文档内容
        request.indices("user");
        //构建查询的请求体
        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        String[] excludes = {"sex"};
        String[] includes = {};
        builder.fetchSource(includes,excludes);
        //将构建好的查询请求体存入搜索请求对象中
        request.source(builder);
        //发送请求 --- 获取响应
        SearchResponse response = esClient.search(request,RequestOptions.DEFAULT);
        //获取查询到的结果集
        SearchHits hits = response.getHits();
        System.out.println(hits.getTotalHits()); //结果集的条数
        System.out.println(response.getTook());  //总耗时
        //遍历结果集
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
        //关闭ES客户端
        esClient.close();
    }
}


2.16 组合条件查询

查询年龄 age=18 或者 name=张起灵的文档内容。

package com.szh.es;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import java.io.IOException;
/**
 *
 */
public class ESTestDocQuery {
    public static void main(String[] args) throws IOException {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        //6.组合查询
        //创建搜索请求对象
        SearchRequest request = new SearchRequest();
        //设置参数 --- 表示查询哪个索引中的文档内容
        request.indices("user");
        //构建查询的请求体
        SearchSourceBuilder builder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.should(QueryBuilders.matchQuery("age",18));
        boolQueryBuilder.should(QueryBuilders.matchQuery("name","张起灵"));
        builder.query(boolQueryBuilder);
        //将构建好的查询请求体存入搜索请求对象中
        request.source(builder);
        //发送请求 --- 获取响应
        SearchResponse response = esClient.search(request,RequestOptions.DEFAULT);
        //获取查询到的结果集
        SearchHits hits = response.getHits();
        System.out.println(hits.getTotalHits()); //结果集的条数
        System.out.println(response.getTook());  //总耗时
        //遍历结果集
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
        //关闭ES客户端
        esClient.close();
    }
}


2.17 范围查询

查询年龄age字段大于等于18、小于25的文档内容。

package com.szh.es;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import java.io.IOException;
/**
 *
 */
public class ESTestDocQuery {
    public static void main(String[] args) throws IOException {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        //7.范围查询
        //创建搜索请求对象
        SearchRequest request = new SearchRequest();
        //设置参数 --- 表示查询哪个索引中的文档内容
        request.indices("user");
        //构建查询的请求体
        SearchSourceBuilder builder = new SearchSourceBuilder();
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age");
        rangeQueryBuilder.gte(18);
        rangeQueryBuilder.lt(25);
        builder.query(rangeQueryBuilder);
        //将构建好的查询请求体存入搜索请求对象中
        request.source(builder);
        //发送请求 --- 获取响应
        SearchResponse response = esClient.search(request,RequestOptions.DEFAULT);
        //获取查询到的结果集
        SearchHits hits = response.getHits();
        System.out.println(hits.getTotalHits()); //结果集的条数
        System.out.println(response.getTook());  //总耗时
        //遍历结果集
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
        //关闭ES客户端
        esClient.close();
    }
}


2.18 模糊查询

builder.query(QueryBuilders.fuzzyQuery("name","小张").fuzziness(Fuzziness.ONE));最后的这个枚举类型 ONE,表示查询结果中允许与我定义的name字段为小张相差1个字符,也就是说,查询出的结果要么包含小、要么包含张、或者就是小张。

package com.szh.es;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import java.io.IOException;
/**
 *
 */
public class ESTestDocQuery {
    public static void main(String[] args) throws IOException {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        //8.模糊查询
        //创建搜索请求对象
        SearchRequest request = new SearchRequest();
        //设置参数 --- 表示查询哪个索引中的文档内容
        request.indices("user");
        //构建查询的请求体
        SearchSourceBuilder builder = new SearchSourceBuilder();
        //FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("name","小张");
        //fuzzyQueryBuilder.fuzziness(Fuzziness.ONE);
        //builder.query(fuzzyQueryBuilder);
        //上面三行代码 等价于 下面这行代码
        builder.query(QueryBuilders.fuzzyQuery("name","小张").fuzziness(Fuzziness.ONE));
        //将构建好的查询请求体存入搜索请求对象中
        request.source(builder);
        //发送请求 --- 获取响应
        SearchResponse response = esClient.search(request,RequestOptions.DEFAULT);
        //获取查询到的结果集
        SearchHits hits = response.getHits();
        System.out.println(hits.getTotalHits()); //结果集的条数
        System.out.println(response.getTook());  //总耗时
        //遍历结果集
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
        //关闭ES客户端
        esClient.close();
    }
}


2.19 聚合查询

查询age字段,年龄最大的文档内容。

package com.szh.es;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import java.io.IOException;
/**
 *
 */
public class ESTestDocQuery {
    public static void main(String[] args) throws IOException {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        //9.聚合查询
        //创建搜索请求对象
        SearchRequest request = new SearchRequest();
        //设置参数 --- 表示查询哪个索引中的文档内容
        request.indices("user");
        //构建查询的请求体
        SearchSourceBuilder builder = new SearchSourceBuilder();
        AggregationBuilder aggregationBuilder = AggregationBuilders.max("maxAge").field("age");
        builder.aggregation(aggregationBuilder);
        //将构建好的查询请求体存入搜索请求对象中
        request.source(builder);
        //发送请求 --- 获取响应
        SearchResponse response = esClient.search(request,RequestOptions.DEFAULT);
        //获取查询到的结果集
        SearchHits hits = response.getHits();
        System.out.println(hits.getTotalHits()); //结果集的条数
        System.out.println(response.getTook());  //总耗时
        //遍历结果集
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
        //关闭ES客户端
        esClient.close();
    }
}

2.20 分组查询


根据age年龄字段进行group分组查询。

package com.szh.es;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import java.io.IOException;
/**
 *
 */
public class ESTestDocQuery {
    public static void main(String[] args) throws IOException {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        //10.分组查询
        //创建搜索请求对象
        SearchRequest request = new SearchRequest();
        //设置参数 --- 表示查询哪个索引中的文档内容
        request.indices("user");
        //构建查询的请求体
        SearchSourceBuilder builder = new SearchSourceBuilder();
        AggregationBuilder aggregationBuilder = AggregationBuilders.terms("ageGroup").field("age");
        builder.aggregation(aggregationBuilder);
        //将构建好的查询请求体存入搜索请求对象中
        request.source(builder);
        //发送请求 --- 获取响应
        SearchResponse response = esClient.search(request,RequestOptions.DEFAULT);
        //获取查询到的结果集
        SearchHits hits = response.getHits();
        System.out.println(hits.getTotalHits()); //结果集的条数
        System.out.println(response.getTook());  //总耗时
        //遍历结果集
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
        //关闭ES客户端
        esClient.close();
    }
}
相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
2月前
|
Java API Apache
Java编程如何读取Word文档里的Excel表格,并在保存文本内容时保留表格的样式?
【10月更文挑战第29天】Java编程如何读取Word文档里的Excel表格,并在保存文本内容时保留表格的样式?
183 5
|
4月前
|
缓存 Java 关系型数据库
【Java面试题汇总】ElasticSearch篇(2023版)
倒排索引、MySQL和ES一致性、ES近实时、ES集群的节点、分片、搭建、脑裂、调优。
|
4月前
|
数据可视化 Java Windows
Elasticsearch入门-环境安装ES和Kibana以及ES-Head可视化插件和浏览器插件es-client
本文介绍了如何在Windows环境下安装Elasticsearch(ES)、Elasticsearch Head可视化插件和Kibana,以及如何配置ES的跨域问题,确保Kibana能够连接到ES集群,并提供了安装过程中可能遇到的问题及其解决方案。
Elasticsearch入门-环境安装ES和Kibana以及ES-Head可视化插件和浏览器插件es-client
|
26天前
|
存储 缓存 监控
极致 ElasticSearch 调优,让你的ES 狂飙100倍!
尼恩分享了一篇关于提升Elasticsearch集群的整体性能和稳定性措施的文章。他从硬件、系统、JVM、集群、索引和查询等多个层面对ES的性能优化进行分析,帮助读者提升技术水平。
|
2月前
|
关系型数据库 MySQL Java
MySQL索引优化与Java应用实践
【11月更文挑战第25天】在大数据量和高并发的业务场景下,MySQL数据库的索引优化是提升查询性能的关键。本文将深入探讨MySQL索引的多种类型、优化策略及其在Java应用中的实践,通过历史背景、业务场景、底层原理的介绍,并结合Java示例代码,帮助Java架构师更好地理解并应用这些技术。
82 2
|
3月前
|
Java Linux
java读取linux服务器下某文档的内容
java读取linux服务器下某文档的内容
49 3
java读取linux服务器下某文档的内容
|
3月前
|
存储 JSON Java
elasticsearch学习一:了解 ES,版本之间的对应。安装elasticsearch,kibana,head插件、elasticsearch-ik分词器。
这篇文章是关于Elasticsearch的学习指南,包括了解Elasticsearch、版本对应、安装运行Elasticsearch和Kibana、安装head插件和elasticsearch-ik分词器的步骤。
350 0
elasticsearch学习一:了解 ES,版本之间的对应。安装elasticsearch,kibana,head插件、elasticsearch-ik分词器。
|
3月前
|
自然语言处理 搜索推荐 Java
SpringBoot 搜索引擎 海量数据 Elasticsearch-7 es上手指南 毫秒级查询 包括 版本选型、操作内容、结果截图(一)
SpringBoot 搜索引擎 海量数据 Elasticsearch-7 es上手指南 毫秒级查询 包括 版本选型、操作内容、结果截图
73 0
|
3月前
|
存储 自然语言处理 搜索推荐
SpringBoot 搜索引擎 海量数据 Elasticsearch-7 es上手指南 毫秒级查询 包括 版本选型、操作内容、结果截图(二)
SpringBoot 搜索引擎 海量数据 Elasticsearch-7 es上手指南 毫秒级查询 包括 版本选型、操作内容、结果截图(二)
56 0
|
4月前
|
JSON 自然语言处理 数据库
ElasticSearch基础1——索引和文档。Kibana,RestClient操作索引和文档+黑马旅游ES库导入
概念、ik分词器、倒排索引、索引和文档的增删改查、RestClient对索引和文档的增删改查
ElasticSearch基础1——索引和文档。Kibana,RestClient操作索引和文档+黑马旅游ES库导入