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

Sort and refactor methods #389

Merged
merged 5 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -21,8 +21,7 @@ import org.apache.commons.lang3.StringUtils.{equalsAny, startsWithAny}
import zio.Chunk
import zio.prelude.{AssertionError, Validator}

object IndexNameValidator
extends Validator[String](name => {
object IndexNameValidator extends Validator[String](name => {
def containsAny(string: String, params: Chunk[String]): Boolean =
params.exists(StringUtils.contains(string, _))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import org.apache.commons.lang3.StringUtils.{equalsAny, startsWithAny}
import zio.Chunk
import zio.prelude.{AssertionError, Validator}

object IndexPatternValidator
extends Validator[String](pattern => {
object IndexPatternValidator extends Validator[String](pattern => {
def containsAny(string: String, params: Chunk[String]): Boolean =
params.exists(StringUtils.contains(string, _))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,6 @@ object ElasticAggregation {
final def cardinalityAggregation(name: String, field: String): CardinalityAggregation =
Cardinality(name = name, field = field, missing = None)

/**
* Constructs an instance of [[zio.elasticsearch.aggregation.FilterAggregation]] using the specified parameters.
*
* @param name
* aggregation name
* @param query
* a query which the documents must match
* @return
* an instance of [[zio.elasticsearch.aggregation.FilterAggregation]] that represents filter aggregation to be
* performed.
*/
final def filterAggregation(name: String, query: ElasticQuery[_]): FilterAggregation =
Filter(name = name, query = query, subAggregations = Chunk.empty)

/**
* Constructs a type-safe instance of [[zio.elasticsearch.aggregation.ExtendedStatsAggregation]] using the specified
* parameters.
Expand Down Expand Up @@ -160,6 +146,20 @@ object ElasticAggregation {
final def extendedStatsAggregation(name: String, field: String): ExtendedStatsAggregation =
ExtendedStats(name = name, field = field, missing = None, sigma = None)

/**
* Constructs an instance of [[zio.elasticsearch.aggregation.FilterAggregation]] using the specified parameters.
*
* @param name
* aggregation name
* @param query
* a query which the documents must match
* @return
* an instance of [[zio.elasticsearch.aggregation.FilterAggregation]] that represents filter aggregation to be
* performed.
*/
final def filterAggregation(name: String, query: ElasticQuery[_]): FilterAggregation =
Filter(name = name, query = query, subAggregations = Chunk.empty)

/**
* Constructs a type-safe instance of [[zio.elasticsearch.aggregation.MaxAggregation]] using the specified parameters.
*
Expand Down
70 changes: 36 additions & 34 deletions modules/library/src/main/scala/zio/elasticsearch/ElasticQuery.scala
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,8 @@ object ElasticQuery {
* @param childType
* a name of the child relationship mapped for the join field
* @param query
* the type-safe [[ElasticQuery]] object representing query you wish to run on child documents of the child `type`
* field
* the type-safe [[zio.elasticsearch.ElasticQuery]] object representing query you wish to run on child documents of
* the child `type` field
* @tparam S
* document for which field query is executed
* @return
Expand All @@ -473,7 +473,8 @@ object ElasticQuery {
* @param childType
* a name of the child relationship mapped for the join field
* @param query
* the [[ElasticQuery]] object representing query you wish to run on child documents of the child `type` field
* the [[zio.elasticsearch.ElasticQuery]] object representing query you wish to run on child documents of the child
* `type` field
* @return
* an instance of [[zio.elasticsearch.query.HasChildQuery]] that represents the `has child query` to be performed.
*/
Expand All @@ -494,8 +495,8 @@ object ElasticQuery {
* @param parentType
* a name of the parent relationship mapped for the join field
* @param query
* the type-safe [[ElasticQuery]] object representing query you wish to run on parent documents of the `parent_type`
* field
* the type-safe [[zio.elasticsearch.ElasticQuery]] object representing query you wish to run on parent documents of
* the `parent_type` field
* @tparam S
* document for which field query is executed
* @return
Expand All @@ -517,7 +518,8 @@ object ElasticQuery {
* @param parentType
* a name of the parent relationship mapped for the join field
* @param query
* the [[ElasticQuery]] object representing query you wish to run on parent documents of the `parent_type` field
* the [[zio.elasticsearch.ElasticQuery]] object representing query you wish to run on parent documents of the
* `parent_type` field
* @return
* an instance of [[zio.elasticsearch.query.HasParentQuery]] that represents the has parent query to be performed.
*/
Expand Down Expand Up @@ -596,8 +598,10 @@ object ElasticQuery {
MatchAll(boost = None)

/**
* Constructs a type-safe instance of [[zio.elasticsearch.query.MatchQuery]] using the specified parameters.
* [[zio.elasticsearch.query.MatchQuery]] is used for matching a provided text, number, date or boolean value.
* Constructs a type-safe instance of [[zio.elasticsearch.query.MatchBooleanPrefixQuery]] using the specified
* parameters. [[zio.elasticsearch.query.MatchBooleanPrefixQuery]] analyzes its input and constructs a
* [[zio.elasticsearch.query.BoolQuery]] from the terms. Each term except the last is used in a
* [[zio.elasticsearch.query.TermQuery]]. The last term is used in a [[zio.elasticsearch.query.PrefixQuery]] query.
*
* @param field
* the type-safe field for which query is specified for
Expand All @@ -608,14 +612,17 @@ object ElasticQuery {
* @tparam A
* the type of value to be matched. A JSON decoder must be provided in the scope for this type
* @return
* an instance of [[zio.elasticsearch.query.MatchQuery]] that represents the match query to be performed.
* an instance of [[zio.elasticsearch.query.MatchBooleanPrefixQuery]] that represents the match boolean prefix query
* to be performed.
*/
final def matches[S, A: ElasticPrimitive](field: Field[S, A], value: A): MatchQuery[S] =
Match(field = field.toString, value = value)
final def matchBooleanPrefix[S, A: ElasticPrimitive](field: Field[S, A], value: A): MatchBooleanPrefixQuery[S] =
MatchBooleanPrefix(field = field.toString, value, minimumShouldMatch = None)

/**
* Constructs an instance of [[zio.elasticsearch.query.MatchQuery]] using the specified parameters.
* [[zio.elasticsearch.query.MatchQuery]] is used for matching a provided text, number, date or boolean value.
* Constructs an instance of [[zio.elasticsearch.query.MatchBooleanPrefixQuery]] using the specified parameters.
* [[zio.elasticsearch.query.MatchBooleanPrefixQuery]] analyzes its input and constructs a
* [[zio.elasticsearch.query.BoolQuery]] from the terms. Each term except the last is used in a
* [[zio.elasticsearch.query.TermQuery]]. The last term is used in a [[zio.elasticsearch.query.PrefixQuery]].
*
* @param field
* the field for which query is specified for
Expand All @@ -624,16 +631,15 @@ object ElasticQuery {
* @tparam A
* the type of value to be matched. A JSON decoder must be provided in the scope for this type
* @return
* an instance of [[zio.elasticsearch.query.MatchQuery]] that represents the match query to be performed.
* an instance of [[zio.elasticsearch.query.MatchBooleanPrefixQuery]] that represents the match boolean prefix query
* to be performed.
*/
final def matches[A: ElasticPrimitive](field: String, value: A): MatchQuery[Any] =
Match(field = field, value = value)
final def matchBooleanPrefix[A: ElasticPrimitive](field: String, value: A): MatchBooleanPrefixQuery[Any] =
MatchBooleanPrefix(field = field, value = value, minimumShouldMatch = None)

/**
* Constructs a type-safe instance of [[zio.elasticsearch.query.MatchBooleanPrefixQuery]] using the specified
* parameters. [[zio.elasticsearch.query.MatchBooleanPrefixQuery]] analyzes its input and constructs a
* [[zio.elasticsearch.query.BoolQuery]] from the terms. Each term except the last is used in a
* [[zio.elasticsearch.query.TermQuery]]. The last term is used in a [[zio.elasticsearch.query.PrefixQuery]] query.
* Constructs a type-safe instance of [[zio.elasticsearch.query.MatchQuery]] using the specified parameters.
* [[zio.elasticsearch.query.MatchQuery]] is used for matching a provided text, number, date or boolean value.
*
* @param field
* the type-safe field for which query is specified for
Expand All @@ -644,17 +650,14 @@ object ElasticQuery {
* @tparam A
* the type of value to be matched. A JSON decoder must be provided in the scope for this type
* @return
* an instance of [[zio.elasticsearch.query.MatchBooleanPrefixQuery]] that represents the match boolean prefix query
* to be performed.
* an instance of [[zio.elasticsearch.query.MatchQuery]] that represents the match query to be performed.
*/
final def matchBooleanPrefix[S, A: ElasticPrimitive](field: Field[S, A], value: A): MatchBooleanPrefixQuery[S] =
MatchBooleanPrefix(field = field.toString, value, minimumShouldMatch = None)
final def matches[S, A: ElasticPrimitive](field: Field[S, A], value: A): MatchQuery[S] =
Match(field = field.toString, value = value)

/**
* Constructs an instance of [[zio.elasticsearch.query.MatchBooleanPrefixQuery]] using the specified parameters.
* [[zio.elasticsearch.query.MatchBooleanPrefixQuery]] analyzes its input and constructs a
* [[zio.elasticsearch.query.BoolQuery]] from the terms. Each term except the last is used in a
* [[zio.elasticsearch.query.TermQuery]]. The last term is used in a [[zio.elasticsearch.query.PrefixQuery]].
* Constructs an instance of [[zio.elasticsearch.query.MatchQuery]] using the specified parameters.
* [[zio.elasticsearch.query.MatchQuery]] is used for matching a provided text, number, date or boolean value.
*
* @param field
* the field for which query is specified for
Expand All @@ -663,11 +666,10 @@ object ElasticQuery {
* @tparam A
* the type of value to be matched. A JSON decoder must be provided in the scope for this type
* @return
* an instance of [[zio.elasticsearch.query.MatchBooleanPrefixQuery]] that represents the match boolean prefix query
* to be performed.
* an instance of [[zio.elasticsearch.query.MatchQuery]] that represents the match query to be performed.
*/
final def matchBooleanPrefix[A: ElasticPrimitive](field: String, value: A): MatchBooleanPrefixQuery[Any] =
MatchBooleanPrefix(field = field, value = value, minimumShouldMatch = None)
final def matches[A: ElasticPrimitive](field: String, value: A): MatchQuery[Any] =
Match(field = field, value = value)

/**
* Constructs a type-safe instance of [[zio.elasticsearch.query.MatchPhraseQuery]] using the specified parameters.
Expand Down Expand Up @@ -840,7 +842,7 @@ object ElasticQuery {
* @param path
* the type-safe path to the field for which query is specified for
* @param query
* the [[ElasticQuery]] object representing the query to execute on nested objects.
* the [[zio.elasticsearch.ElasticQuery]] object representing the query to execute on nested objects.
* @tparam S
* document for which field query is executed
* @tparam A
Expand All @@ -858,7 +860,7 @@ object ElasticQuery {
* @param path
* the path to the field for which query is specified for
* @param query
* the [[ElasticQuery]] object representing the query to execute on nested objects.
* the [[zio.elasticsearch.ElasticQuery]] object representing the query to execute on nested objects.
* @return
* an instance of [[zio.elasticsearch.query.NestedQuery]] that represents the nested query to be performed.
*/
Expand Down
108 changes: 48 additions & 60 deletions modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -476,29 +476,6 @@ object ElasticRequest {
document.json
}

sealed trait CreateWithIdRequest
extends BulkableRequest[CreationOutcome]
with HasRefresh[CreateWithIdRequest]
with HasRouting[CreateWithIdRequest]

private[elasticsearch] final case class CreateWithId(
index: IndexName,
id: DocumentId,
document: Document,
refresh: Option[Boolean],
routing: Option[Routing]
) extends CreateWithIdRequest { self =>

def refresh(value: Boolean): CreateWithIdRequest =
self.copy(refresh = Some(value))

def routing(value: Routing): CreateWithIdRequest =
self.copy(routing = Some(value))

private[elasticsearch] def toJson: Json =
document.json
}

sealed trait CreateIndexRequest extends ElasticRequest[CreationOutcome]

private[elasticsearch] final case class CreateIndex(
Expand Down Expand Up @@ -532,6 +509,29 @@ object ElasticRequest {
document.json
}

sealed trait CreateWithIdRequest
extends BulkableRequest[CreationOutcome]
with HasRefresh[CreateWithIdRequest]
with HasRouting[CreateWithIdRequest]

private[elasticsearch] final case class CreateWithId(
index: IndexName,
id: DocumentId,
document: Document,
refresh: Option[Boolean],
routing: Option[Routing]
) extends CreateWithIdRequest { self =>

def refresh(value: Boolean): CreateWithIdRequest =
self.copy(refresh = Some(value))

def routing(value: Routing): CreateWithIdRequest =
self.copy(routing = Some(value))

private[elasticsearch] def toJson: Json =
document.json
}

sealed trait DeleteByIdRequest
extends BulkableRequest[DeletionOutcome]
with HasRefresh[DeleteByIdRequest]
Expand Down Expand Up @@ -615,10 +615,10 @@ object ElasticRequest {
* documents that can match. If not provided, all documents are allowed to match.
*
* @param query
* the Elastic query to be added
* [[zio.elasticsearch.ElasticQuery]] object for filtering the matching documents
* @return
* an instance of a [[zio.elasticsearch.ElasticRequest.KNNRequest]] that represents the kNN search operation
* enriched with filter query to be performed.
* enriched with filter query.
*/
def filter(query: ElasticQuery[_]): KNNRequest
}
Expand Down Expand Up @@ -661,7 +661,7 @@ object ElasticRequest {
* [[zio.elasticsearch.ElasticRequest.SearchRequest]].
*
* @param aggregation
* the Elastic aggregation to be added
* [[zio.elasticsearch.ElasticAggregation]] object for aggregating documents
* @return
* an instance of a [[zio.elasticsearch.ElasticRequest.SearchAndAggregateRequest]] that represents search and
* aggregate operations to be performed.
Expand Down Expand Up @@ -733,26 +733,21 @@ object ElasticRequest {
self.copy(sortBy = sortBy ++ (sort +: sorts))

private[elasticsearch] def toJson: Json = {
val fromJson: Json = from.fold(Obj())(f => Obj("from" -> f.toJson))

val sizeJson: Json = size.fold(Obj())(s => Obj("size" -> s.toJson))

val highlightsJson: Json = highlights.fold(Obj())(h => Obj("highlight" -> h.toJson(None)))

val fromJson: Json = from.fold(Obj())(f => Obj("from" -> f.toJson))
val sizeJson: Json = size.fold(Obj())(s => Obj("size" -> s.toJson))
val highlightsJson: Json = highlights.fold(Obj())(h => Obj("highlight" -> h.toJson(None)))
val searchAfterJson: Json = searchAfter.fold(Obj())(sa => Obj("search_after" -> sa))

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

val sortJson: Json = if (sortBy.nonEmpty) Obj("sort" -> Arr(sortBy.map(_.toJson))) else Obj()
val sourceJson: Json =
(included, excluded) match {
case (Chunk(), Chunk()) =>
Obj()
case (included, excluded) =>
Obj("_source" -> {
val includes = if (included.isEmpty) Obj() else Obj("includes" -> Arr(included.map(_.toJson)))
val excludes = if (excluded.isEmpty) Obj() else Obj("excludes" -> Arr(excluded.map(_.toJson)))
includes merge excludes
})
case (is, Chunk()) =>
Obj("_source" -> Obj("includes" -> Arr(is.map(_.toJson))))
case (Chunk(), es) =>
Obj("_source" -> Obj("excludes" -> Arr(es.map(_.toJson))))
case (is, es) =>
Obj("_source" -> Obj("includes" -> Arr(is.map(_.toJson)), "excludes" -> Arr(es.map(_.toJson))))
}

Obj("query" -> query.toJson(fieldPath = None)) merge
Expand Down Expand Up @@ -825,26 +820,21 @@ object ElasticRequest {
self.copy(sortBy = sortBy ++ (sort +: sorts))

private[elasticsearch] def toJson: Json = {
val fromJson: Json = from.fold(Obj())(f => Obj("from" -> f.toJson))

val sizeJson: Json = size.fold(Obj())(s => Obj("size" -> s.toJson))

val highlightsJson: Json = highlights.fold(Obj())(h => Obj("highlight" -> h.toJson(None)))

val fromJson: Json = from.fold(Obj())(f => Obj("from" -> f.toJson))
val sizeJson: Json = size.fold(Obj())(s => Obj("size" -> s.toJson))
val highlightsJson: Json = highlights.fold(Obj())(h => Obj("highlight" -> h.toJson(None)))
val searchAfterJson: Json = searchAfter.fold(Obj())(sa => Obj("search_after" -> sa))

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

val sortJson: Json = sortBy.nonEmptyOrElse(Obj())(sb => Obj("sort" -> Arr(sb.map(_.toJson))))
val sourceJson: Json =
(included, excluded) match {
case (Chunk(), Chunk()) =>
Obj()
case (included, excluded) =>
Obj("_source" -> {
val includes = if (included.isEmpty) Obj() else Obj("includes" -> Arr(included.map(_.toJson)))
val excludes = if (excluded.isEmpty) Obj() else Obj("excludes" -> Arr(excluded.map(_.toJson)))
includes merge excludes
})
case (is, Chunk()) =>
Obj("_source" -> Obj("includes" -> Arr(is.map(_.toJson))))
case (Chunk(), es) =>
Obj("_source" -> Obj("excludes" -> Arr(es.map(_.toJson))))
case (is, es) =>
Obj("_source" -> Obj("includes" -> Arr(is.map(_.toJson)), "excludes" -> Arr(es.map(_.toJson))))
}

Obj("query" -> query.toJson(fieldPath = None)) merge
Expand Down Expand Up @@ -897,11 +887,9 @@ object ElasticRequest {
self.copy(routing = Some(value))

private[elasticsearch] def toJson: Json = {
val docToJson: Json = doc.fold(Obj())(d => Obj("doc" -> d.json))

val docToJson: Json = doc.fold(Obj())(d => Obj("doc" -> d.json))
val scriptToJson: Json = script.fold(Obj())(s => Obj("script" -> s.toJson))

val upsertJson: Json = upsert.fold(Obj())(u => Obj("upsert" -> u.json))
val upsertJson: Json = upsert.fold(Obj())(u => Obj("upsert" -> u.json))

scriptToJson merge docToJson merge upsertJson
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ private[elasticsearch] final case class Field[-S, +A](parent: Option[Field[S, _]
* Appends a suffix to the name of the [[Field]]. The type of the field's value is preserved.
*
* @param suffix
* a user-defined suffix to append (e.g. 'keyword', 'raw')
* @tparam A1
* the underlying type of the current [[Field]], constrained by the [[ElasticPrimitive]], which specifies that it
* must be a supertype of the field's value
Expand Down
Loading