Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ThisBuild / organization := "app.softnetwork"

name := "softclient4es"

ThisBuild / version := "0.3.0"
ThisBuild / version := "0.4.0"

ThisBuild / scalaVersion := scala213

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ package app.softnetwork.elastic.sql.bridge

import app.softnetwork.elastic.sql.{
AggregateFunction,
Asc,
Avg,
BucketSelectorScript,
Count,
ElasticBoolQuery,
Max,
Min,
SQLBucket,
SQLCriteria,
SQLField,
SortOrder,
Sum
}
import com.sksamuel.elastic4s.ElasticApi.{
avgAgg,
bucketSelectorAggregation,
cardinalityAgg,
filterAgg,
maxAgg,
Expand All @@ -23,11 +27,13 @@ import com.sksamuel.elastic4s.ElasticApi.{
termsAgg,
valueCountAgg
}
import com.sksamuel.elastic4s.script.Script
import com.sksamuel.elastic4s.searches.aggs.{
Aggregation,
FilterAggregation,
NestedAggregation,
TermsAggregation
TermsAggregation,
TermsOrder
}

import scala.language.implicitConversions
Expand All @@ -42,17 +48,24 @@ case class ElasticAggregation(
nestedAgg: Option[NestedAggregation] = None,
filteredAgg: Option[FilterAggregation] = None,
aggType: AggregateFunction,
agg: Aggregation
agg: Aggregation,
direction: Option[SortOrder] = None
) {
val nested: Boolean = nestedAgg.nonEmpty
val filtered: Boolean = filteredAgg.nonEmpty
}

object ElasticAggregation {
def apply(sqlAgg: SQLField, filter: Option[SQLCriteria]): ElasticAggregation = {
def apply(
sqlAgg: SQLField,
having: Option[SQLCriteria],
bucketsDirection: Map[String, SortOrder]
): ElasticAggregation = {
import sqlAgg._
val sourceField = identifier.name

val direction = bucketsDirection.get(identifier.identifierName)

val field = fieldAlias match {
case Some(alias) => alias.alias
case _ => sourceField
Expand Down Expand Up @@ -92,7 +105,7 @@ object ElasticAggregation {
val filteredAggName = "filtered_agg"

val filteredAgg: Option[FilterAggregation] =
filter match {
having match {
case Some(f) =>
val boolQuery = Option(ElasticBoolQuery(group = true))
Some(
Expand Down Expand Up @@ -135,44 +148,60 @@ object ElasticAggregation {
nestedAgg = nestedAgg,
filteredAgg = filteredAgg,
aggType = aggType,
agg = _agg
agg = _agg,
direction = direction
)
}

/*
def apply(
def buildBuckets(
buckets: Seq[SQLBucket],
bucketsDirection: Map[String, SortOrder],
aggregations: Seq[Aggregation],
current: Option[TermsAggregation]
aggregationsDirection: Map[String, SortOrder],
having: Option[SQLCriteria]
): Option[TermsAggregation] = {
buckets match {
case Nil =>
current.map(_.copy(subaggs = aggregations))
case bucket +: tail =>
val agg = termsAgg(bucket.name, s"${bucket.identifier.name}.keyword")
current match {
case Some(a) =>
apply(tail, aggregations, Some(agg)) match {
case Some(subAgg) =>
Some(a.copy(subaggs = a.subaggs :+ subAgg))
case _ => Some(a)
}
Console.println(bucketsDirection)
buckets.reverse.foldLeft(Option.empty[TermsAggregation]) { (current, bucket) =>
val agg = {
bucketsDirection.get(bucket.identifier.identifierName) match {
case Some(direction) =>
termsAgg(bucket.name, s"${bucket.identifier.name}.keyword")
.order(Seq(direction match {
case Asc => TermsOrder(bucket.name, asc = true)
case _ => TermsOrder(bucket.name, asc = false)
}))
case None =>
apply(tail, aggregations, Some(agg))
termsAgg(bucket.name, s"${bucket.identifier.name}.keyword")
}
}
}
*/

def buildBuckets(
buckets: Seq[SQLBucket],
aggregations: Seq[Aggregation]
): Option[TermsAggregation] = {
buckets.reverse.foldLeft(Option.empty[TermsAggregation]) { (current, bucket) =>
val agg = termsAgg(bucket.name, s"${bucket.identifier.name}.keyword")
}
current match {
case Some(subAgg) => Some(agg.copy(subaggs = Seq(subAgg)))
case None => Some(agg.copy(subaggs = aggregations))
case None =>
val aggregationsWithOrder: Seq[TermsOrder] = aggregationsDirection.toSeq.map { kv =>
kv._2 match {
case Asc => TermsOrder(kv._1, asc = true)
case _ => TermsOrder(kv._1, asc = false)
}
}
val withAggregationOrders =
if (aggregationsWithOrder.nonEmpty)
agg.order(aggregationsWithOrder)
else
agg
val withHaving = having match {
case Some(criteria) =>
import BucketSelectorScript._
val script = toPainless(criteria)
val bucketsPath = extractBucketsPath(criteria)

val bucketSelector =
bucketSelectorAggregation("having_filter", Script(script), bucketsPath)

withAggregationOrders.copy(subaggs = aggregations :+ bucketSelector)

case None => withAggregationOrders.copy(subaggs = aggregations)
}
Some(withHaving)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ case class ElasticQuery(filter: ElasticFilter) {
case isNull: SQLIsNull => isNull
case isNotNull: SQLIsNotNull => isNotNull
case in: SQLIn[_, _] => in
case between: SQLBetween => between
case between: SQLBetween[String] => between
case between: SQLBetween[Long] => between
case between: SQLBetween[Double] => between
case geoDistance: ElasticGeoDistance => geoDistance
case matchExpression: ElasticMatch => matchExpression
case other =>
Expand Down
Loading