@@ -282,18 +282,18 @@ object ElasticAggregation {
282282 nested : Option [NestedElement ],
283283 allElasticAggregations : Seq [ElasticAggregation ]
284284 ): Option [Aggregation ] = {
285- var first = false
286285 val nbBuckets = buckets.size
287286 buckets.reverse.foldLeft(Option .empty[Aggregation ]) { (current, bucket) =>
288287 // Determine the bucketPath of the current bucket
289288 val currentBucketPath = bucket.identifier.path
290289
291- val minDocCount =
292- if ((first || current.isEmpty) && nbBuckets > 1 ) {
293- 0
290+ val aggScript =
291+ if (bucket.shouldBeScripted) {
292+ val context = PainlessContext ()
293+ val painless = bucket.painless(Some (context))
294+ Some (Script (s " $context$painless" ).lang(" painless" ))
294295 } else {
295- first = true
296- 1
296+ None
297297 }
298298
299299 var agg : Aggregation = {
@@ -320,32 +320,74 @@ object ElasticAggregation {
320320 } else {
321321 None
322322 }
323- bucketsDirection.get(bucket.identifier.identifierName) match {
324- case Some (direction) =>
325- DateHistogramAggregation (bucket.name, calendarInterval = interval)
326- .field(currentBucketPath)
327- .minDocCount(minDocCount)
328- .order(direction match {
329- case Asc => HistogramOrder (" _key" , asc = true )
330- case _ => HistogramOrder (" _key" , asc = false )
331- })
323+
324+ aggScript match {
325+ case Some (script) =>
326+ // Scripted date histogram
327+ bucketsDirection.get(bucket.identifier.identifierName) match {
328+ case Some (direction) =>
329+ DateHistogramAggregation (bucket.name, calendarInterval = interval)
330+ .script(script)
331+ .minDocCount(1 )
332+ .order(direction match {
333+ case Asc => HistogramOrder (" _key" , asc = true )
334+ case _ => HistogramOrder (" _key" , asc = false )
335+ })
336+ case _ =>
337+ DateHistogramAggregation (bucket.name, calendarInterval = interval)
338+ .script(script)
339+ .minDocCount(1 )
340+ }
332341 case _ =>
333- DateHistogramAggregation (bucket.name, calendarInterval = interval)
334- .field(currentBucketPath)
335- .minDocCount(minDocCount)
342+ // Standard date histogram
343+ bucketsDirection.get(bucket.identifier.identifierName) match {
344+ case Some (direction) =>
345+ DateHistogramAggregation (bucket.name, calendarInterval = interval)
346+ .field(currentBucketPath)
347+ .minDocCount(1 )
348+ .order(direction match {
349+ case Asc => HistogramOrder (" _key" , asc = true )
350+ case _ => HistogramOrder (" _key" , asc = false )
351+ })
352+ case _ =>
353+ DateHistogramAggregation (bucket.name, calendarInterval = interval)
354+ .field(currentBucketPath)
355+ .minDocCount(1 )
356+ }
336357 }
358+
337359 case _ =>
338- bucketsDirection.get(bucket.identifier.identifierName) match {
339- case Some (direction) =>
340- termsAgg(bucket.name, currentBucketPath)
341- .minDocCount(minDocCount)
342- .order(Seq (direction match {
343- case Asc => TermsOrder (" _key" , asc = true )
344- case _ => TermsOrder (" _key" , asc = false )
345- }))
360+ aggScript match {
361+ case Some (script) =>
362+ // Scripted terms aggregation
363+ bucketsDirection.get(bucket.identifier.identifierName) match {
364+ case Some (direction) =>
365+ TermsAggregation (bucket.name)
366+ .script(script)
367+ .minDocCount(1 )
368+ .order(Seq (direction match {
369+ case Asc => TermsOrder (" _key" , asc = true )
370+ case _ => TermsOrder (" _key" , asc = false )
371+ }))
372+ case _ =>
373+ TermsAggregation (bucket.name)
374+ .script(script)
375+ .minDocCount(1 )
376+ }
346377 case _ =>
347- termsAgg(bucket.name, currentBucketPath)
348- .minDocCount(minDocCount)
378+ // Standard terms aggregation
379+ bucketsDirection.get(bucket.identifier.identifierName) match {
380+ case Some (direction) =>
381+ termsAgg(bucket.name, currentBucketPath)
382+ .minDocCount(1 )
383+ .order(Seq (direction match {
384+ case Asc => TermsOrder (" _key" , asc = true )
385+ case _ => TermsOrder (" _key" , asc = false )
386+ }))
387+ case _ =>
388+ termsAgg(bucket.name, currentBucketPath)
389+ .minDocCount(1 )
390+ }
349391 }
350392 }
351393 }
0 commit comments