elasticsearch 使用RestHighLevelClient

2020/04/27 14:47
阅读数 4.8K
AI总结

因公司使用ES搜索引擎,所以自己研究了几天,目前看网上的帖子五花八门,而且好多版本不用导致不能调用.

顾自己硬着头皮,看官方介绍,逐字翻译api,终于功夫不负有心人,demo可以使用了

本人用es最新版本es-7.2.0,客户端用RestHighLevelClient.还有一个客户端是transportClient客户端,但注意这个客户端将要被弃用.虽然本人开始研究的时候,是这个客户端,但为了后期,就果断放弃此客户端.

代码框架springboot2.0.X版本,springboot也有自己的封装客户端,好像是rest什么的,但底层封装的客户端是transportClient,而且和官方介绍的api差很多,boot也没有一个介绍相关调用的案列,顾没有深入研究.

废话不说,直接上代码了

 

我们是集群的配置:

配置文件:

bootstrap.yml.

firstIp: bs01 secondIp: bs02 thirdIp: bs03 firstPort: 9093 secondPort: 9093 thirdPort: 9093 clusterName: bses

创建客户端:

ESClientSpringFactory

package com.ztgx.es.config;
 
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
 
@Configuration
public class ESClientSpringFactory {
 
    private Logger logger  = LoggerFactory.getLogger(this.getClass());
 
    @Value("${elasticsearch.firstIp}")
    private String firstIp;
    @Value("${elasticsearch.secondIp}")
    private String secondIp;
    @Value("${elasticsearch.thirdIp}")
    private String thirdIp;
    @Value("${elasticsearch.firstPort}")
    private String firstPort;
    @Value("${elasticsearch.secondPort}")
    private String secondPort;
    @Value("${elasticsearch.thirdPort}")
    private String thirdPort;
    @Value("${elasticsearch.clusterName}")
    private String clusterName;
 
 
    @Bean
    public  RestHighLevelClient getrestHighLevelClient() throws IOException {
        RestHighLevelClient client = null;
        logger.info("ElasticSearch初始化开始。。");
        logger.info("要连接的节点1的ip是{},端口是{},集群名为{}" , firstIp , firstPort , clusterName);
        logger.info("要连接的节点2的ip是{},端口是{},集群名为{}" , secondIp , secondPort , clusterName);
        logger.info("要连接的节点3的ip是{},端口是{},集群名为{}" , thirdIp , thirdPort , clusterName);
        try {
 
            client = new RestHighLevelClient(
                    RestClient.builder(
                            new HttpHost(InetAddress.getByName(firstIp),Integer.parseInt(firstPort),"http"),
                            new HttpHost(InetAddress.getByName(firstIp),Integer.parseInt(firstPort),"http"),
                            new HttpHost(InetAddress.getByName(firstIp),Integer.parseInt(firstPort),"http")));
 
            logger.info("ElasticSearch初始化完成。。");
        } catch (UnknownHostException e) {
            e.printStackTrace();
            logger.error("ElasticSearch初始化失败:" +  e.getMessage(),e);
            client.close();
        }
 
 
        return client;
    }
 
}
直接调用demo:

package com.ztgx.es;
 
 
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.ScoreSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.io.IOException;
 
@Slf4j
@RestController
@RequestMapping("/test")
public class testcontroller {
 
 
    @Qualifier("getrestHighLevelClient")
    @Autowired
    private RestHighLevelClient client;
 
    @PostMapping("/testes")
    public SearchResponse matchQueryBuilder() throws IOException {
        try {
 
            //模糊查询
            MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("field", "bar");
            matchQueryBuilder.fuzziness(Fuzziness.AUTO);
            matchQueryBuilder.prefixLength(0);
            matchQueryBuilder.maxExpansions(10);
 
        /*QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("user", "kimchy")
                .fuzziness(Fuzziness.AUTO)
                .prefixLength(3)
                .maxExpansions(10);*/
 
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(matchQueryBuilder);
 
 
            //排序
            //Sort descending by _score (the default)
            searchSourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));//
            //Also sort ascending by _id field
            searchSourceBuilder.sort(new FieldSortBuilder("_id").order(SortOrder.ASC));
 
 
            //查询指定字段   不查询指定字段
            //该方法还接受一个由一个或多个通配符模式组成的数组,以控制以更细粒度的方式包含或排除哪些字段:
//        searchSourceBuilder.fetchSource(false);
//        String[] includeFields = new String[]{"field"};
//        String[] excludeFields = Strings.EMPTY_ARRAY;
//        searchSourceBuilder.fetchSource(includeFields, excludeFields);
 
 
            SearchRequest searchRequest = new SearchRequest();
            searchRequest.indices("posts");
            searchRequest.source(searchSourceBuilder);
            SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
            System.out.println(search);
            return search;
 
        }catch(Exception e){
            client.close();
        e.getStackTrace();
            try {
                client.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }finally {
            client.close();
        }
        return null;
    }
}
 

pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>bigdataservice</artifactId>
        <groupId>com.ztgx.bigdataservice</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
 
    <artifactId>bigdataservice-web-es</artifactId>
 
    <dependencies>
 
        <!-- 单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.2.0</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
 
 
 
    </dependencies>
 
 
</project>
 

上面的直接就可以实现了,挺简单的.

本人不才,自己有写了几个测试api,如有需要可以看一看.

package com.ztgx.es.config;
 
 
import org.apache.http.HttpHost;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.fieldcaps.FieldCapabilities;
import org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequest;
import org.elasticsearch.action.fieldcaps.FieldCapabilitiesResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.ShardSearchFailure;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.replication.ReplicationResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.Avg;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.ScoreSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;
 
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
 
import static java.util.Collections.singletonMap;
 
 
public class ESbean {
 
    RestHighLevelClient client = new RestHighLevelClient(
            RestClient.builder(
                    new HttpHost("ip1", 9093, "http"),//此处写你自己的ip,如果是单机版本,删掉其他两个就好了
                    new HttpHost("ip2", 9093, "http"),
                    new HttpHost("ip3", 9093, "http")));
//@Qualifier("getrestHighLevelClient")
//@Autowired
//private RestHighLevelClient client;
 
 
    /**
     * 创建索引
     *
     * @throws IOException
     */
    @Test
    public void creatIndex() throws IOException {
 
 
        // IndexRequest request = new IndexRequest("posts");
     /*   Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("user", "kimchy");
        jsonMap.put("postDate", new Date());
        jsonMap.put("message", "trying out Elasticsearch");
        IndexRequest indexRequest = new IndexRequest("posts")
                .id("1").source(jsonMap);*/
 
        //以映射形式提供的文档源,该映射将自动转换为JSON格式
        /*XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.startObject();
        {
            builder.field("user", "kimchy");
            builder.timeField("postDate", new Date());
            builder.field("message", "trying out Elasticsearch");
        }
        builder.endObject();
        IndexRequest indexRequest = new IndexRequest("posts")
                .id("1").source(builder);*/
 
        //文档源作为XContentBuilder对象提供,Elasticsearch内置助手生成JSON内容
        IndexRequest indexRequest = new IndexRequest("posts")
                .id("1")
                .source("user", "kimchy",
                        "postDate", new Date(),
                        "message", "trying out Elasticsearch");
 
        //同步执行
        //当以下列方式执行索引请求时,客户端在继续执行代码之前等待返回索引响应:
        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println(indexResponse);
 
        String index = indexResponse.getIndex();
        String id = indexResponse.getId();
 
        if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
            //处理(如果需要)第一次创建文档的情况
 
        } else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
            //处理(如果需要)将文档重写为已经存在的情况
 
        }
        ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
        if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
            //处理成功碎片的数量少于总碎片的情况
 
        }
        if (shardInfo.getFailed() > 0) {
            for (ReplicationResponse.ShardInfo.Failure failure :
                    shardInfo.getFailures()) {
                String reason = failure.reason();
                //处理潜在的故障
            }
        }
    }
 
    /**
     * 获取文档中的数据
     */
    @Test
    public void getApi() throws IOException {
 
 
        //GetRequest()方法第一个参数是索引的名字,第二个参数是文档的id
        GetRequest getRequest = new GetRequest("posts", "1");
 
        //查询特定字段,包含和不包含的字段
        String[] includes = new String[]{"message", "*Date", "user"};//查询的字段
        String[] excludes = new String[]{};//不查询的字段
        FetchSourceContext fetchSourceContext =
                new FetchSourceContext(true, includes, excludes);
        getRequest.fetchSourceContext(fetchSourceContext);
 
 
        //为特定的存储字段配置检索(要求字段在映射中单独存储)
        getRequest.storedFields("message");
        GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
        System.out.println(getResponse);
 
        String index = getResponse.getIndex();
        String id = getResponse.getId();
        if (getResponse.isExists()) {
            long version = getResponse.getVersion();
            //将结果封装成String   或者map类型  或者bytes类型
            String sourceAsString = getResponse.getSourceAsString();
            Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
            byte[] sourceAsBytes = getResponse.getSourceAsBytes();
 
            // 获取结果集中某个字段的数据
            String message = (String) getResponse.getSource().get("message");
            System.out.println(message);
        } else {
 
            // 处理没有找到文档的场景。注意,虽然返回的响应有404状态代码,但是返回的是有效的GetResponse,
            // 而不是抛出异常。这样的响应不包含任何源文档,并且它的isExists方法返回     }
 
        }
    }
 
 
    @Test
    public void deleteApi() throws IOException {
 
        //索引   以及id
        DeleteRequest request = new DeleteRequest("posts", "1");
        DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);
 
        //异步删除
        //client.deleteAsync(request, RequestOptions.DEFAULT, listener);
 
        String index = deleteResponse.getIndex();
        String id = deleteResponse.getId();
        long version = deleteResponse.getVersion();
        ReplicationResponse.ShardInfo shardInfo = deleteResponse.getShardInfo();
        if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
 
        }
        if (shardInfo.getFailed() > 0) {
            for (ReplicationResponse.ShardInfo.Failure failure :
                    shardInfo.getFailures()) {
                String reason = failure.reason();
            }
        }
    }
 
    /**
     * 更新
     *
     * @throws IOException
     */
    public void updateApi() throws IOException {
 
        UpdateRequest request = new UpdateRequest("posts", "1");
 
        Map<String, Object> parameters = singletonMap("count", 4);
 
        Script inline = new Script(ScriptType.INLINE, "painless",
                "ctx._source.field += params.count", parameters);
        request.script(inline);
 
        //更新特定字段
        String[] includes = new String[]{"updated", "r*"};
        String[] excludes = Strings.EMPTY_ARRAY;
        request.fetchSource(
                new FetchSourceContext(true, includes, excludes));
 
        UpdateResponse updateResponse = client.update(
                request, RequestOptions.DEFAULT);
 
    }
 
    /**
     * 批量添加  删除
     */
    @Test
    public void bulkRequest() throws IOException {
        //添加
        BulkRequest request = new BulkRequest();
        request.add(new IndexRequest("posts").id("5")
                .source(XContentType.JSON, "user", "赵琦水岸城1", "name", "赵琦水岸城1"));
        request.add(new IndexRequest("posts").id("7")
                .source(XContentType.JSON, "user", "张三红包2", "name", "马六红包2"));
        request.add(new IndexRequest("posts").id("8")
                .source(XContentType.JSON, "user", "王五红包3", "name", "王五红包3"));
        request.add(new IndexRequest("posts").id("9")
                .source(XContentType.JSON, "user", "王五", "name", "zhangsan红包4"));
        request.add(new IndexRequest("posts").id("10")
                .source(XContentType.JSON, "user", "李四红包5", "name", "李四红包5"));
        request.add(new IndexRequest("posts").id("6")
                .source(XContentType.JSON, "user", "张三红包6", "name", "张三红包6"));
//
//        删除
//        BulkRequest request1 = new BulkRequest();
//        request1.add(new DeleteRequest("posts", "3"));
//        request1.add(new UpdateRequest("posts", "2")
//                .doc(XContentType.JSON,"other", "test"));
//        request1.add(new IndexRequest("posts").id("4")
//                .source(XContentType.JSON,"field", "baz"));
 
        BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT);
    }
 
    /**
     * SearchRequest用于与搜索文档、聚合、建议有关的任何操作,还提供了请求突出显示结果文档的方法。
     */
    @Test
    public void SearchRequest() throws IOException {
 
        //创建SeachRequest。如果没有参数,则运行所有索引。
       /* SearchRequest searchRequest = new SearchRequest();
 
        //大多数搜索参数都添加到SearchSourceBuilder中。它为进入搜索请求体的所有内容提供setter。
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //向SearchSourceBuilder添加一个match_all查询。
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchRequest.source(searchSourceBuilder);
        */
 
        //查询具体得到索引下的数据
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //设置查询。可以是任何类型
        sourceBuilder.query(QueryBuilders.termQuery("name", "王五"));
 
        //分页
        sourceBuilder.from(0);
        sourceBuilder.size(5);
 
        //设置一个可选的超时,控制允许搜索花费的时间。
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("posts");
        searchRequest.source(sourceBuilder);
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
        //getProfileResults
        System.out.println(search);
        System.out.println(search.getProfileResults().get("name"));
 
 
    }
 
    /**
     * 模糊查询
     *
     * @throws IOException
     */
    @Test
    public void matchQueryBuilder() throws IOException {
 
        //QueryBuilder matchQueryBuilder = QueryBuilders.termQuery("user", "3").//也是模糊查询,但对中文不兼容
        // QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("user", "王五").//模糊查询
//                .fuzziness(Fuzziness.AUTO)
//                .prefixLength(3)
//                .maxExpansions(10);*/
 
        //模糊查询
         MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("user.keyword", "李四");//加上.keyword就是精确查找
        matchQueryBuilder.fuzziness(Fuzziness.AUTO);
        matchQueryBuilder.prefixLength(0);// 在匹配查询上设置前缀长度选项
        matchQueryBuilder.maxExpansions(1);// 设置最大扩展选项以控制查询的模糊过程
 
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(matchQueryBuilder);
 
        //排序
        //Sort descending by _score (the default)
        searchSourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));//
        //Also sort ascending by _id field
        searchSourceBuilder.sort(new FieldSortBuilder("_id").order(SortOrder.ASC));
 
 
        //查询指定字段   不查询指定字段
        //该方法还接受一个由一个或多个通配符模式组成的数组,以控制以更细粒度的方式包含或排除哪些字段:
//        searchSourceBuilder.fetchSource(false);
//        String[] includeFields = new String[]{"name"};
//        String[] excludeFields = Strings.EMPTY_ARRAY;
//        searchSourceBuilder.fetchSource(includeFields, excludeFields);
 
 
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("posts");
        searchRequest.source(searchSourceBuilder);
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
 
        System.out.println(search);
 
    }
 
    /**
     * 聚合查询
     * 通过首先创建适当的AggregationBuilder,然后将其设置在SearchSourceBuilder上,
     * 可以将聚合添加到搜索中。在下面的示例中,我们创建了一个基于公司名称的术语聚合,其中包含一个关于公司员工平均年龄的子聚合
     * terms相当于是别名
     */
 
     /**
     * 聚合查询
     * 通过首先创建适当的AggregationBuilder,然后将其设置在SearchSourceBuilder上,
     * 可以将聚合添加到搜索中。在下面的示例中,我们创建了一个基于公司名称的术语聚合,其中包含一个关于公司员工平均年龄的子聚合
     * terms相当于是别名
     */
    @Test
    public void Aggregations() throws IOException {
 
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //查询条件
        QueryBuilder query = QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("area.keyword", "阳原县"));
        //统计条件
        TermsAggregationBuilder serviceLineAgg = AggregationBuilders.terms("serviceline_count").field("area.keyword").size(30);
        TermsAggregationBuilder appNameAgg = AggregationBuilders.terms("appName_count").field("company_org_type.keyword").size(30);
 
        searchSourceBuilder.query(query).size(0);
       searchSourceBuilder.aggregation(serviceLineAgg.subAggregation(appNameAgg)).size(0);
 
 
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("company");
        searchRequest.source(searchSourceBuilder);
        System.out.println(searchRequest);
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
       System.out.println(response);
 
        Map<String, Aggregation> serviceLineMap = response.getAggregations().asMap();
        ParsedStringTerms serviceLineTerms = (ParsedStringTerms) serviceLineMap.get("serviceline_count");
        List serviceLists = serviceLineTerms.getBuckets();
        for (Object serviceList : serviceLists) {
            ParsedStringTerms.ParsedBucket serviceListObj = (ParsedStringTerms.ParsedBucket) serviceList;
            String serviceLine = serviceListObj.getKeyAsString();
            Map<String, Aggregation> appNameMap = serviceListObj.getAggregations().asMap();
            ParsedStringTerms appNameTerms = (ParsedStringTerms) appNameMap.get("appName_count");
            List appNameLists = appNameTerms.getBuckets();
            for (Object appNameList : appNameLists) {
                ParsedStringTerms.ParsedBucket appNameObj = (ParsedStringTerms.ParsedBucket) appNameList;
                String appName = appNameObj.getKeyAsString();
                Long count = appNameObj.getDocCount();
                System.out.println(serviceLine );
                System.out.println(appName);
                System.out.println(count);
            }
        }
 
 
    }

 /**
     * 按月统计总数
     * @throws IOException
     */
    @Test
    public void Aggregations12() throws IOException {
 
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //查询条件
      //  QueryBuilder query = QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("area.keyword", "阳原县"));
        QueryBuilder query = QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery("updatetime").gte("2019-04-19T01:00:00.000").lt("2019-08-19T00:00:00.000"));
        //TermsAggregationBuilder aggregation = AggregationBuilders.terms("by_company").field("company_org_type.keyword");
        AggregationBuilder  aggregation1 = AggregationBuilders.dateHistogram("group_by_day").
                field("updatetime").calendarInterval(DateHistogramInterval.MONTH).minDocCount(0).
                        format("yyyy-MM-dd HH:mm:ss").extendedBounds( new ExtendedBounds("2019-01-19 00:00:00","2019-08-19 00:00:00"));//这句话可以返回里其中的空文档
 
        //aggregation.subAggregation(aggregation1);
        searchSourceBuilder.aggregation(aggregation1).size(0);
        searchSourceBuilder.query(query).size(0);
 
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("company");
        searchRequest.source(searchSourceBuilder);
        System.out.println(searchRequest);
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println("返回结果:"+response);
 
 
        Map<String, Aggregation> serviceLineMap = response.getAggregations().asMap();
        ParsedDateHistogram serviceLineTerms = (ParsedDateHistogram) serviceLineMap.get("group_by_day");
        List serviceLists = serviceLineTerms.getBuckets();
        for (Object serviceList : serviceLists) {
            ParsedDateHistogram.ParsedBucket serviceListObj = (ParsedDateHistogram.ParsedBucket) serviceList;
            String serviceLine = serviceListObj.getKeyAsString();
            Map<String, Aggregation> appNameMap = serviceListObj.getAggregations().asMap();
           // ParsedDateHistogram appNameTerms = (ParsedDateHistogram) appNameMap.get("group_by_day");
           System.out.println(serviceLine);
//            List appNameLists = appNameTerms.getBuckets();
//            for (Object appNameList : appNameLists) {
//                ParsedDateHistogram.ParsedBucket appNameObj = (ParsedDateHistogram.ParsedBucket) appNameList;
//                String appName = appNameObj.getKeyAsString();
//                Long count = appNameObj.getDocCount();
//                System.out.println(serviceLine);
//                System.out.println(appName);
//                System.out.println(count);
//            }
        }
 
 
    }
}
 
 
 /**
     * 按天统计  请求的人数
     *
     * @throws IOException
     */
    @Test
    public void Aggregations12() throws IOException {
 
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().trackTotalHits(true);
        QueryBuilder query = QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery("startTime.keyword").gte("2020-04-20 15:10:55").lte("2020-04-24 15:10:55")).must(QueryBuilders.termQuery("interfaceName.keyword", "/X/X/X/login/X"));//.must(QueryBuilders.termQuery("appId.keyword","1156082838098079746"));
        AggregationBuilder aggregation1 = AggregationBuilders.dateHistogram("group_by_day").
                field("endTime_UTC").calendarInterval(DateHistogramInterval.DAY).minDocCount(0).format("yyyy-MM-dd HH:mm:ss").extendedBounds(new ExtendedBounds("2020-04-19 00:00:00", "2020-04-24 00:00:00"))
                .subAggregation(AggregationBuilders.terms("requestParam").field("requestParam.keyword"));
        searchSourceBuilder.aggregation(aggregation1).size(0);//全局
        searchSourceBuilder.query(query).size(0);
        searchSourceBuilder.fetchSource(new String[]{"userId", "startTime", "appId"}, new String[]{});
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("log_index");
        searchRequest.source(searchSourceBuilder);
        System.out.println(searchRequest);
        SearchResponse response = AccountUserLogin.client().search(searchRequest, RequestOptions.DEFAULT);
        System.out.println("返回结果:" + response);
 
        Map<String, Aggregation> serviceLineMap = response.getAggregations().asMap();
        ParsedDateHistogram serviceLineTerms = (ParsedDateHistogram) serviceLineMap.get("group_by_day");
 
        List<ParsedDateHistogram.ParsedBucket> buckets = (List<ParsedDateHistogram.ParsedBucket>) serviceLineTerms.getBuckets();
 
        Map map=new HashMap<>();
        JSONObject jsonObject=new JSONObject();
 
        for (int i = 0; i < buckets.size(); i++) {
            ParsedDateHistogram.ParsedBucket bucket = buckets.get(i);
            ZonedDateTime timestamp = (ZonedDateTime) bucket.getKey();
            System.out.println(timestamp);
            ParsedStringTerms lt = bucket.getAggregations().get("requestParam");
            List<ParsedStringTerms.ParsedBucket> bucketList = (List<ParsedStringTerms.ParsedBucket>) lt.getBuckets();
            HashSet hashSet = new HashSet<>();
            for (ParsedStringTerms.ParsedBucket item : bucketList) {
                String deliveryId = (String) item.getKey();
                JSONObject json= JSONObject.parseObject(deliveryId);
                hashSet.add(json.get("phone"));
            }
            jsonObject.put(timestamp+"",hashSet);
        }
        System.out.println(jsonObject);
    }
 
 
 
    /**
     * 多个索引之间查询
     */
    public void moreIndexs() throws IOException {
 
        FieldCapabilitiesRequest request = new FieldCapabilitiesRequest()
                .fields("user")
                .indices("posts", "authors", "contributors");
 
        request.indicesOptions(IndicesOptions.lenientExpandOpen());
        FieldCapabilitiesResponse response = client.fieldCaps(request, RequestOptions.DEFAULT);
        Map<String, FieldCapabilities> userResponse = response.getField("user");
        FieldCapabilities textCapabilities = userResponse.get("keyword");
 
        boolean isSearchable = textCapabilities.isSearchable();
        boolean isAggregatable = textCapabilities.isAggregatable();
 
        String[] indices = textCapabilities.indices();
        String[] nonSearchableIndices = textCapabilities.nonSearchableIndices();
        String[] nonAggregatableIndices = textCapabilities.nonAggregatableIndices();
 
 
    }
 
    /**
     *
     * 具体代码  可参考:https://blog.csdn.net/majun_guang/article/details/81103623
     * 根据多条件组合与查询
     *
     * 多条件或查询
     * must 就像sql里的and   相较于sql  select * from accounts.person where title='JAVA开发工程师' and age=30
     *
     *
     * should 就像sql里的or  SortBuilder 的作用不言而喻就是用来排序
     * 以上代码相较于sql  select * from   accounts.person where user='kimchy14' or  user='kimchy15'   ;
     *
     *
     * 范围查询rangeQuery.from(30,true)方法是大于30  后面的参数是是否包含 为true的话就是大于等于30 to就相当于小于
     * 如果也有包含参数为true的话就是小于等于  gt 是大于 gte是大于等于   lt是小于 lte是小于等于  第一句的builder就相当于 select * from accounts.person where age >=30 and age<=30; 
     *
     *
     * 包含查询使用termsQuery 可以传列表 也可以传多个参数 或者数组 setFetchSource有两个参数
     * 第一个参数是包含哪些参数 第二个参数是排除哪些参数   
     * 以上这段代码就相当于sql  select age from accounts.person where user in ('kimchy14','kimchy15','kimchy16');
     *
     *
     * 通配符查询像我们sql里的like 但是还不一样 like的百分号可以加到前后   
     * elasticsearch技术解析与实战中有一句话 是这么说的 为了避免极端缓慢的通配符查询
     * 通配符索引词不应该以一个通配符开头 通配符查询应该避免以通配符开头 
     *
     *
     常见统计  统计分为指标 和 桶 桶就是我们统计的样本  指标就是我们平时所查的count  sum  与sql不一样的是 我们还可以将统计的样本拿到 就是response.getHits
     */
    public void moretermQuery(){
//        QueryBuilder qb=QueryBuilders.termQuery("user","kimchy14");
//        QueryBuilder qb1=QueryBuilders.termQuery("user","kimchy15");
//
//        SortBuilder sortBuilder= SortBuilders.fieldSort("age");
//        sortBuilder.order(SortOrder.DESC);
//        QueryBuilder s=QueryBuilders.boolQuery().should(qb).should(qb1);//.must(qb5);
//        SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(s).addSort(sortBuilder).setFrom(0)
//                .setSize(100);
//        logger.log(Level.INFO,sv.toString());
//        SearchResponse response=  sv.get();
//        SearchHits searchHits =  response.getHits();
//        for(SearchHit hit:searchHits.getHits()){
//            logger.log(Level.INFO , hit.getSourceAsString());
//        }
//
//        }
        //范围查询
        // RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").from(30,true).to(30,true);
//        // RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").gt(30 );
//        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").gte(30 );
//        QueryBuilder s=QueryBuilders.boolQuery().must(rangeQueryBuilder);//.must(qb5);
//        SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(s).setFrom(0)
//                .setSize(100);
//        logger.log(Level.INFO,sv.toString());
//        SearchResponse response=  sv.get();
//        SearchHits searchHits =  response.getHits();
//        for(SearchHit hit:searchHits.getHits()){
//            logger.log(Level.INFO , hit.getSourceAsString());
        }
    }
 

经过了挺大的心血,研究的差不多了,也能够用了.如有疑问,可以提问.
————————————————
版权声明:本文为CSDN博主「爪哇狗」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zhangshng/java/article/details/95946596

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
0
分享
AI总结
返回顶部
顶部