ElasticSearch学习(十一)在Java应用中实现聚合查询、query string查询

    //聚合查询
    @Test
    public void test20() 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));

        //求最大值
        MaxAggregationBuilder builder = AggregationBuilders.max("aggMax").field("age");

        SearchResponse response = client.prepareSearch("lib3").addAggregation(builder).get();

        Max max = response.getAggregations().get("aggMax");
        double value = max.getValue();
        System.out.println(value);
    }

    //聚合查询
    @Test
    public void test21() 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));

        //求最小值
        AggregationBuilder builder = AggregationBuilders.min("aggMin").field("age");

        SearchResponse response = client.prepareSearch("lib3").addAggregation(builder).get();

        Min min = response.getAggregations().get("aggMin");
        double value = min.getValue();
        System.out.println(value);
    }

    //聚合查询
    @Test
    public void test22() 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));

        //求平均值
        AggregationBuilder builder = AggregationBuilders.avg("aggavg").field("age");

        SearchResponse response = client.prepareSearch("lib3").addAggregation(builder).get();

        Avg avg = response.getAggregations().get("aggavg");
        double value = avg.getValue();
        System.out.println(value);
    }
    //聚合查询
    @Test
    public void test23() 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));

        //求平均值
        AggregationBuilder builder = AggregationBuilders.sum("aggsum").field("age");

        SearchResponse response = client.prepareSearch("lib3").addAggregation(builder).get();

        Sum sum = response.getAggregations().get("aggsum");
        double value = sum.getValue();
        System.out.println(value);
    }

    //聚合查询
    @Test
    public void test24() 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));

        //求基数(有多少个互不相同的值)
        AggregationBuilder builder = AggregationBuilders.cardinality("aggcardinality").field("age");

        SearchResponse response = client.prepareSearch("lib3").addAggregation(builder).get();

        Cardinality cardinality = response.getAggregations().get("aggcardinality");
        double value = cardinality.getValue();
        System.out.println(value);
    }


    //query string查询   相当于   GET lib3/user/_search?q=_id:1
    @Test
    public void test25() 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));

        //指定字段搜索
        CommonTermsQueryBuilder builder = QueryBuilders.commonTermsQuery("name", "zhaoliu");
        SearchResponse response = client.prepareSearch("lib3")
                .setQuery(builder)
                .get();
        SearchHits hits = response.getHits();
        for (SearchHit hit : hits){
            System.out.println(hit.getSourceAsString());
        }


    }

    //query string查询   相当于   GET lib3/user/_search?q=_id:1
    @Test
    public void test26() 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));

        //不用指定字段,全文搜索 包含duanlian  不包含hejiu 精确查询
        QueryStringQueryBuilder builder = QueryBuilders.queryStringQuery("+duanlian -hejiu");


        SearchResponse response = client.prepareSearch("lib3")
                .setQuery(builder)
                .get();
        SearchHits hits = response.getHits();
        for (SearchHit hit : hits){
            System.out.println(hit.getSourceAsString());
        }
    }


    @Test
    public void test27() 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));

        //只是会查询差不多的,满足其中一个就会查询出出来
        SimpleQueryStringBuilder builder = QueryBuilders.simpleQueryStringQuery("+duanlian -hejiu");
        SearchResponse response = client.prepareSearch("lib3")
                .setQuery(builder)
                .get();
        SearchHits hits = response.getHits();
        for (SearchHit hit : hits){
            System.out.println(hit.getSourceAsString());
        }
    }

 

你可能感兴趣的:(ElasticSearch)