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.
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
.
For executing a search with aggregations, we will perform the following steps:
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._
object AggregationExample extends App with ElasticSearchClientTrait { val indexName = "myindex" val typeName = "mytype" ensureIndexMapping(indexName, typeName) populateSampleData(indexName, typeName, 1000)
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
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")}")
client.execute(deleteIndex(indexName)).await client.close() }
number of tags: 5 max price of first tag awesome: 10.799999999999999 min size of first tag awesome: 0.0
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 fieldsgeocentroidAggregation
: This is used to compute the center of documents resultsThe 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 resulttermsResult
: This wraps a generic terms Aggregation resultmissingResult
: This wraps a missing aggregation resultcardinalityResult
: This wraps a cardinality aggregation resultavgResult
: This wraps an average metric aggregation resultmaxResult
: This wraps a max metric aggregation resultsumResult
: This wraps a sum metric aggregation resultminResult
: This wraps a min metric aggregation resulthistogramResult
: This wraps a histogram aggregation resultvalueCountResult
: This wraps a count aggregation resultIf the aggregation result is not part of these aggregations results, an helper method, get[T]:T
, allows you to retrieve a casted aggregation result.
3.147.51.191