1, grouped by age, number of queries each group
@Test public void testTerms() throws IOException, InterruptedException, ExecutionException { //指定集群 Settings settings = Settings.builder().put("cluster.name","my-application").build(); //创建客户端 TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress ( new new TransportAddress (InetAddress.getByName ( "192.168.43.151"), 9300 )); // create a query object, grouped by age, the number of queries each group AggregationBuilder agg = AggregationBuilders.terms ( "terms" ) field. ( "Age" ); // execute the query SearchResponse SR = client.prepareSearch ( "lib3" ) .addAggregation (agg) .execute() .actionGet(); //获取结果 Terms terms = sr.getAggregations().get("terms"); //遍历 for(Terms.Bucket entry:terms.getBuckets()) { System.out.println(entry.getKey()+"----------"+entry.getDocCount()); } client.close(); }
2, filter query field is the age of 20 documents
@Test public void testFilter() throws IOException, InterruptedException, ExecutionException { //指定集群 Settings settings = Settings.builder().put("cluster.name","my-application").build(); //创建客户端 TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress ( new new TransportAddress (InetAddress.getByName ( "192.168.43.151"), 9300 )); // the specified query conditions, age field 20 of the QueryBuilder QueryBuilders.termQuery = Query ( "Age", 29 ); // filtering query conditions, filtering out documents that satisfy the query AggregationBuilder AGG = AggregationBuilders.filter ( "filter" , query); // execute the query SearchResponse SR = client.prepareSearch ( "lib3" ) .addAggregation (agg) .execute() .actionGet(); // Get a result the Filter filter = sr.getAggregations () GET ( "filter." ); // number of output System.out.println (filter.getDocCount ()); client.close(); }
3, filter query interests field contains changge, hejiu documents
@Test public void testFilters() throws IOException, InterruptedException, ExecutionException { //指定集群 Settings settings = Settings.builder().put("cluster.name","my-application").build(); //创建客户端 TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress ( new new TransportAddress (InetAddress.getByName ( "192.168.43.151"), 9300 )); // the specified query conditions, interests of Changge field contains the QueryBuilder QueryBuilders.termQuery = Query ( "Interests", "Changge" ); // specify the query criteria, the hejiu Interests field contains the QueryBuilder query2 = QueryBuilders.termQuery ( "Interests", "hejiu" ); // filter query, query or a document satisfying filtered of query2 AggregationBuilder AGG = AggregationBuilders.filters ( "filters" , new new FiltersAggregator.KeyedFilter ( "condition 1", Query), new new FiltersAggregator.KeyedFilter ( "condition 2" , query2)); // execute the query SearchResponse SR = client.prepareSearch ( "lib3" ) .addAggregation (agg) .execute() .actionGet(); // Get a result the Filters Filters = sr.getAggregations () GET ( "Filters." ); // traversal, the two specified filter, the output data of two, key are Conditions 1 and 2 for (entry Filters.Bucket : filters.getBuckets ()) { System.out.println(entry.getKey()+"----------"+entry.getDocCount()); } client.close(); }
4, the statistics were younger than 50, between 25 and 50, larger than the number of documents 25
@Test public void testRange() throws IOException, InterruptedException, ExecutionException { //指定集群 Settings settings = Settings.builder().put("cluster.name","my-application").build(); //创建客户端 TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress ( new new TransportAddress (InetAddress.getByName ( "192.168.43.151"), 9300 )); // statistical data set 3 is younger than 50, between 25 and 50, greater than 25 AggregationBuilder agg = AggregationBuilders.range ( " the Range " ) .field("age") .addUnboundedTo(50)//( ,to) .addRange(25,50)//[from,to) .addUnboundedFrom(25);//[from, ) //执行查询 SearchResponse sr = client.prepareSearch("lib3") .addAggregation (agg) .execute() .actionGet(); // Get a result the Range = R & lt sr.getAggregations () GET ( "Range." ); // iterate outputs three Key, * - 50.0; 25.0-50.0; 25.0- * for (Range.Bucket entry: r.getBuckets ()) { System.out.println(entry.getKey()+"----------"+entry.getDocCount()); } client.close(); }
5, the number of missing statistics document the age field
@Test public void testMissing() throws IOException, InterruptedException, ExecutionException { //指定集群 Settings settings = Settings.builder().put("cluster.name","my-application").build(); //创建客户端 TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress ( new new TransportAddress (InetAddress.getByName ( "192.168.43.151"), 9300 )); // Statistics missing fields age of the document . AggregationBuilder AGG = AggregationBuilders.missing ( "Missing") Field, ( "age" ); // execute the query SearchResponse SR = client.prepareSearch ( "lib3" ) .addAggregation (agg) .execute() .actionGet(); // Get Results Aggregation sr.getAggregations aggregation = () GET ( "Missing." ); // output System.out.println (aggregation.toString ()); client.close(); }