Navin Gelot
Navin Gelot

Reputation: 1334

value withFilter not a member of java.util.list

for {
  bucket: Terms.Bucket <- topLevelBuckets
  aggResult = AggResult(bucket.getKeyAsString, bucket.getDocCount,
    bucket.getAggregations.get("total_usage").asInstanceOf[Sum].getValue)
} yield aggResult

}

case class AggResult(bucketKey: String, bucketCount: Long, bucketValue: Double)

//here i want to return an List of AggResult but it's showing me above error

/* Error:(113, 35) value withFilter is not a member of
java.util.List[org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket]
for {
  bucket: Terms.Bucket <-  topLevelBuckets
*/

/* this works fine but returning an array */

def extractAggResults_(client: TcpClient): Array[AggResult] = {

    val nestingAggregation = client.execute(aggQuery).await

    val topLevelBuckets = nestingAggregation.aggregations.termsResult("by_users").getBuckets
    topLevelBuckets.toArray() map(bucket =>  AggResult(bucket.asInstanceOf[Terms.Bucket].getKeyAsString, bucket.asInstanceOf[Terms.Bucket].getDocCount, bucket.asInstanceOf[Terms.Bucket].getAggregations.get("total_usage").asInstanceOf[Sum].getValue))
  }

Upvotes: 1

Views: 2100

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170745

Note that JavaConversions (linked in the comment) is deprecated.

Use

import scala.collection.JavaConverters._

for {
  bucket <- topLevelBuckets.asScala
} yield AggResult(bucket.getKeyAsString, bucket.getDocCount,
    bucket.getAggregations.get("total_usage").asInstanceOf[Sum].getValue)

If you need a Java List to pass to other methods, call .asJava on the result.

asJava and asScala are cheap operations; they just create simple adapter objects and don't copy all elements, as converting to Scala List would require.

Upvotes: 1

Related Questions