Skip to content

Commit

Permalink
update bool query for filtered aggregate
Browse files Browse the repository at this point in the history
  • Loading branch information
fupelaqu committed Jun 22, 2024
1 parent d5b16ea commit fb3c6d0
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,16 @@ object ElasticQuery {
def _filtered: Aggregation = {
aggregation.filter match {
case Some(f) =>
val boolQuery = Option(ElasticBoolQuery(group = true))
val filteredAgg = s"filtered_agg"
aggPath ++= Seq(filteredAgg)
filterAgg(
filteredAgg,
f.criteria
.map(_.asFilter(None).query(Set(identifier.innerHitsName).flatten, None))
.map(
_.asFilter(boolQuery)
.query(Set(identifier.innerHitsName).flatten, boolQuery)
)
.getOrElse(matchAllQuery())
) subaggs {
aggPath ++= Seq(agg)
Expand Down
27 changes: 11 additions & 16 deletions sql/src/main/scala/app/softnetwork/elastic/sql/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -681,22 +681,17 @@ package object sql {
innerHitsNames: Set[String] = Set.empty,
currentQuery: Option[ElasticBoolQuery]
): Query = {
criteria match {
case c: ElasticNested =>
c.query(innerHitsNames, currentQuery)
case _ =>
if (innerHitsNames.contains(innerHitsName.getOrElse(""))) {
criteria.asFilter(currentQuery).query(innerHitsNames, currentQuery)
} else {
val boolQuery = Option(ElasticBoolQuery(group = true))
val filteredQuery = criteria.asFilter(boolQuery)
nestedQuery(
relationType.getOrElse(""),
filteredQuery
.query(innerHitsNames + innerHitsName.getOrElse(""), boolQuery)
) /*.scoreMode(ScoreMode.None)*/
.inner(innerHits(innerHitsName.getOrElse("")))
}
if (innerHitsNames.contains(innerHitsName.getOrElse(""))) {
criteria.asFilter(currentQuery).query(innerHitsNames, currentQuery)
} else {
val boolQuery = Option(ElasticBoolQuery(group = true))
nestedQuery(
relationType.getOrElse(""),
criteria
.asFilter(boolQuery)
.query(innerHitsNames + innerHitsName.getOrElse(""), boolQuery)
) /*.scoreMode(ScoreMode.None)*/
.inner(innerHits(innerHitsName.getOrElse("")))
}
}
}
Expand Down
194 changes: 189 additions & 5 deletions sql/src/test/scala/app/softnetwork/elastic/sql/ElasticQuerySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ class ElasticQuerySpec extends AnyFlatSpec with Matchers {
select.isDefined shouldBe true
val result = select.get
result.query shouldBe
"""
"""
|{
| "query":{
| "bool":{
Expand All @@ -502,7 +502,7 @@ class ElasticQuerySpec extends AnyFlatSpec with Matchers {
|}""".stripMargin.replaceAll("\\s+", "")
}

it should "handle complex query" in {
it should "perform complex query" in {
val select = ElasticQuery.select(
SQLQuery(
s"""SELECT
Expand All @@ -511,7 +511,7 @@ class ElasticQuerySpec extends AnyFlatSpec with Matchers {
| stores-dev store,
| UNNEST(store.products) as inner_products
|WHERE
| (
| ((
| firstName is not null AND
| lastName is not null AND
| description is not null AND
Expand All @@ -529,7 +529,7 @@ class ElasticQuerySpec extends AnyFlatSpec with Matchers {
| inner_products.deleted=false AND
| inner_products.upForSale=true AND
| inner_products.stock > 0
| ) AND
| )) AND
| (
| match(products.name, "lasagnes") OR
| match(products.description, "lasagnes") OR
Expand All @@ -540,7 +540,191 @@ class ElasticQuerySpec extends AnyFlatSpec with Matchers {
)
select.isDefined shouldBe true
val result = select.get
println(result.query)
// println(result.query)
result.query shouldBe
"""
|{
| "query":{
| "bool":{
| "filter":[
| {
| "bool":{
| "filter":[
| {
| "bool":{
| "filter":[
| {
| "bool":{
| "filter":[
| {
| "bool":{
| "filter":[
| {
| "bool":{
| "filter":[
| {
| "exists":{
| "field":"firstName"
| }
| },
| {
| "exists":{
| "field":"lastName"
| }
| },{
| "exists":{
| "field":"description"
| }
| },{
| "range":{
| "preparationTime":{
| "lte":"120"
| }
| }
| }
| ]
| }
| },
| {
| "term":{
| "deliveryPeriods.dayOfWeek":{
| "value":"6"
| }
| }
| }
| ]
| }
| },
| {
| "bool":{
| "should":[
| {
| "geo_distance":{
| "distance":"7000m",
| "pickup.location":[0.0,0.0]
| }
| },
| {
| "geo_distance":{
| "distance":"7000m",
| "withdrawals.location":[0.0,0.0]
| }
| }
| ]
| }
| }
| ]
| }
| },
| {
| "bool":{
| "must_not":[
| {
| "term":{
| "receiptOfOrdersDisabled":{
| "value":true
| }
| }
| }
| ],
| "filter":[
| {
| "bool":{
| "must_not":[
| {
| "regexp":{
| "blockedCustomers":{
| "value":"()uuid()"
| }
| }
| }
| ]
| }
| }
| ]
| }
| }
| ]
| }
| },
| {
| "nested":{
| "path":"products",
| "query":{
| "bool":{
| "filter":[
| {
| "term":{
| "products.deleted":{
| "value":false
| }
| }
| },
| {
| "term":{
| "products.upForSale":{
| "value":true
| }
| }
| },
| {
| "range":{
| "products.stock":{
| "gt":"0"
| }
| }
| }
| ]
| }
| },
| "inner_hits":{
| "name":"inner_products"
| }
| }
| }
| ]
| }
| },
| {
| "bool":{
| "should":[
| {
| "match":{
| "products.name":{
| "query":"lasagnes"
| }
| }
| },
| {
| "match":{
| "products.description":{
| "query":"lasagnes"
| }
| }
| },
| {
| "match":{
| "products.ingredients":{
| "query":"lasagnes"
| }
| }
| }
| ]
| }
| }
| ]
| }
| },
| "from":0,
| "size":100,
| "_source":{
| "includes":[
| "inner_products.name",
| "inner_products.category",
| "inner_products.price"
| ]
| }
|}""".stripMargin.replaceAll("\\s+", "")
}

}

0 comments on commit fb3c6d0

Please sign in to comment.