Executing a search with aggregations

The next step after searching in Elasticsearch is to execute the aggregations. The elastic4s DSL also provides support for aggregation so it can be built in a safer typed way.

Getting ready

You need an up-and-running Elasticsearch installation, as we described in the Downloading and installing Elasticsearch recipe in Chapter 2, Downloading and Setup.

A Maven tool or an IDE that supports Scala programming, such as Eclipse (ScalaIDE) or IntelliJ IDEA, with the Scala plugin should be installed.

The code for this recipe can be found in the chapter_15/elastic4s_sample file and the referred class is AggregationExample.

How to do it...

For executing a search with aggregations, we will perform the following steps:

  1. We need to import the classes needed for the aggregations:
            import com.sksamuel.elastic4s.ElasticDsl._ 
            import 
             org.elasticsearch.search.aggregations.metrics.geocentroid
             .InternalGeoCentroid 
            import org.elasticsearch.search.aggregations.metrics.stats
            .extended.InternalExtendedStats 
            import scala.collection.JavaConversions._ 
    
  2. We create an index and populate it with some data that will be used for the aggregations:
            object AggregationExample extends App with   
            ElasticSearchClientTrait { 
              val indexName = "myindex" 
              val typeName = "mytype" 
              ensureIndexMapping(indexName, typeName) 
              populateSampleData(indexName, typeName, 1000) 
    
  3. We know how to execute a search with aggregation using terms Aggregation with several sub-aggregations (extended statistics, geocentroid):
            val resp = client.execute { 
              search(indexName / typeName) size 0 aggregations ( 
                termsAggregation("tag") size 100 subAggregations( 
                  extendedStatsAggregation("price") field "price", 
                  extendedStatsAggregation("size") field "size", 
                  geoBoundsAggregation("centroid") field "location" 
                )) 
            }.await 
    
  4. Now we can process the response. We extract the aggregation results and show some values:
            val tagsAgg = resp.aggregations.stringTermsResult("tag") 
            println(s"Result Hits: ${resp.size}") 
            println(s"number of tags: ${tagsAgg.getBuckets.size()}") 
            println(s"max price of first tag   
            ${tagsAgg.getBuckets.head.getKey}:  
            ${tagsAgg.getBuckets.head.getAggregations.get
            [InternalExtendedStats]("price").value("max")}") 
            println(s"min size of first tag 
            ${tagsAgg.getBuckets.head.getKey}:     
            ${tagsAgg.getBuckets.head.getAggregations.get
            [InternalExtendedStats]("size").value("min")}") 
    
  5. At the end, we clean up the used resources:
            client.execute(deleteIndex(indexName)).await 
            client.close() 
            } 
    
  6. The result should be like this one:
            number of tags: 5 
            max price of first tag awesome: 10.799999999999999 
            min size of first tag awesome: 0.0 
    

How it works...

Elastic4s provides a powerful DSL for more type-safe aggregations.

In the preceding example, we  used termsAggregation to initially aggregate the buckets by tag settings to collect at least 100 buckets (termsAggregation("tag") size 100), then we have two types of sub-aggregations:

  • extendedStatsAggregation: This is used to collect extended statistics on the price and size fields
  • geocentroidAggregation: This is used to compute the center of documents results

The elastic4s DSL provides all the official Elasticsearch aggregations.

Also the aggregation result contains helpers for managing aggregations, such as automatic casing for some types:

  • stringTermsResult: This wraps a string terms Aggregation result
  • termsResult: This wraps a generic terms Aggregation result
  • missingResult: This wraps a missing aggregation result
  • cardinalityResult: This wraps a cardinality aggregation result
  • avgResult: This wraps an average metric aggregation result
  • maxResult: This wraps a max metric aggregation result
  • sumResult: This wraps a sum metric aggregation result
  • minResult: This wraps a min metric aggregation result
  • histogramResult: This wraps a histogram aggregation result
  • valueCountResult: This wraps a count aggregation result

If the aggregation result is not part of these aggregations results, an helper method, get[T]:T, allows you to retrieve a casted aggregation result.

See also

  • The Executing term Aggregations recipe in Chapter 8, Aggregations, which describes term Aggregation
  • The Executing statistical aggregations recipe in Chapter 8, Aggregations, for more detail about statistical aggregations
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.51.191