(22) ElasticSearch java project aggregation operators seeking maximum, minimum, average, sum and find different values

  1, selecting the maximum value of the age field index lib3

@Test
     public  void testAggMax () throws IOException, InterruptedException, ExecutionException {
         // specify the cluster 
        Settings Settings = Settings.builder () PUT ( "cluster.name", "My-the Application." ) .Build (); 
         // create a client end 
        TransportClient Client = new new PreBuiltTransportClient (Settings) 
                                .addTransportAddress ( new new TransportAddress (InetAddress.getByName ( "192.168.43.151"), 9300 ));
         // aggMax is fixed wording, seeking maximum age 
        AggregationBuilder agg = AggregationBuilders.max ( "aggMax" ) .field ( "Age" );
        //Execute the query 
        SearchResponse the Response = client.prepareSearch ( "lib3") // index is lib 
                                  .addAggregation (AGG) 
                                  .get (); 
        // get the results 
        Max max = response.getAggregations () GET ( "aggMax." );
         // output 
        System.out.println (max.getValue ()); 
        client.close (); 
   }

  2, minimum age field in the index lib3

@Test
     public  void testAggMin () throws IOException, InterruptedException, ExecutionException {
         // specify the cluster 
        Settings Settings = Settings.builder () PUT ( "cluster.name", "My-the Application." ) .Build (); 
         // create a client end 
        TransportClient Client = new new PreBuiltTransportClient (Settings) 
                                .addTransportAddress ( new new TransportAddress (InetAddress.getByName ( "192.168.43.151"), 9300 ));
         // aggMin is fixed wording, for the minimum Age 
        AggregationBuilder agg = AggregationBuilders.min ( "aggMin" ) .field ( "Age" );
        //Execute the query 
        SearchResponse the Response = client.prepareSearch ( "lib3") // index is lib 
                                  .addAggregation (AGG) 
                                  .get (); 
        // get the result 
        Min min = response.getAggregations () GET ( "aggMin." );
         // output 
        System.out.println (min.getValue ()); 
        client.close (); 
   }

  3, an average age field in the index lib3

@Test
     public  void testAggAvg () throws IOException, InterruptedException, ExecutionException {
         // specify the cluster 
        Settings Settings = Settings.builder () PUT ( "cluster.name", "My-the Application." ) .Build (); 
         // create a client end 
        TransportClient Client = new new PreBuiltTransportClient (Settings) 
                                .addTransportAddress ( new new TransportAddress (InetAddress.getByName ( "192.168.43.151"), 9300 ));
         // aggAvg is fixed wording, find the average age 
        AggregationBuilder agg = AggregationBuilders.avg ( "aggAvg" ) .field ( "Age" );
        //Execute the query 
        SearchResponse the Response = client.prepareSearch ( "lib3") // index is lib 
                                  .addAggregation (AGG) 
                                  .get (); 
        // get the result 
        Avg AVG = response.getAggregations () GET ( "aggAvg." );
         // output 
        System.out.println (avg.getValue ()); 
        client.close (); 
   }

  4, at age field seeking index and lib3

@Test
     public  void testAggSum () throws IOException, InterruptedException, ExecutionException {
         // specify the cluster 
        Settings Settings = Settings.builder () PUT ( "cluster.name", "My-the Application." ) .Build (); 
         // create a client end 
        TransportClient Client = new new PreBuiltTransportClient (Settings) 
                                .addTransportAddress ( new new TransportAddress (InetAddress.getByName ( "192.168.43.151"), 9300 ));
         // aggSum wording is fixed, for obtaining the sum of age 
        AggregationBuilder agg = AggregationBuilders.sum ( "aggSum" ) .field ( "Age" );
        //Execute the query 
        SearchResponse the Response = client.prepareSearch ( "lib3") // index is lib 
                                  .addAggregation (AGG) 
                                  .get (); 
        // get the result 
        Sum response.getAggregations = SUM () GET ( "aggSum." );
         // output 
        System.out.println (sum.getValue ()); 
        client.close (); 
   }

  5, seeking index lib3 age field in a plurality of different values

@Test
     public  void testAggCardinality () throws IOException, InterruptedException, ExecutionException {
         // specify the cluster 
        Settings Settings = Settings.builder () PUT ( "cluster.name", "My-the Application." ) .Build (); 
         // create a client end 
        TransportClient Client = new new PreBuiltTransportClient (Settings) 
                                .addTransportAddress ( new new TransportAddress (InetAddress.getByName ( "192.168.43.151"), 9300 ));
         // aggCardinality is fixed wording, seeking a field with a plurality of different values 
        AggregationBuilder agg = AggregationBuilders. cardinality ( "aggCardinality"). field ( "age");
         // execute the query 
        SearchResponse the Response = client.prepareSearch ( "lib3") // index is lib 
                                  .addAggregation (AGG) 
                                  .get (); 
        // get the result 
        Cardinality cardinality = response.getAggregations () get ( "aggCardinality". );
         // output 
        System.out.println (cardinality.getValue ()); 
        client.close (); 
   }

 

Guess you like

Origin www.cnblogs.com/javasl/p/12070429.html