ElasticSearch学习(十二)在Java应用中实现组合查询、实现桶聚合

//组合查询
    @Test
    public void test28() throws Exception {
        //指定ES集群
        Settings settings = Settings.builder().put("cluster.name", "my-application").build();

        //创建访问ES服务器的客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.123.101"),9300));

        //使用bool实现组合查询
        BoolQueryBuilder builder = QueryBuilders.boolQuery()
                .must(QueryBuilders.matchQuery("interests", "duanlian"))
                .mustNot(QueryBuilders.matchQuery("name", "wangwu"))
                .should(QueryBuilders.matchQuery("address", "bei jing"))
                .filter(QueryBuilders.rangeQuery("birthday").gte("1990-01-01").format("yyyy-MM-dd"));
        SearchResponse response = client.prepareSearch("lib3")
                .setQuery(builder)
                .get();
        SearchHits hits = response.getHits();
        for (SearchHit hit : hits){
            System.out.println(hit.getSourceAsString());
        }

    }



    //组合查询
    @Test
    public void test29() throws Exception {
        //指定ES集群
        Settings settings = Settings.builder().put("cluster.name", "my-application").build();

        //创建访问ES服务器的客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.123.101"),9300));

        //使用constantscore实现组合查询
        ConstantScoreQueryBuilder builder = QueryBuilders.constantScoreQuery(QueryBuilders.termQuery("name", "zhaoliu"));

        SearchResponse response = client.prepareSearch("lib3")
                .setQuery(builder)
                .get();
        SearchHits hits = response.getHits();
        for (SearchHit hit : hits){
            System.out.println(hit.getSourceAsString());
        }
    }
    //分组聚合
    @Test
    public void test30() throws Exception {
        //指定ES集群
        Settings settings = Settings.builder().put("cluster.name", "my-application").build();

        //创建访问ES服务器的客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.123.101"),9300));

        TermsAggregationBuilder builder = AggregationBuilders.terms("terms").field("age");
        SearchResponse response = client.prepareSearch("lib3").addAggregation(builder).execute().actionGet();
        Terms terms = response.getAggregations().get("terms");

        for (Terms.Bucket entry:terms.getBuckets()){
            System.out.println(entry.getKey() +":"+entry.getDocCount());
        }
    }
    //过滤聚合filter
    @Test
    public void test31() throws Exception {
        //指定ES集群
        Settings settings = Settings.builder().put("cluster.name", "my-application").build();

        //创建访问ES服务器的客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.123.101"),9300));

        TermQueryBuilder builder = QueryBuilders.termQuery("age", 20);

        FilterAggregationBuilder aggregationBuilder = AggregationBuilders.filter("filter", builder);

        SearchResponse response = client.prepareSearch("lib3").addAggregation(aggregationBuilder).execute().actionGet();

        Filter filter = response.getAggregations().get("filter");
        System.out.println(filter.getDocCount());

    }


    //过滤聚合filters
    @Test
    public void test32() throws Exception {
        //指定ES集群
        Settings settings = Settings.builder().put("cluster.name", "my-application").build();

        //创建访问ES服务器的客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.123.101"),9300));

        FiltersAggregationBuilder agg = AggregationBuilders.filters("filters",
                new FiltersAggregator.KeyedFilter("duanlian", QueryBuilders.termQuery("interests", "duanlian")),
                new FiltersAggregator.KeyedFilter("hejiu", QueryBuilders.termQuery("interests", "hejiu"))
        );
        SearchResponse response = client.prepareSearch("lib3").addAggregation(agg).execute().actionGet();
        Filters filters = response.getAggregations().get("filters");

        for (Filters.Bucket entry : filters.getBuckets()){
            System.out.println(entry.getKey() +":"+entry.getDocCount());
        }
    }

    //range聚合
    @Test
    public void test33() throws Exception {
        //指定ES集群
        Settings settings = Settings.builder().put("cluster.name", "my-application").build();

        //创建访问ES服务器的客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.123.101"),9300));

        RangeAggregationBuilder aggregationBuilder = AggregationBuilders
                .range("range")
                .field("age")
                .addUnboundedTo(50)
                .addRange(25, 50)
                .addUnboundedFrom(25);
        SearchResponse response = client.prepareSearch("lib3").addAggregation(aggregationBuilder).execute().actionGet();
        Range range = response.getAggregations().get("range");

        for (Range.Bucket entry : range.getBuckets()){
            System.out.println(entry.getKey() +":"+entry.getDocCount());
        }
    }


    //missing聚合 统计某个字段上的值为null的值有多少个
    @Test
    public void test34() throws Exception {
        //指定ES集群
        Settings settings = Settings.builder().put("cluster.name", "my-application").build();

        //创建访问ES服务器的客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.123.101"),9300));

        MissingAggregationBuilder agg = AggregationBuilders.missing("missing").field("price");
        SearchResponse response = client.prepareSearch("lib4").addAggregation(agg).execute().actionGet();

        Aggregation aggregation = response.getAggregations().get("missing");
        System.out.println(aggregation.toString());


    }

 

你可能感兴趣的:(ElasticSearch)