Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restrict sort and order methods #193

Merged
merged 4 commits into from
May 2, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package zio.elasticsearch

import zio.Chunk
import zio.elasticsearch.aggregation._

object ElasticAggregation {
Expand All @@ -42,7 +43,7 @@ object ElasticAggregation {
* performed.
*/
final def termsAggregation(name: String, field: Field[_, String]): TermsAggregation =
Terms(name = name, field = field.toString, order = Set.empty, subAggregations = Nil, size = None)
Terms(name = name, field = field.toString, order = Chunk.empty, subAggregations = Nil, size = None)

/**
* Constructs an instance of [[zio.elasticsearch.aggregation.TermsAggregation]] using the specified parameters.
Expand All @@ -56,7 +57,7 @@ object ElasticAggregation {
* performed.
*/
final def termsAggregation(name: String, field: String): TermsAggregation =
Terms(name = name, field = field, order = Set.empty, subAggregations = Nil, size = None)
Terms(name = name, field = field, order = Chunk.empty, subAggregations = Nil, size = None)

/**
* Constructs a type-safe instance of [[zio.elasticsearch.aggregation.MaxAggregation]] using the specified parameters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package zio.elasticsearch

import zio.Chunk
import zio.elasticsearch.ElasticPrimitive.ElasticPrimitiveOps
import zio.elasticsearch.aggregation.ElasticAggregation
import zio.elasticsearch.highlights.Highlights
Expand Down Expand Up @@ -226,7 +227,7 @@ object ElasticRequest {
Search(
index = index,
query = query,
sortBy = Set.empty,
sortBy = Chunk.empty,
from = None,
highlights = None,
routing = None,
Expand Down Expand Up @@ -255,7 +256,7 @@ object ElasticRequest {
index = index,
query = query,
aggregation = aggregation,
sortBy = Set.empty,
sortBy = Chunk.empty,
from = None,
highlights = None,
routing = None,
Expand Down Expand Up @@ -564,7 +565,7 @@ object ElasticRequest {
private[elasticsearch] final case class Search(
index: IndexName,
query: ElasticQuery[_],
sortBy: Set[Sort],
sortBy: Chunk[Sort],
from: Option[Int],
highlights: Option[Highlights],
routing: Option[Routing],
Expand Down Expand Up @@ -600,8 +601,8 @@ object ElasticRequest {
def size(value: Int): SearchRequest =
self.copy(size = Some(value))

def sort(sorts: Sort*): SearchRequest =
self.copy(sortBy = sortBy ++ sorts.toSet)
def sort(sort: Sort, sorts: Sort*): SearchRequest =
self.copy(sortBy = sortBy ++ (sort :: sorts.toList))

def toJson: Json = {
val fromJson: Json = self.from.fold(Obj())(f => Obj("from" -> f.toJson))
Expand All @@ -613,7 +614,7 @@ object ElasticRequest {
val searchAfterJson: Json = searchAfter.fold(Obj())(sa => Obj("search_after" -> sa))

val sortJson: Json =
if (self.sortBy.nonEmpty) Obj("sort" -> Arr(self.sortBy.toList.map(_.paramsToJson): _*)) else Obj()
if (self.sortBy.nonEmpty) Obj("sort" -> Arr(self.sortBy.map(_.paramsToJson): _*)) else Obj()

fromJson merge sizeJson merge highlightsJson merge sortJson merge self.query.toJson merge searchAfterJson
}
Expand All @@ -634,7 +635,7 @@ object ElasticRequest {
index: IndexName,
query: ElasticQuery[_],
aggregation: ElasticAggregation,
sortBy: Set[Sort],
sortBy: Chunk[Sort],
from: Option[Int],
highlights: Option[Highlights],
routing: Option[Routing],
Expand All @@ -656,8 +657,8 @@ object ElasticRequest {
def searchAfter(value: Json): SearchAndAggregateRequest =
self.copy(searchAfter = Some(value))

def sort(sorts: Sort*): SearchAndAggregateRequest =
self.copy(sortBy = sortBy ++ sorts.toSet)
def sort(sort: Sort, sorts: Sort*): SearchAndAggregateRequest =
self.copy(sortBy = sortBy ++ (sort :: sorts.toList))

def toJson: Json = {
val fromJson: Json = self.from.fold(Obj())(f => Obj("from" -> f.toJson))
Expand All @@ -669,7 +670,7 @@ object ElasticRequest {
val searchAfterJson: Json = searchAfter.fold(Obj())(sa => Obj("search_after" -> sa))

val sortJson: Json =
if (self.sortBy.nonEmpty) Obj("sort" -> Arr(self.sortBy.toList.map(_.paramsToJson): _*)) else Obj()
if (self.sortBy.nonEmpty) Obj("sort" -> Arr(self.sortBy.map(_.paramsToJson): _*)) else Obj()

fromJson merge
sizeJson merge
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package zio.elasticsearch.aggregation

import zio.Chunk
import zio.elasticsearch.ElasticAggregation.multipleAggregations
import zio.elasticsearch.ElasticPrimitive.ElasticPrimitiveOps
import zio.elasticsearch.aggregation.options._
Expand Down Expand Up @@ -85,12 +86,12 @@ sealed trait TermsAggregation
private[elasticsearch] final case class Terms(
name: String,
field: String,
order: Set[AggregationOrder],
order: Chunk[AggregationOrder],
subAggregations: List[SingleElasticAggregation],
size: Option[Int]
) extends TermsAggregation { self =>
def orderBy(order: AggregationOrder, orders: AggregationOrder*): TermsAggregation =
self.copy(order = self.order + order ++ orders.toSet)
self.copy(order = self.order ++ (order :: orders.toList))

private[elasticsearch] def paramsToJson: Json =
Obj(name -> paramsToJsonHelper)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ private[elasticsearch] trait HasSort[R <: HasSort[R]] {
* Sets the sorting criteria for the [[zio.elasticsearch.ElasticRequest.SearchRequest]] or the
* [[zio.elasticsearch.ElasticRequest.SearchAndAggregateRequest]].
*
* @param sort
* required [[zio.elasticsearch.query.sort.Sort]] object that define the sorting criteria
* @param sorts
* one or more [[zio.elasticsearch.query.sort.Sort]] objects that define the sorting criteria
* rest of the [[zio.elasticsearch.query.sort.Sort]] objects that define the sorting criteria
* @return
* an instance of the [[zio.elasticsearch.ElasticRequest]] enriched with the sorting criteria.
*/
def sort(sorts: Sort*): R
def sort(sort: Sort, sorts: Sort*): R
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package zio.elasticsearch

import zio.Chunk
import zio.elasticsearch.ElasticAggregation.{maxAggregation, multipleAggregations, termsAggregation}
import zio.elasticsearch.aggregation._
import zio.elasticsearch.domain.{TestDocument, TestSubDocument}
Expand Down Expand Up @@ -44,7 +45,7 @@ object ElasticAggregationSpec extends ZIOSpecDefault {
Terms(
name = "first",
field = "stringField",
order = Set(AggregationOrder(value = "_key", order = Desc)),
order = Chunk(AggregationOrder(value = "_key", order = Desc)),
subAggregations = Nil,
size = None
),
Expand All @@ -60,7 +61,7 @@ object ElasticAggregationSpec extends ZIOSpecDefault {
Terms(
name = "first",
field = "testField",
order = Set.empty,
order = Chunk.empty,
subAggregations = List(
Max(
name = "second",
Expand All @@ -73,7 +74,7 @@ object ElasticAggregationSpec extends ZIOSpecDefault {
Terms(
name = "third",
field = "stringField",
order = Set.empty,
order = Chunk.empty,
subAggregations = Nil,
size = None
)
Expand All @@ -97,12 +98,12 @@ object ElasticAggregationSpec extends ZIOSpecDefault {
Terms(
name = "first",
field = "stringField",
order = Set.empty,
order = Chunk.empty,
subAggregations = List(
Terms(
name = "second",
field = "stringField.raw",
order = Set.empty,
order = Chunk.empty,
subAggregations = Nil,
size = None
)
Expand All @@ -117,14 +118,14 @@ object ElasticAggregationSpec extends ZIOSpecDefault {
Terms(
name = "first",
field = "stringField",
order = Set.empty,
order = Chunk.empty,
subAggregations = Nil,
size = None
),
Terms(
name = "second",
field = "testField",
order = Set.empty,
order = Chunk.empty,
subAggregations = List(
Max(
name = "third",
Expand Down Expand Up @@ -154,15 +155,15 @@ object ElasticAggregationSpec extends ZIOSpecDefault {

assert(aggregation)(
equalTo(
Terms(name = "aggregation", field = "testField", order = Set.empty, subAggregations = Nil, size = None)
Terms(name = "aggregation", field = "testField", order = Chunk.empty, subAggregations = Nil, size = None)
)
) &&
assert(aggregationTs)(
equalTo(
Terms(
name = "aggregation",
field = "stringField",
order = Set.empty,
order = Chunk.empty,
subAggregations = Nil,
size = None
)
Expand All @@ -173,7 +174,7 @@ object ElasticAggregationSpec extends ZIOSpecDefault {
Terms(
name = "aggregation",
field = "stringField.raw",
order = Set.empty,
order = Chunk.empty,
subAggregations = Nil,
size = None
)
Expand All @@ -184,8 +185,11 @@ object ElasticAggregationSpec extends ZIOSpecDefault {
Terms(
name = "aggregation",
field = "stringField",
order =
Set(AggregationOrder("_key", Desc), AggregationOrder("test", Desc), AggregationOrder("_count", Asc)),
order = Chunk(
AggregationOrder("_key", Desc),
AggregationOrder("test", Desc),
AggregationOrder("_count", Asc)
),
subAggregations = Nil,
size = None
)
Expand All @@ -196,7 +200,7 @@ object ElasticAggregationSpec extends ZIOSpecDefault {
Terms(
name = "aggregation",
field = "stringField",
order = Set.empty,
order = Chunk.empty,
subAggregations = Nil,
size = Some(10)
)
Expand All @@ -207,7 +211,7 @@ object ElasticAggregationSpec extends ZIOSpecDefault {
Terms(
name = "aggregation",
field = "stringField.test",
order = Set(AggregationOrder("_count", Desc), AggregationOrder("_key", Asc)),
order = Chunk(AggregationOrder("_count", Desc), AggregationOrder("_key", Asc)),
subAggregations = Nil,
size = Some(5)
)
Expand Down