diff --git a/docs/overview/requests/elastic_request_aggregate.md b/docs/overview/requests/elastic_request_aggregate.md index 09d92f9c1..7bb96afd7 100644 --- a/docs/overview/requests/elastic_request_aggregate.md +++ b/docs/overview/requests/elastic_request_aggregate.md @@ -9,7 +9,7 @@ To create a `Aggregate` request do the following: ```scala import zio.elasticsearch.ElasticRequest.AggregateRequest import zio.elasticsearch.ElasticRequest.aggregate -// this import is required for using `IndexName, IndexPattern, MultiIndex` +// this import is required for using `IndexName`, `IndexPattern` and `MultiIndex` import zio.elasticsearch._ import zio.elasticsearch.ElasticAggregation._ diff --git a/docs/overview/requests/elastic_request_refresh.md b/docs/overview/requests/elastic_request_refresh.md index b314d1e5f..93544bcb9 100644 --- a/docs/overview/requests/elastic_request_refresh.md +++ b/docs/overview/requests/elastic_request_refresh.md @@ -9,7 +9,7 @@ In order to use the `Refresh` request import the following: ```scala import zio.elasticsearch.ElasticRequest.RefreshRequest import zio.elasticsearch.ElasticRequest.refresh -// this import is required for using `IndexName, IndexPattern, MultiIndex` +// this import is required for using `IndexName`, `IndexPattern` and `MultiIndex` import zio.elasticsearch._ ``` diff --git a/docs/overview/requests/elastic_request_search.md b/docs/overview/requests/elastic_request_search.md index e6b91ca55..c15576ea5 100644 --- a/docs/overview/requests/elastic_request_search.md +++ b/docs/overview/requests/elastic_request_search.md @@ -13,7 +13,7 @@ To create a `Search` request do the following: ```scala import zio.elasticsearch.ElasticRequest.SearchRequest import zio.elasticsearch.ElasticRequest.search -// this import is required for using `IndexName, IndexPattern, MultiIndex` +// this import is required for using `IndexName`, `IndexPattern` and `MultiIndex` import zio.elasticsearch._ import zio.elasticsearch.ElasticQuery._ diff --git a/modules/library/src/main/scala-2/zio/elasticsearch/MultiIndex.scala b/modules/library/src/main/scala-2/zio/elasticsearch/MultiIndex.scala index 3ac7769a2..d98670566 100644 --- a/modules/library/src/main/scala-2/zio/elasticsearch/MultiIndex.scala +++ b/modules/library/src/main/scala-2/zio/elasticsearch/MultiIndex.scala @@ -46,7 +46,7 @@ object IndexSelector { } -final case class MultiIndex private[elasticsearch] (indices: Chunk[String]) { self => +private[elasticsearch] final case class MultiIndex private[elasticsearch] (indices: Chunk[String]) { self => def names(name: IndexName, names: IndexName*): MultiIndex = self.copy(indices = indices ++ Chunk.fromIterable(name.toString +: names.map(IndexName.unwrap))) diff --git a/modules/library/src/main/scala-3/zio/elasticsearch/IndexPatternValidator.scala b/modules/library/src/main/scala-3/zio/elasticsearch/IndexPatternValidator.scala index 4a349936d..fa55ff1f0 100644 --- a/modules/library/src/main/scala-3/zio/elasticsearch/IndexPatternValidator.scala +++ b/modules/library/src/main/scala-3/zio/elasticsearch/IndexPatternValidator.scala @@ -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, _)) diff --git a/modules/library/src/main/scala-3/zio/elasticsearch/MultiIndex.scala b/modules/library/src/main/scala-3/zio/elasticsearch/MultiIndex.scala index 71ff7fa04..24bbaef97 100644 --- a/modules/library/src/main/scala-3/zio/elasticsearch/MultiIndex.scala +++ b/modules/library/src/main/scala-3/zio/elasticsearch/MultiIndex.scala @@ -47,7 +47,7 @@ object IndexSelector { } -final case class MultiIndex private[elasticsearch] (indices: Chunk[String]) { self => +private[elasticsearch] final case class MultiIndex private[elasticsearch] (indices: Chunk[String]) { self => def names(name: IndexName, names: IndexName*): MultiIndex = self.copy(indices = indices ++ Chunk.fromIterable(name.toString +: names.map(IndexName.unwrap))) diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala index 74caa3963..fcfe0738b 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala @@ -48,7 +48,7 @@ object ElasticRequest { * Constructs an instance of [[AggregateRequest]] using the specified parameters. * * @param selectors - * the name of the index or more indices to be refreshed + * the name of the index or more indices to aggregate on * @param aggregation * the desired [[ElasticAggregation]] to perform * @return @@ -128,26 +128,26 @@ object ElasticRequest { /** * Constructs an instance of [[CreateIndexRequest]] used for creating an empty index. * - * @param name + * @param index * the name of the index to be created * @return * an instance of [[CreateIndexRequest]] that represents the create index operation to be performed. */ - final def createIndex(name: IndexName): CreateIndexRequest = - CreateIndex(name = name, definition = None) + final def createIndex(index: IndexName): CreateIndexRequest = + CreateIndex(index = index, definition = None) /** * Constructs an instance of [[CreateIndexRequest]] used for creating an index with a specified definition. * - * @param name + * @param index * the name of the index to be created * @param definition * the settings for the index * @return * an instance of [[CreateIndexRequest]] that represents the create index operation to be performed. */ - final def createIndex(name: IndexName, definition: String): CreateIndexRequest = - CreateIndex(name = name, definition = Some(definition)) + final def createIndex(index: IndexName, definition: String): CreateIndexRequest = + CreateIndex(index = index, definition = Some(definition)) /** * Constructs an instance of [[DeleteByIdRequest]] used for deleting a document from the specified index by specified @@ -180,13 +180,13 @@ object ElasticRequest { /** * Constructs an instance of [[DeleteIndexRequest]] used for deleting an index by specified name. * - * @param name + * @param index * the name of the index to be deleted * @return * an instance of [[DeleteIndexRequest]] that represents delete index operation to be performed. */ - final def deleteIndex(name: IndexName): DeleteIndexRequest = - DeleteIndex(name = name) + final def deleteIndex(index: IndexName): DeleteIndexRequest = + DeleteIndex(index = index) /** * Constructs an instance of [[ExistsRequest]] used for checking whether document exists. @@ -373,6 +373,7 @@ object ElasticRequest { private[elasticsearch] final case class Aggregate(selectors: String, aggregation: ElasticAggregation) extends AggregateRequest { + private[elasticsearch] def toJson: Json = Obj("aggs" -> aggregation.toJson) } @@ -387,6 +388,7 @@ object ElasticRequest { refresh: Option[Boolean], routing: Option[Routing] ) extends BulkRequest { self => + def refresh(value: Boolean): BulkRequest = self.copy(refresh = Some(value)) @@ -430,6 +432,7 @@ object ElasticRequest { query: Option[ElasticQuery[_]], routing: Option[Routing] ) extends CountRequest { self => + def routing(value: Routing): CountRequest = self.copy(routing = Some(value)) @@ -447,6 +450,7 @@ object ElasticRequest { refresh: Option[Boolean], routing: Option[Routing] ) extends CreateRequest { self => + def refresh(value: Boolean): CreateRequest = self.copy(refresh = Some(value)) @@ -469,6 +473,7 @@ object ElasticRequest { refresh: Option[Boolean], routing: Option[Routing] ) extends CreateWithIdRequest { self => + def refresh(value: Boolean): CreateWithIdRequest = self.copy(refresh = Some(value)) @@ -482,9 +487,10 @@ object ElasticRequest { sealed trait CreateIndexRequest extends ElasticRequest[CreationOutcome] private[elasticsearch] final case class CreateIndex( - name: IndexName, + index: IndexName, definition: Option[String] ) extends CreateIndexRequest { + private[elasticsearch] def toJson: String = definition.getOrElse("") } @@ -500,6 +506,7 @@ object ElasticRequest { refresh: Option[Boolean], routing: Option[Routing] ) extends CreateOrUpdateRequest { self => + def refresh(value: Boolean): CreateOrUpdateRequest = self.copy(refresh = Some(value)) @@ -521,6 +528,7 @@ object ElasticRequest { refresh: Option[Boolean], routing: Option[Routing] ) extends DeleteByIdRequest { self => + def refresh(value: Boolean): DeleteByIdRequest = self.copy(refresh = Some(value)) @@ -539,6 +547,7 @@ object ElasticRequest { refresh: Option[Boolean], routing: Option[Routing] ) extends DeleteByQueryRequest { self => + def refresh(value: Boolean): DeleteByQueryRequest = self.copy(refresh = Some(value)) @@ -551,7 +560,7 @@ object ElasticRequest { sealed trait DeleteIndexRequest extends ElasticRequest[DeletionOutcome] - private[elasticsearch] final case class DeleteIndex(name: IndexName) extends DeleteIndexRequest + private[elasticsearch] final case class DeleteIndex(index: IndexName) extends DeleteIndexRequest sealed trait ExistsRequest extends ElasticRequest[Boolean] with HasRouting[ExistsRequest] @@ -560,6 +569,7 @@ object ElasticRequest { id: DocumentId, routing: Option[Routing] ) extends ExistsRequest { self => + def routing(value: Routing): ExistsRequest = self.copy(routing = Some(value)) } @@ -575,6 +585,7 @@ object ElasticRequest { refresh: Option[Boolean], routing: Option[Routing] ) extends GetByIdRequest { self => + def refresh(value: Boolean): GetByIdRequest = self.copy(refresh = Some(value)) @@ -621,6 +632,7 @@ object ElasticRequest { searchAfter: Option[Json], size: Option[Int] ) extends SearchRequest { self => + def aggregate(aggregation: ElasticAggregation): SearchAndAggregateRequest = SearchAndAggregate( aggregation = aggregation, @@ -727,6 +739,7 @@ object ElasticRequest { searchAfter: Option[Json], size: Option[Int] ) extends SearchAndAggregateRequest { self => + def excludes[S](field: Field[S, _], fields: Field[S, _]*): SearchAndAggregateRequest = self.copy(excluded = excluded ++ (field.toString +: fields.map(_.toString))) @@ -824,6 +837,7 @@ object ElasticRequest { script: Option[Script], upsert: Option[Document] ) extends UpdateRequest { self => + def orCreate[A: Schema](doc: A): UpdateRequest = self.copy(upsert = Some(Document.from(doc))) @@ -869,6 +883,7 @@ object ElasticRequest { refresh: Option[Boolean], routing: Option[Routing] ) extends UpdateByQueryRequest { self => + def conflicts(value: UpdateConflicts): UpdateByQueryRequest = self.copy(conflicts = Some(value)) diff --git a/modules/library/src/main/scala/zio/elasticsearch/StreamConfig.scala b/modules/library/src/main/scala/zio/elasticsearch/StreamConfig.scala index 7c7a795fa..5228f3dcd 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/StreamConfig.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/StreamConfig.scala @@ -17,6 +17,7 @@ package zio.elasticsearch final case class StreamConfig(searchAfter: Boolean, keepAlive: String, pageSize: Option[Int] = None) { self => + def withPageSize(n: Int): StreamConfig = self.copy(pageSize = Some(n)) def keepAliveFor(s: String): StreamConfig = self.copy(keepAlive = s) diff --git a/modules/library/src/main/scala/zio/elasticsearch/aggregation/Aggregations.scala b/modules/library/src/main/scala/zio/elasticsearch/aggregation/Aggregations.scala index a38e9559a..68058df13 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/aggregation/Aggregations.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/aggregation/Aggregations.scala @@ -35,6 +35,7 @@ sealed trait AvgAggregation extends SingleElasticAggregation with HasMissing[Avg private[elasticsearch] final case class Avg(name: String, field: String, missing: Option[Double]) extends AvgAggregation { self => + def missing(value: Double): AvgAggregation = self.copy(missing = Some(value)) @@ -96,6 +97,7 @@ private[elasticsearch] final case class BucketSort( from: Option[Int], size: Option[Int] ) extends BucketSortAggregation { self => + def from(value: Int): BucketSortAggregation = self.copy(from = Some(value)) @@ -127,6 +129,7 @@ sealed trait CardinalityAggregation private[elasticsearch] final case class Cardinality(name: String, field: String, missing: Option[Double]) extends CardinalityAggregation { self => + def missing(value: Double): CardinalityAggregation = self.copy(missing = Some(value)) @@ -144,6 +147,7 @@ sealed trait MaxAggregation extends SingleElasticAggregation with HasMissing[Max private[elasticsearch] final case class Max(name: String, field: String, missing: Option[Double]) extends MaxAggregation { self => + def missing(value: Double): MaxAggregation = self.copy(missing = Some(value)) @@ -161,6 +165,7 @@ sealed trait MinAggregation extends SingleElasticAggregation with HasMissing[Min private[elasticsearch] final case class Min(name: String, field: String, missing: Option[Double]) extends MinAggregation { self => + def missing(value: Double): MinAggregation = self.copy(missing = Some(value)) @@ -200,6 +205,7 @@ sealed trait MultipleAggregations extends ElasticAggregation with WithAgg { private[elasticsearch] final case class Multiple(aggregations: Chunk[SingleElasticAggregation]) extends MultipleAggregations { self => + def aggregations(aggregations: SingleElasticAggregation*): MultipleAggregations = self.copy(aggregations = self.aggregations ++ aggregations) @@ -256,6 +262,7 @@ sealed trait SumAggregation extends SingleElasticAggregation with HasMissing[Sum private[elasticsearch] final case class Sum(name: String, field: String, missing: Option[Double]) extends SumAggregation { self => + def missing(value: Double): SumAggregation = self.copy(missing = Some(value)) @@ -283,6 +290,7 @@ private[elasticsearch] final case class Terms( subAggregations: Chunk[SingleElasticAggregation], size: Option[Int] ) extends TermsAggregation { self => + def orderBy(order: AggregationOrder, orders: AggregationOrder*): TermsAggregation = self.copy(order = self.order ++ (order +: orders)) diff --git a/modules/library/src/main/scala/zio/elasticsearch/executor/HttpExecutor.scala b/modules/library/src/main/scala/zio/elasticsearch/executor/HttpExecutor.scala index ad2851ee3..cd0246e07 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/executor/HttpExecutor.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/executor/HttpExecutor.scala @@ -226,7 +226,7 @@ private[elasticsearch] final class HttpExecutor private (esConfig: ElasticConfig private def executeCreateIndex(r: CreateIndex): Task[CreationOutcome] = sendRequest( baseRequest - .put(uri"${esConfig.uri}/${r.name}") + .put(uri"${esConfig.uri}/${r.index}") .contentType(ApplicationJson) .body(r.toJson) ).flatMap { response => @@ -304,7 +304,7 @@ private[elasticsearch] final class HttpExecutor private (esConfig: ElasticConfig } private def executeDeleteIndex(r: DeleteIndex): Task[DeletionOutcome] = - sendRequest(baseRequest.delete(uri"${esConfig.uri}/${r.name}")).flatMap { response => + sendRequest(baseRequest.delete(uri"${esConfig.uri}/${r.index}")).flatMap { response => response.code match { case HttpOk => ZIO.succeed(DeletionOutcome.Deleted) case HttpNotFound => ZIO.succeed(DeletionOutcome.NotFound) diff --git a/modules/library/src/main/scala/zio/elasticsearch/highlights/Highlights.scala b/modules/library/src/main/scala/zio/elasticsearch/highlights/Highlights.scala index e7ad18f97..66a37dd26 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/highlights/Highlights.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/highlights/Highlights.scala @@ -116,6 +116,7 @@ object Highlights { } private[elasticsearch] final case class HighlightField(field: String, config: HighlightConfig = Map.empty) { + def toStringJsonPair(fieldPath: Option[String]): (String, Obj) = fieldPath.map(_ + "." + field).getOrElse(field) -> Obj(Chunk.fromIterable(config)) diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/Distance.scala b/modules/library/src/main/scala/zio/elasticsearch/query/Distance.scala index a4287671f..950ba1a02 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/query/Distance.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/query/Distance.scala @@ -17,44 +17,75 @@ package zio.elasticsearch.query final case class Distance(distanceValue: Double, distanceUnit: DistanceUnit) { - override def toString: String = s"$distanceValue$distanceUnit" -} -sealed trait DistanceUnit { - def symbol: String - override def toString: String = symbol + override def toString: String = s"$distanceValue$distanceUnit" } +sealed trait DistanceUnit object DistanceUnit { - case object Centimeters extends DistanceUnit { def symbol: String = "cm" } - case object Feet extends DistanceUnit { def symbol: String = "ft" } - case object Inches extends DistanceUnit { def symbol: String = "in" } - case object Kilometers extends DistanceUnit { def symbol: String = "km" } - case object Miles extends DistanceUnit { def symbol: String = "mi" } - case object Meters extends DistanceUnit { def symbol: String = "m" } - case object Millimeters extends DistanceUnit { def symbol: String = "mm" } - case object NauticalMiles extends DistanceUnit { def symbol: String = "nmi" } - case object Yards extends DistanceUnit { def symbol: String = "yd" } -} + case object Centimeters extends DistanceUnit { + override def toString: String = "cm" + } + + case object Feet extends DistanceUnit { + override def toString: String = "ft" + } + + case object Inches extends DistanceUnit { + override def toString: String = "in" + } + + case object Kilometers extends DistanceUnit { + override def toString: String = "km" + } + + case object Miles extends DistanceUnit { + override def toString: String = "mi" + } + + case object Meters extends DistanceUnit { + override def toString: String = "m" + } + + case object Millimeters extends DistanceUnit { + override def toString: String = "mm" + } + + case object NauticalMiles extends DistanceUnit { + override def toString: String = "nmi" + } + + case object Yards extends DistanceUnit { + override def toString: String = "yd" + } -sealed trait DistanceType { - def value: String - override def toString: String = value } +sealed trait DistanceType + object DistanceType { - case object Arc extends DistanceType { def value: String = "arc" } - case object Plane extends DistanceType { def value: String = "plane" } + case object Arc extends DistanceType { + override def toString: String = "arc" + } -} + case object Plane extends DistanceType { + override def toString: String = "plane" + } -sealed trait ValidationMethod { - def value: String - override def toString: String = value } +sealed trait ValidationMethod + object ValidationMethod { - case object Coerce extends ValidationMethod { def value: String = "COERCE" } - case object IgnoreMalformed extends ValidationMethod { def value: String = "IGNORE_MALFORMED" } - case object Strict extends ValidationMethod { def value: String = "STRICT" } + case object Coerce extends ValidationMethod { + override def toString: String = "COERCE" + } + + case object IgnoreMalformed extends ValidationMethod { + override def toString: String = "IGNORE_MALFORMED" + } + + case object Strict extends ValidationMethod { + override def toString: String = "STRICT" + } } diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/FunctionScoreFunction.scala b/modules/library/src/main/scala/zio/elasticsearch/query/FunctionScoreFunction.scala index 1d59e5deb..c930ee6a7 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/query/FunctionScoreFunction.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/query/FunctionScoreFunction.scala @@ -698,6 +698,7 @@ private[elasticsearch] final case class ScriptScoreFunction[S]( private[elasticsearch] final case class WeightFunction[S](weight: Double, filter: Option[ElasticQuery[S]]) extends FunctionScoreFunction[S] { self => + def filter(filter: ElasticQuery[Any]): FunctionScoreFunction[Any] = WeightFunction(weight = self.weight, filter = Some(filter)) diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/Queries.scala b/modules/library/src/main/scala/zio/elasticsearch/query/Queries.scala index d7e78871c..d2c2595f4 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/query/Queries.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/query/Queries.scala @@ -138,6 +138,7 @@ private[elasticsearch] final case class Bool[S]( boost: Option[Double], minimumShouldMatch: Option[Int] ) extends BoolQuery[S] { self => + def boost(value: Double): BoolQuery[S] = self.copy(boost = Some(value)) @@ -202,6 +203,7 @@ private[elasticsearch] final case class ConstantScore[S](query: ElasticQuery[S], sealed trait ExistsQuery[S] extends ElasticQuery[S] with HasBoost[ExistsQuery[S]] private[elasticsearch] final case class Exists[S](field: String, boost: Option[Double]) extends ExistsQuery[S] { self => + def boost(value: Double): ExistsQuery[S] = self.copy(boost = Some(value)) @@ -538,8 +540,8 @@ private[elasticsearch] final case class HasParent[S]( ignoreUnmapped: Option[Boolean], innerHitsField: Option[InnerHits], score: Option[Boolean] -) extends HasParentQuery[S] { - self => +) extends HasParentQuery[S] { self => + def boost(value: Double): HasParentQuery[S] = self.copy(boost = Some(value)) @@ -570,6 +572,7 @@ private[elasticsearch] final case class HasParent[S]( sealed trait MatchQuery[S] extends ElasticQuery[S] private[elasticsearch] final case class Match[S, A: ElasticPrimitive](field: String, value: A) extends MatchQuery[S] { + private[elasticsearch] def toJson(fieldPath: Option[String]): Json = Obj("match" -> Obj(fieldPath.foldRight(field)(_ + "." + _) -> value.toJson)) } @@ -577,6 +580,7 @@ private[elasticsearch] final case class Match[S, A: ElasticPrimitive](field: Str sealed trait MatchAllQuery extends ElasticQuery[Any] with HasBoost[MatchAllQuery] private[elasticsearch] final case class MatchAll(boost: Option[Double]) extends MatchAllQuery { self => + def boost(value: Double): MatchAllQuery = self.copy(boost = Some(value)) @@ -609,6 +613,7 @@ sealed trait MatchPhraseQuery[S] extends ElasticQuery[S] with HasBoost[MatchPhra private[elasticsearch] final case class MatchPhrase[S](field: String, value: String, boost: Option[Double]) extends MatchPhraseQuery[S] { self => + def boost(value: Double): MatchPhraseQuery[S] = self.copy(boost = Some(value)) @@ -732,6 +737,7 @@ private[elasticsearch] final case class Nested[S]( innerHitsField: Option[InnerHits], scoreMode: Option[ScoreMode] ) extends NestedQuery[S] { self => + def ignoreUnmapped(value: Boolean): NestedQuery[S] = self.copy(ignoreUnmapped = Some(value)) @@ -756,31 +762,43 @@ private[elasticsearch] final case class Nested[S]( } sealed trait LowerBound { + private[elasticsearch] def toJson: Option[(String, Json)] } private[elasticsearch] final case class GreaterThan[A: ElasticPrimitive](value: A) extends LowerBound { - private[elasticsearch] def toJson: Option[(String, Json)] = Some("gt" -> value.toJson) + + private[elasticsearch] def toJson: Option[(String, Json)] = + Some("gt" -> value.toJson) } private[elasticsearch] final case class GreaterThanOrEqualTo[A: ElasticPrimitive](value: A) extends LowerBound { - private[elasticsearch] def toJson: Option[(String, Json)] = Some("gte" -> value.toJson) + + private[elasticsearch] def toJson: Option[(String, Json)] = + Some("gte" -> value.toJson) } sealed trait UpperBound { + private[elasticsearch] def toJson: Option[(String, Json)] } private[elasticsearch] final case class LessThan[A: ElasticPrimitive](value: A) extends UpperBound { - private[elasticsearch] def toJson: Option[(String, Json)] = Some("lt" -> value.toJson) + + private[elasticsearch] def toJson: Option[(String, Json)] = + Some("lt" -> value.toJson) } private[elasticsearch] final case class LessThanOrEqualTo[A: ElasticPrimitive](value: A) extends UpperBound { - private[elasticsearch] def toJson: Option[(String, Json)] = Some("lte" -> value.toJson) + + private[elasticsearch] def toJson: Option[(String, Json)] = + Some("lte" -> value.toJson) } private[elasticsearch] case object Unbounded extends LowerBound with UpperBound { - private[elasticsearch] def toJson: Option[(String, Json)] = None + + private[elasticsearch] def toJson: Option[(String, Json)] = + None } sealed trait PrefixQuery[S] extends ElasticQuery[S] with HasCaseInsensitive[PrefixQuery[S]] @@ -871,6 +889,7 @@ private[elasticsearch] final case class Range[S, A, LB <: LowerBound, UB <: Uppe boost: Option[Double], format: Option[String] ) extends RangeQuery[S, A, LB, UB] { self => + def boost(value: Double): RangeQuery[S, A, LB, UB] = self.copy(boost = Some(value)) @@ -944,6 +963,7 @@ sealed trait ScriptQuery extends ElasticQuery[Any] with HasBoost[ScriptQuery] private[elasticsearch] final case class Script(script: zio.elasticsearch.script.Script, boost: Option[Double]) extends ScriptQuery { self => + def boost(value: Double): ScriptQuery = self.copy(boost = Some(value)) @@ -959,6 +979,7 @@ private[elasticsearch] final case class Term[S, A: ElasticPrimitive]( boost: Option[Double], caseInsensitive: Option[Boolean] ) extends TermQuery[S] { self => + def boost(value: Double): TermQuery[S] = self.copy(boost = Some(value)) @@ -980,6 +1001,7 @@ private[elasticsearch] final case class Terms[S, A: ElasticPrimitive]( values: Chunk[A], boost: Option[Double] ) extends TermsQuery[S] { self => + def boost(value: Double): TermsQuery[S] = self.copy(boost = Some(value)) @@ -1001,6 +1023,7 @@ private[elasticsearch] final case class Wildcard[S]( boost: Option[Double], caseInsensitive: Option[Boolean] ) extends WildcardQuery[S] { self => + def boost(value: Double): WildcardQuery[S] = self.copy(boost = Some(value)) @@ -1018,6 +1041,7 @@ private[elasticsearch] final case class Wildcard[S]( sealed trait IdsQuery[S] extends ElasticQuery[S] private[elasticsearch] final case class Ids[S](values: Chunk[String]) extends IdsQuery[S] { self => + private[elasticsearch] def toJson(fieldPath: Option[String]): Json = Obj("ids" -> Obj("values" -> Arr(values.map(_.toJson)))) } diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/package.scala b/modules/library/src/main/scala/zio/elasticsearch/query/package.scala index 78e78f1f4..ca245f94f 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/query/package.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/query/package.scala @@ -19,5 +19,4 @@ package zio.elasticsearch package object query { type GeoHash = GeoHash.Type - } diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/sort/Sort.scala b/modules/library/src/main/scala/zio/elasticsearch/query/sort/Sort.scala index b9a69af13..b34ea783b 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/query/sort/Sort.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/query/sort/Sort.scala @@ -112,6 +112,7 @@ private[elasticsearch] final case class SortByFieldOptions( order: Option[SortOrder], unmappedType: Option[String] ) extends SortByField { self => + def format(value: String): SortByField = self.copy(format = Some(value)) @@ -152,6 +153,7 @@ private[elasticsearch] final case class SortByScriptOptions( mode: Option[SortMode], order: Option[SortOrder] ) extends SortByScript { self => + def mode(value: SortMode): SortByScript = self.copy(mode = Some(value)) diff --git a/modules/library/src/main/scala/zio/elasticsearch/request/Document.scala b/modules/library/src/main/scala/zio/elasticsearch/request/Document.scala index 2a8fca8e6..6b39bbb2a 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/request/Document.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/request/Document.scala @@ -25,7 +25,6 @@ import zio.schema.codec.JsonCodec private[elasticsearch] final case class Document(json: Json) private[elasticsearch] object Document { - def from[A](doc: A)(implicit schema: Schema[A]): Document = Document( - JsonCodec.jsonEncoder(schema).encodeJson(doc, indent = None).fromJson[Json].fold(_ => Obj(), identity) - ) + def from[A](doc: A)(implicit schema: Schema[A]): Document = + Document(JsonCodec.jsonEncoder(schema).encodeJson(doc, indent = None).fromJson[Json].fold(_ => Obj(), identity)) } diff --git a/modules/library/src/main/scala/zio/elasticsearch/result/ElasticResult.scala b/modules/library/src/main/scala/zio/elasticsearch/result/ElasticResult.scala index 848a18850..0fbcc7d4a 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/result/ElasticResult.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/result/ElasticResult.scala @@ -71,6 +71,7 @@ private[elasticsearch] sealed trait DocumentResult[F[_]] { final class AggregateResult private[elasticsearch] ( private val aggs: Map[String, AggregationResult] ) extends ResultWithAggregation { + def aggregation(name: String): Task[Option[AggregationResult]] = ZIO.succeed(aggs.get(name)) @@ -79,6 +80,7 @@ final class AggregateResult private[elasticsearch] ( } final class GetResult private[elasticsearch] (private val doc: Option[Item]) extends DocumentResult[Option] { + def documentAs[A: Schema]: IO[DecodingException, Option[A]] = ZIO .fromEither(doc match { @@ -96,6 +98,7 @@ final class SearchResult private[elasticsearch] ( private val hits: Chunk[Item], private val fullResponse: SearchWithAggregationsResponse ) extends DocumentResult[Chunk] { + def documentAs[A: Schema]: IO[DecodingException, Chunk[A]] = ZIO.fromEither { ZValidation.validateAll(hits.map(item => ZValidation.fromEither(item.documentAs))).toEitherWith { errors => @@ -122,6 +125,7 @@ final class SearchAndAggregateResult private[elasticsearch] ( private val fullResponse: SearchWithAggregationsResponse ) extends DocumentResult[Chunk] with ResultWithAggregation { + def aggregation(name: String): Task[Option[AggregationResult]] = ZIO.succeed(aggs.get(name)) diff --git a/modules/library/src/main/scala/zio/elasticsearch/result/Item.scala b/modules/library/src/main/scala/zio/elasticsearch/result/Item.scala index abf927a51..2084a6349 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/result/Item.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/result/Item.scala @@ -32,6 +32,7 @@ final case class Item( private val innerHits: Map[String, Chunk[Hit]] = Map.empty, sort: Option[Json] = None ) { + def documentAs[A](implicit schema: Schema[A]): Either[DecodeError, A] = JsonDecoder.decode(schema, raw.toString) lazy val highlights: Option[Map[String, Chunk[String]]] = highlight.flatMap { json => diff --git a/modules/library/src/main/scala/zio/elasticsearch/script/Script.scala b/modules/library/src/main/scala/zio/elasticsearch/script/Script.scala index c65a19bc8..5498225be 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/script/Script.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/script/Script.scala @@ -28,6 +28,7 @@ final case class Script private[elasticsearch] ( private val lang: Option[ScriptLang] ) extends HasLang[Script] with HasParams[Script] { self => + def lang(value: ScriptLang): Script = self.copy(lang = Some(value)) diff --git a/modules/library/src/test/scala/zio/elasticsearch/ElasticRequestSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/ElasticRequestSpec.scala index e7fea93d2..ce382d449 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/ElasticRequestSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/ElasticRequestSpec.scala @@ -196,10 +196,10 @@ object ElasticRequestSpec extends ZIOSpecDefault { }, test("createIndex") { val createIndexRequest = createIndex(Index) - val createIndexRequestWithDefinition = createIndex(name = Index, definition = "definition") + val createIndexRequestWithDefinition = createIndex(index = Index, definition = "definition") - assert(createIndexRequest)(equalTo(CreateIndex(name = Index, definition = None))) && - assert(createIndexRequestWithDefinition)(equalTo(CreateIndex(name = Index, definition = Some("definition")))) + assert(createIndexRequest)(equalTo(CreateIndex(index = Index, definition = None))) && + assert(createIndexRequestWithDefinition)(equalTo(CreateIndex(index = Index, definition = Some("definition")))) }, test("deleteById") { val deleteByIdRequest = deleteById(index = Index, id = DocId) @@ -252,18 +252,13 @@ object ElasticRequestSpec extends ZIOSpecDefault { val getByIdRequestWithRouting = getById(index = Index, id = DocId).routing(RoutingValue) val getByIdRequestWithAllParams = getById(index = Index, id = DocId).refreshTrue.routing(RoutingValue) - assert(getByIdRequest)( - equalTo(GetById(index = Index, id = DocId, refresh = None, routing = None)) - ) && assert( - getByIdRequestWithRefresh - )( + assert(getByIdRequest)(equalTo(GetById(index = Index, id = DocId, refresh = None, routing = None))) && + assert(getByIdRequestWithRefresh)( equalTo(GetById(index = Index, id = DocId, refresh = Some(true), routing = None)) ) && assert(getByIdRequestWithRouting)( equalTo(GetById(index = Index, id = DocId, refresh = None, routing = Some(RoutingValue))) ) && assert(getByIdRequestWithAllParams)( - equalTo( - GetById(index = Index, id = DocId, refresh = Some(true), routing = Some(RoutingValue)) - ) + equalTo(GetById(index = Index, id = DocId, refresh = Some(true), routing = Some(RoutingValue))) ) }, test("refresh") { @@ -1095,7 +1090,7 @@ object ElasticRequestSpec extends ZIOSpecDefault { val jsonRequest = createIndex(Index) match { case r: CreateIndex => r.toJson } - val jsonRequestWithDefinition = createIndex(name = Index, definition = definition) match { + val jsonRequestWithDefinition = createIndex(index = Index, definition = definition) match { case r: CreateIndex => r.toJson } diff --git a/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala index d698e1f48..13555fe80 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala @@ -102,7 +102,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { assertZIO(executorCreateDocumentId)(equalTo(Created)) }, test("createIndex") { - val executorCreateIndex = Executor.execute(ElasticRequest.createIndex(name = index)) + val executorCreateIndex = Executor.execute(ElasticRequest.createIndex(index = index)) val mapping = """ @@ -125,7 +125,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { |} |""".stripMargin val executorCreateIndexMapping = - Executor.execute(ElasticRequest.createIndex(name = index, definition = mapping)) + Executor.execute(ElasticRequest.createIndex(index = index, definition = mapping)) assertZIO(executorCreateIndex)(equalTo(Created)) && assertZIO(executorCreateIndexMapping)(equalTo(Created)) @@ -150,7 +150,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { assertZIO(executorDeleteByQuery)(equalTo(Deleted)) }, test("deleteIndex") { - val executorDeleteIndex = Executor.execute(ElasticRequest.deleteIndex(name = index)) + val executorDeleteIndex = Executor.execute(ElasticRequest.deleteIndex(index = index)) assertZIO(executorDeleteIndex)(equalTo(Deleted)) },