Skip to content

Commit 1b9e8f7

Browse files
committed
fix not operator and regexp value
1 parent 1755af1 commit 1b9e8f7

File tree

5 files changed

+87
-126
lines changed

5 files changed

+87
-126
lines changed

es6/sql-bridge/src/test/scala/app/softnetwork/elastic/sql/SQLCriteriaSpec.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class SQLCriteriaSpec extends AnyFlatSpec with Matchers {
147147
|"query":{
148148
| "bool":{"filter":[{"regexp" : {
149149
| "identifier" : {
150-
| "value" : ".*?un.*?"
150+
| "value" : ".*un.*"
151151
| }
152152
| }
153153
| }
@@ -161,7 +161,7 @@ class SQLCriteriaSpec extends AnyFlatSpec with Matchers {
161161
| "filter":[{"bool":{"must_not": [{
162162
| "regexp": {
163163
| "identifier": {
164-
| "value": ".*?un.*?"
164+
| "value": ".*un.*"
165165
| }
166166
| }
167167
| }]

es6/sql-bridge/src/test/scala/app/softnetwork/elastic/sql/SQLQuerySpec.scala

Lines changed: 83 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -558,35 +558,6 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
558558
|{
559559
| "query": {
560560
| "bool": {
561-
| "must": [
562-
| {
563-
| "match": {
564-
| "products.name": {
565-
| "query": "lasagnes"
566-
| }
567-
| }
568-
| },
569-
| {
570-
| "bool": {
571-
| "should": [
572-
| {
573-
| "match": {
574-
| "products.description": {
575-
| "query": "lasagnes"
576-
| }
577-
| }
578-
| },
579-
| {
580-
| "match": {
581-
| "products.ingredients": {
582-
| "query": "lasagnes"
583-
| }
584-
| }
585-
| }
586-
| ]
587-
| }
588-
| }
589-
| ],
590561
| "filter": [
591562
| {
592563
| "bool": {
@@ -626,7 +597,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
626597
| {
627598
| "regexp": {
628599
| "blockedCustomers": {
629-
| "value": ".*?uuid.*?"
600+
| "value": ".*uuid.*"
630601
| }
631602
| }
632603
| }
@@ -669,52 +640,14 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
669640
| }
670641
| ]
671642
| }
672-
| },
673-
| {
674-
| "nested": {
675-
| "path": "products",
676-
| "query": {
677-
| "bool": {
678-
| "filter": [
679-
| {
680-
| "term": {
681-
| "products.deleted": {
682-
| "value": false
683-
| }
684-
| }
685-
| },
686-
| {
687-
| "term": {
688-
| "products.upForSale": {
689-
| "value": true
690-
| }
691-
| }
692-
| },
693-
| {
694-
| "range": {
695-
| "products.stock": {
696-
| "gt": "0"
697-
| }
698-
| }
699-
| }
700-
| ]
701-
| }
702-
| },
703-
| "inner_hits": {
704-
| "name": "inner_products",
705-
| "from": 0,
706-
| "size": 10
707-
| }
708-
| }
709643
| }
710644
| ]
711645
| }
712646
| }
713647
| ]
714648
| }
715649
| },
716-
| "from": 0,
717-
| "size": 100,
650+
| "size": 0,
718651
| "min_score": 1.0,
719652
| "sort": [
720653
| {
@@ -728,27 +661,93 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
728661
| }
729662
| }
730663
| ],
731-
| "_source": {
732-
| "includes": [
733-
| "inner_products.name",
734-
| "inner_products.category",
735-
| "inner_products.price"
736-
| ]
737-
| },
664+
| "_source": true,
738665
| "aggs": {
739666
| "nested_products": {
740667
| "nested": {
741668
| "path": "products"
742669
| },
743670
| "aggs": {
744-
| "min_price": {
745-
| "min": {
746-
| "field": "products.price"
747-
| }
748-
| },
749-
| "max_price": {
750-
| "max": {
751-
| "field": "products.price"
671+
| "filtered_agg": {
672+
| "filter": {
673+
| "bool": {
674+
| "filter": [
675+
| {
676+
| "term": {
677+
| "products.deleted": {
678+
| "value": false
679+
| }
680+
| }
681+
| },
682+
| {
683+
| "term": {
684+
| "products.upForSale": {
685+
| "value": true
686+
| }
687+
| }
688+
| },
689+
| {
690+
| "range": {
691+
| "products.stock": {
692+
| "gt": "0"
693+
| }
694+
| }
695+
| },
696+
| {
697+
| "match": {
698+
| "products.name": {
699+
| "query": "lasagnes"
700+
| }
701+
| }
702+
| },
703+
| {
704+
| "bool": {
705+
| "should": [
706+
| {
707+
| "match": {
708+
| "products.description": {
709+
| "query": "lasagnes"
710+
| }
711+
| }
712+
| },
713+
| {
714+
| "match": {
715+
| "products.ingredients": {
716+
| "query": "lasagnes"
717+
| }
718+
| }
719+
| }
720+
| ]
721+
| }
722+
| }
723+
| ]
724+
| }
725+
| },
726+
| "aggs": {
727+
| "category": {
728+
| "terms": {
729+
| "field": "products.category.keyword"
730+
| },
731+
| "aggs": {
732+
| "productName": {
733+
| "terms": {
734+
| "field": "products.name.keyword"
735+
| },
736+
| "aggs": {
737+
| "min_price": {
738+
| "min": {
739+
| "field": "products.price"
740+
| }
741+
| },
742+
| "max_price": {
743+
| "max": {
744+
| "field": "products.price"
745+
| }
746+
| }
747+
| }
748+
| }
749+
| }
750+
| }
752751
| }
753752
| }
754753
| }

sql/src/main/scala/app/softnetwork/elastic/sql/SQLWhere.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ case class SQLExpression(
159159
) extends SQLCriteriaWithIdentifier
160160
with ElasticFilter {
161161
override def sql =
162-
s"${maybeNot.map(_ => "not ").getOrElse("")}$identifier $operator $value"
162+
s"$identifier ${maybeNot.map(_ => "not ").getOrElse("")}$operator $value"
163163
override def update(request: SQLSearchRequest): SQLCriteria = {
164164
val updated = this.copy(identifier = identifier.update(request))
165165
if (updated.nested) {

sql/src/main/scala/app/softnetwork/elastic/sql/package.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ package object sql {
187187
value.substring(0, value.length - 1)
188188
else
189189
value
190-
s"""${if (startWith) ".*?"}$v${if (endWith) ".*?"}"""
190+
s"""${if (startWith) ".*"}$v${if (endWith) ".*"}"""
191191
}
192192

193193
case object Alias extends SQLExpr("as") with SQLRegex

sql/src/test/scala/app/softnetwork/elastic/sql/SQLParserSpec.scala

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -79,40 +79,6 @@ object Queries {
7979
|GROUP BY Country,City
8080
|HAVING Country <> "USA" AND City <> "Berlin" AND COUNT(CustomerID) > 1
8181
|ORDER BY COUNT(CustomerID) DESC,Country asc""".stripMargin.replaceAll("\n", " ").toLowerCase
82-
83-
val complexQuery: String =
84-
"""SELECT
85-
| inner_products.category as category,
86-
|inner_products.name as productName,
87-
|min(inner_products.price) as min_price,
88-
|max(inner_products.price) as max_price
89-
| FROM
90-
| stores as store,
91-
|UNNEST(store.products LIMIT 10) as inner_products
92-
| WHERE
93-
| (
94-
|firstName is not null AND
95-
| lastName is not null AND
96-
| description is not null AND
97-
| preparationTime <= 120 AND
98-
| store.deliveryPeriods.dayOfWeek = 6 AND
99-
| not blockedCustomers like "%uuid%" AND
100-
| NOT receiptOfOrdersDisabled = true AND
101-
| (
102-
|distance(pickup.location,(0.0,0.0)) <= "7000m" OR
103-
| distance(withdrawals.location,(0.0,0.0)) <= "7000m"
104-
|)
105-
|)
106-
| GROUP BY
107-
| inner_products.category,
108-
|inner_products.name
109-
| HAVING inner_products.deleted = false AND
110-
| inner_products.upForSale = true AND
111-
| inner_products.stock > 0 AND
112-
| match (inner_products.name) against ("lasagnes") AND
113-
| match (inner_products.description, inner_products.ingredients) against ("lasagnes")
114-
| ORDER BY preparationTime ASC, nbOrders DESC
115-
| LIMIT 100""".stripMargin.replaceAll("\n", "").toLowerCase
11682
}
11783

11884
/** Created by smanciot on 15/02/17.
@@ -377,8 +343,4 @@ class SQLParserSpec extends AnyFlatSpec with Matchers {
377343
result.toOption.flatMap(_.left.toOption.map(_.sql)).getOrElse("") should ===(groupByWithHaving)
378344
}
379345

380-
it should "parse complex query" in {
381-
val result = SQLParser(complexQuery)
382-
result.toOption.flatMap(_.left.toOption.map(_.sql)).getOrElse("") should ===(complexQuery)
383-
}
384346
}

0 commit comments

Comments
 (0)