diff --git a/modules/example/src/main/scala/example/RepositoriesElasticsearch.scala b/modules/example/src/main/scala/example/RepositoriesElasticsearch.scala index a6bf5849a..28ab974da 100644 --- a/modules/example/src/main/scala/example/RepositoriesElasticsearch.scala +++ b/modules/example/src/main/scala/example/RepositoriesElasticsearch.scala @@ -18,9 +18,9 @@ package example import zio._ import zio.elasticsearch.ElasticQuery.matchAll +import zio.elasticsearch._ import zio.elasticsearch.query.ElasticQuery import zio.elasticsearch.request.{CreationOutcome, DeletionOutcome} -import zio.elasticsearch.{DocumentId, ElasticRequest, Elasticsearch, Routing, ZIODocumentOps} final case class RepositoriesElasticsearch(elasticsearch: Elasticsearch) { diff --git a/modules/example/src/main/scala/example/package.scala b/modules/example/src/main/scala/example/package.scala index 2c5113509..98e570dd2 100644 --- a/modules/example/src/main/scala/example/package.scala +++ b/modules/example/src/main/scala/example/package.scala @@ -14,7 +14,7 @@ * limitations under the License. */ -import zio.elasticsearch.IndexName +import zio.elasticsearch._ package object example { final val Index: IndexName = IndexName("repositories") diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala index 05a827470..beb7dbf61 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala @@ -22,6 +22,7 @@ import zio.elasticsearch.highlights.Highlights import zio.elasticsearch.query.ElasticQuery import zio.elasticsearch.query.sort.Sort import zio.elasticsearch.request._ +import zio.elasticsearch.request.options._ import zio.elasticsearch.result.{ AggregationResult, GetResult, @@ -370,7 +371,7 @@ object ElasticRequest { self.copy(routing = Some(value)) lazy val body: String = requests.flatMap { r => - r match { + (r: @unchecked) match { case Create(index, document, _, routing) => List(getActionAndMeta("create", List(("_index", Some(index)), ("routing", routing))), document.json) case CreateWithId(index, id, document, _, routing) => @@ -551,7 +552,7 @@ object ElasticRequest { extends ElasticRequest[SearchResult] with HasFrom[SearchRequest] with HasRouting[SearchRequest] - with WithSort[SearchRequest] + with HasSort[SearchRequest] with HasSize[SearchRequest] { def aggregate(aggregation: ElasticAggregation): SearchAndAggregateRequest @@ -623,7 +624,7 @@ object ElasticRequest { with HasFrom[SearchAndAggregateRequest] with HasRouting[SearchAndAggregateRequest] with HasSize[SearchAndAggregateRequest] - with WithSort[SearchAndAggregateRequest] { + with HasSort[SearchAndAggregateRequest] { def highlights(value: Highlights): SearchAndAggregateRequest def searchAfter(value: Json): SearchAndAggregateRequest 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 5b5de6758..45e094968 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/aggregation/Aggregations.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/aggregation/Aggregations.scala @@ -18,6 +18,7 @@ package zio.elasticsearch.aggregation import zio.elasticsearch.ElasticAggregation.multipleAggregations import zio.elasticsearch.ElasticPrimitive.ElasticPrimitiveOps +import zio.elasticsearch.aggregation.options._ import zio.json.ast.Json import zio.json.ast.Json.Obj diff --git a/modules/library/src/main/scala/zio/elasticsearch/aggregation/options/WithAgg.scala b/modules/library/src/main/scala/zio/elasticsearch/aggregation/options/WithAgg.scala new file mode 100644 index 000000000..bf39a7f80 --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/aggregation/options/WithAgg.scala @@ -0,0 +1,32 @@ +/* + * Copyright 2022 LambdaWorks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package zio.elasticsearch.aggregation.options + +import zio.elasticsearch.aggregation.{MultipleAggregations, SingleElasticAggregation} + +private[elasticsearch] trait WithAgg { + + /** + * Adds a new aggregation to the list of aggregations represented as [[MultipleAggregations]]. + * + * @param agg + * the [[SingleElasticAggregation]] to add + * @return + * a new instance of [[MultipleAggregations]] with the specified aggregation added to its list of aggregations. + */ + def withAgg(agg: SingleElasticAggregation): MultipleAggregations +} diff --git a/modules/library/src/main/scala/zio/elasticsearch/result/package.scala b/modules/library/src/main/scala/zio/elasticsearch/aggregation/options/WithSubAgg.scala similarity index 52% rename from modules/library/src/main/scala/zio/elasticsearch/result/package.scala rename to modules/library/src/main/scala/zio/elasticsearch/aggregation/options/WithSubAgg.scala index 56de58875..ca8fc14b5 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/result/package.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/aggregation/options/WithSubAgg.scala @@ -14,17 +14,19 @@ * limitations under the License. */ -package zio.elasticsearch +package zio.elasticsearch.aggregation.options -package object result { - class ElasticException(message: String) extends RuntimeException(message) +import zio.elasticsearch.aggregation.SingleElasticAggregation - final case class DecodingException(message: String) extends ElasticException(message) +private[elasticsearch] trait WithSubAgg[A <: WithSubAgg[A]] { - case object UnauthorizedException extends ElasticException("Wrong credentials provided.") - - final case class VersionConflictException(succeeded: Int, failed: Int) - extends ElasticException( - s"There are $failed documents failed due to version conflict, but $succeeded documents are updated successfully." - ) + /** + * Adds a sub-aggregation to the aggregation. + * + * @param subAgg + * the [[SingleElasticAggregation]] to add as sub-aggregation + * @return + * a new instance of the [[zio.elasticsearch.aggregation.ElasticAggregation]] with the given sub-aggregation. + */ + def withSubAgg(subAgg: SingleElasticAggregation): A } diff --git a/modules/library/src/main/scala/zio/elasticsearch/aggregation/package.scala b/modules/library/src/main/scala/zio/elasticsearch/aggregation/package.scala deleted file mode 100644 index cd0ba9269..000000000 --- a/modules/library/src/main/scala/zio/elasticsearch/aggregation/package.scala +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2022 LambdaWorks - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package zio.elasticsearch - -package object aggregation { - private[elasticsearch] trait WithSubAgg[A <: WithSubAgg[A]] { - - /** - * Adds a sub-aggregation to the aggregation. - * - * @param subAgg - * the [[SingleElasticAggregation]] to add as sub-aggregation - * @return - * a new instance of the [[ElasticAggregation]] with the given sub-aggregation. - */ - def withSubAgg(subAgg: SingleElasticAggregation): A - } - - private[elasticsearch] trait WithAgg { - - /** - * Adds a new aggregation to the list of aggregations represented as [[MultipleAggregations]]. - * - * @param agg - * the [[SingleElasticAggregation]] to add - * @return - * a new instance of [[MultipleAggregations]] with the specified aggregation added to its list of aggregations. - */ - def withAgg(agg: SingleElasticAggregation): MultipleAggregations - } -} diff --git a/modules/library/src/main/scala/zio/elasticsearch/executor/response/AggregationResponse.scala b/modules/library/src/main/scala/zio/elasticsearch/executor/response/AggregationResponse.scala index fb81cc890..5924fd83d 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/executor/response/AggregationResponse.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/executor/response/AggregationResponse.scala @@ -73,7 +73,7 @@ private[elasticsearch] object TermsAggregationBucket { val docCount = allFields("doc_count").asInstanceOf[Int] val subAggs = allFields.collect { case (field, data) if field != "key" && field != "doc_count" => - field match { + (field: @unchecked) match { case str if str.contains("terms#") => (field.split("#")(1), data.asInstanceOf[TermsAggregationResponse]) } 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 6548ab4ee..100a2951d 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/query/Queries.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/query/Queries.scala @@ -17,6 +17,7 @@ package zio.elasticsearch.query import zio.elasticsearch.ElasticPrimitive._ +import zio.elasticsearch.query.options._ import zio.json.ast.Json import zio.json.ast.Json.{Arr, Num, Obj, Str} import zio.schema.Schema diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/options/HasBoost.scala b/modules/library/src/main/scala/zio/elasticsearch/query/options/HasBoost.scala new file mode 100644 index 000000000..39f2ea13e --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/query/options/HasBoost.scala @@ -0,0 +1,32 @@ +/* + * Copyright 2022 LambdaWorks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package zio.elasticsearch.query.options + +private[elasticsearch] trait HasBoost[Q <: HasBoost[Q]] { + + /** + * Sets the `boost` parameter for this [[zio.elasticsearch.query.ElasticQuery]]. The `boost` value is a positive + * multiplier applied to the score of documents matching the query. A value greater than 1 increases the relevance + * score of matching documents, while a value less than 1 decreases it. The default `boost` value is 1. + * + * @param value + * a non-negative real number to set `boost` parameter to + * @return + * a new instance of the [[zio.elasticsearch.query.ElasticQuery]] with the `boost` value set. + */ + def boost(value: Double): Q +} diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/options/HasCaseInsensitive.scala b/modules/library/src/main/scala/zio/elasticsearch/query/options/HasCaseInsensitive.scala new file mode 100644 index 000000000..d90133469 --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/query/options/HasCaseInsensitive.scala @@ -0,0 +1,53 @@ +/* + * Copyright 2022 LambdaWorks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package zio.elasticsearch.query.options + +private[elasticsearch] trait HasCaseInsensitive[Q <: HasCaseInsensitive[Q]] { + + /** + * Sets the `caseInsensitive` parameter for the [[zio.elasticsearch.query.ElasticQuery]]. Case-insensitive queries + * match text regardless of the case of the characters in the query string. By default, queries are case-sensitive. + * + * @param value + * the [[scala.Boolean]] value for `caseInsensitive` parameter + * @return + * a new instance of the [[zio.elasticsearch.query.ElasticQuery]] with the `caseInsensitive` value set. + */ + def caseInsensitive(value: Boolean): Q + + /** + * Sets the `caseInsensitive` parameter to `false` for this [[zio.elasticsearch.query.ElasticQuery]]. Same as + * [[caseInsensitive]](false). + * + * @return + * a new instance of the [[zio.elasticsearch.query.ElasticQuery]] with the `caseInsensitive` value set to `false`. + * @see + * #caseInsensitive + */ + final def caseInsensitiveFalse: Q = caseInsensitive(false) + + /** + * Sets the `caseInsensitive` parameter to `true` for this [[zio.elasticsearch.query.ElasticQuery]]. Same as + * [[caseInsensitive]](true). + * + * @return + * a new instance of the [[zio.elasticsearch.query.ElasticQuery]] with the `caseInsensitive` value set to `true`. + * @see + * #caseInsensitive + */ + final def caseInsensitiveTrue: Q = caseInsensitive(true) +} diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/options/HasIgnoreUnmapped.scala b/modules/library/src/main/scala/zio/elasticsearch/query/options/HasIgnoreUnmapped.scala new file mode 100644 index 000000000..ee70e6c3b --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/query/options/HasIgnoreUnmapped.scala @@ -0,0 +1,52 @@ +/* + * Copyright 2022 LambdaWorks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package zio.elasticsearch.query.options + +private[elasticsearch] trait HasIgnoreUnmapped[Q <: HasIgnoreUnmapped[Q]] { + + /** + * Sets the `ignoreUnmapped` parameter to control whether to ignore unmapped fields and return empty hits. + * + * @param value + * the `boolean` value for `ignoreUnmapped` parameter + * @return + * a new instance of the [[zio.elasticsearch.query.ElasticQuery]] with the `ignoreUnmapped` value set. + */ + def ignoreUnmapped(value: Boolean): Q + + /** + * Sets the `ignoreUnmapped` parameter to `false` for this [[zio.elasticsearch.query.ElasticQuery]]. Same as + * [[ignoreUnmapped]](false). + * + * @return + * a new instance of the [[zio.elasticsearch.query.ElasticQuery]] with the `ignoreUnmapped` value set to `false`. + * @see + * #ignoreUnmapped + */ + final def ignoreUnmappedFalse: Q = ignoreUnmapped(false) + + /** + * Sets the `ignoreUnmapped` parameter to `true` for this [[zio.elasticsearch.query.ElasticQuery]]. Same as + * [[ignoreUnmapped]](true). + * + * @return + * a new instance of the [[zio.elasticsearch.query.ElasticQuery]] with the `ignoreUnmapped` value set to `true`. + * @see + * #ignoreUnmapped + */ + final def ignoreUnmappedTrue: Q = ignoreUnmapped(true) +} diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/options/HasInnerHits.scala b/modules/library/src/main/scala/zio/elasticsearch/query/options/HasInnerHits.scala new file mode 100644 index 000000000..5e836f9b1 --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/query/options/HasInnerHits.scala @@ -0,0 +1,40 @@ +/* + * Copyright 2022 LambdaWorks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package zio.elasticsearch.query.options + +import zio.elasticsearch.query.InnerHits + +private[elasticsearch] trait HasInnerHits[Q <: HasInnerHits[Q]] { + + /** + * Sets the inner hits for this [[zio.elasticsearch.query.ElasticQuery]] to the default `InnerHits()` value. + * + * @return + * a new instance of the [[zio.elasticsearch.query.ElasticQuery]] with the default inner hits. + */ + final def innerHits: Q = innerHits(InnerHits()) + + /** + * Sets the inner hits configuration for the [[zio.elasticsearch.query.NestedQuery]]. + * + * @param innerHits + * the configuration for inner hits + * @return + * a new instance of the [[zio.elasticsearch.query.ElasticQuery]] with the specified inner hits configuration. + */ + def innerHits(innerHits: InnerHits): Q +} diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/options/HasMinimumShouldMatch.scala b/modules/library/src/main/scala/zio/elasticsearch/query/options/HasMinimumShouldMatch.scala new file mode 100644 index 000000000..7731b1272 --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/query/options/HasMinimumShouldMatch.scala @@ -0,0 +1,32 @@ +/* + * Copyright 2022 LambdaWorks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package zio.elasticsearch.query.options + +private[elasticsearch] trait HasMinimumShouldMatch[Q <: HasMinimumShouldMatch[Q]] { + + /** + * Sets the `minimumShouldMatch` parameter for this [[ElasticQuery]]. The `minimumShouldMatch` value is the number of + * should clauses returned documents must match. If the [[zio.elasticsearch.query.BoolQuery]] includes at least one + * `should` clause and no `must`/`filter` clauses, the default value is 1. Otherwise, the default value is 0. + * + * @param value + * a number to set `minimumShouldMatch` parameter to + * @return + * a new instance of the [[ElasticQuery]] with the `minimumShouldMatch` value set. + */ + def minimumShouldMatch(value: Int): Q +} diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/options/HasScoreMode.scala b/modules/library/src/main/scala/zio/elasticsearch/query/options/HasScoreMode.scala new file mode 100644 index 000000000..eebf24f34 --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/query/options/HasScoreMode.scala @@ -0,0 +1,39 @@ +/* + * Copyright 2022 LambdaWorks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package zio.elasticsearch.query.options + +import zio.elasticsearch.query.ScoreMode + +private[elasticsearch] trait HasScoreMode[Q <: HasScoreMode[Q]] { + + /** + * Sets the [[ScoreMode]] for the [[zio.elasticsearch.query.NestedQuery]]. The [[zio.elasticsearch.query.ScoreMode]] + * specifies how scores of matching documents are combined for the [[zio.elasticsearch.query.NestedQuery]]. + * + * @param scoreMode + * the [[ScoreMode]] to use for the [[zio.elasticsearch.query.NestedQuery]] + * - [[ScoreMode.Avg]]: uses the mean relevance score of all matching child objects + * - [[ScoreMode.Max]]: uses the highest relevance score of all matching child objects + * - [[ScoreMode.Min]]: uses the lowest relevance score of all matching child objects + * - [[ScoreMode.None]]: ignores relevance scores of matching child objects and uses 0 as a score + * - [[ScoreMode.Sum]]: adds together the relevance scores of all matching child objects + * @return + * a new instance of the [[zio.elasticsearch.query.ElasticQuery]] with the specified + * [[zio.elasticsearch.query.ScoreMode]]. + */ + def scoreMode(scoreMode: ScoreMode): Q +} diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/package.scala b/modules/library/src/main/scala/zio/elasticsearch/query/package.scala deleted file mode 100644 index 60ea150c3..000000000 --- a/modules/library/src/main/scala/zio/elasticsearch/query/package.scala +++ /dev/null @@ -1,156 +0,0 @@ -/* - * Copyright 2022 LambdaWorks - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package zio.elasticsearch - -package object query { - private[elasticsearch] trait HasBoost[Q <: HasBoost[Q]] { - - /** - * Sets the `boost` parameter for this [[ElasticQuery]]. The `boost` value is a positive multiplier applied to the - * score of documents matching the query. A value greater than 1 increases the relevance score of matching - * documents, while a value less than 1 decreases it. The default `boost` value is 1. - * - * @param value - * a non-negative real number to set `boost` parameter to - * @return - * a new instance of the [[ElasticQuery]] with the `boost` value set. - */ - def boost(value: Double): Q - } - - private[elasticsearch] trait HasCaseInsensitive[Q <: HasCaseInsensitive[Q]] { - - /** - * Sets the `caseInsensitive` parameter for the [[ElasticQuery]]. Case-insensitive queries match text regardless of - * the case of the characters in the query string. By default, queries are case-sensitive. - * - * @param value - * the [[scala.Boolean]] value for `caseInsensitive` parameter - * @return - * a new instance of the [[ElasticQuery]] with the `caseInsensitive` value set. - */ - def caseInsensitive(value: Boolean): Q - - /** - * Sets the `caseInsensitive` parameter to `false` for this [[ElasticQuery]]. Same as [[caseInsensitive]](false). - * - * @return - * a new instance of the [[ElasticQuery]] with the `caseInsensitive` value set to `false`. - * @see - * #caseInsensitive - */ - final def caseInsensitiveFalse: Q = caseInsensitive(false) - - /** - * Sets the `caseInsensitive` parameter to `true` for this [[ElasticQuery]]. Same as [[caseInsensitive]](true). - * - * @return - * a new instance of the [[ElasticQuery]] with the `caseInsensitive` value set to `true`. - * @see - * #caseInsensitive - */ - final def caseInsensitiveTrue: Q = caseInsensitive(true) - } - - private[elasticsearch] trait HasIgnoreUnmapped[Q <: HasIgnoreUnmapped[Q]] { - - /** - * Sets the `ignoreUnmapped` parameter to control whether to ignore unmapped fields and return empty hits. - * - * @param value - * the `boolean` value for `ignoreUnmapped` parameter - * @return - * a new instance of the [[ElasticQuery]] with the `ignoreUnmapped` value set. - */ - def ignoreUnmapped(value: Boolean): Q - - /** - * Sets the `ignoreUnmapped` parameter to `false` for this [[ElasticQuery]]. Same as [[ignoreUnmapped]](false). - * - * @return - * a new instance of the [[ElasticQuery]] with the `ignoreUnmapped` value set to `false`. - * @see - * #ignoreUnmapped - */ - final def ignoreUnmappedFalse: Q = ignoreUnmapped(false) - - /** - * Sets the `ignoreUnmapped` parameter to `true` for this [[ElasticQuery]]. Same as [[ignoreUnmapped]](true). - * - * @return - * a new instance of the [[ElasticQuery]] with the `ignoreUnmapped` value set to `true`. - * @see - * #ignoreUnmapped - */ - final def ignoreUnmappedTrue: Q = ignoreUnmapped(true) - } - - private[elasticsearch] trait HasInnerHits[Q <: HasInnerHits[Q]] { - - /** - * Sets the inner hits for this [[ElasticQuery]] to the default `InnerHits()` value. - * - * @return - * a new instance of the [[ElasticQuery]] with the default inner hits. - */ - final def innerHits: Q = innerHits(InnerHits()) - - /** - * Sets the inner hits configuration for the [[NestedQuery]]. - * - * @param innerHits - * the configuration for inner hits - * @return - * a new instance of the [[ElasticQuery]] with the specified inner hits configuration. - */ - def innerHits(innerHits: InnerHits): Q - } - - private[elasticsearch] trait HasMinimumShouldMatch[Q <: HasMinimumShouldMatch[Q]] { - - /** - * Sets the `minimumShouldMatch` parameter for this [[ElasticQuery]]. The `minimumShouldMatch` value is the number - * of should clauses returned documents must match. If the [[zio.elasticsearch.query.BoolQuery]] includes at least - * one `should` clause and no `must`/`filter` clauses, the default value is 1. Otherwise, the default value is 0. - * - * @param value - * a number to set `minimumShouldMatch` parameter to - * @return - * a new instance of the [[ElasticQuery]] with the `minimumShouldMatch` value set. - */ - def minimumShouldMatch(value: Int): Q - } - - private[elasticsearch] trait HasScoreMode[Q <: HasScoreMode[Q]] { - - /** - * Sets the [[ScoreMode]] for the [[NestedQuery]]. The [[ScoreMode]] specifies how scores of matching documents are - * combined for the [[NestedQuery]]. - * - * @param scoreMode - * the [[ScoreMode]] to use for the [[NestedQuery]] - * - [[ScoreMode.Avg]]: uses the mean relevance score of all matching child objects - * - [[ScoreMode.Max]]: uses the highest relevance score of all matching child objects - * - [[ScoreMode.Min]]: uses the lowest relevance score of all matching child objects - * - [[ScoreMode.None]]: ignores relevance scores of matching child objects and uses 0 as a score - * - [[ScoreMode.Sum]]: adds together the relevance scores of all matching child objects - * @return - * a new instance of the [[ElasticQuery]] with the specified [[ScoreMode]]. - */ - def scoreMode(scoreMode: ScoreMode): Q - } -} 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 d25dd6fce..563c016b9 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 @@ -17,6 +17,7 @@ package zio.elasticsearch.query.sort import zio.elasticsearch.ElasticPrimitive.ElasticPrimitiveOps +import zio.elasticsearch.query.sort.options._ import zio.elasticsearch.script.Script import zio.json.ast.Json import zio.json.ast.Json.Obj @@ -27,12 +28,12 @@ sealed trait Sort { sealed trait SortByField extends Sort - with WithFormat[SortByField] - with WithMissing[SortByField] - with WithMode[SortByField] - with WithNumericType[SortByField] - with WithOrder[SortByField] - with WithUnmappedType[SortByField] { + with HasFormat[SortByField] + with HasMissing[SortByField] + with HasMode[SortByField] + with HasNumericType[SortByField] + with HasOrder[SortByField] + with HasUnmappedType[SortByField] { def paramsToJson: Json } @@ -77,7 +78,7 @@ private[elasticsearch] final case class SortByFieldOptions( self.copy(unmappedType = Some(value)) } -sealed trait SortByScript extends Sort with WithMode[SortByScript] with WithOrder[SortByScript] +sealed trait SortByScript extends Sort with HasMode[SortByScript] with HasOrder[SortByScript] private[elasticsearch] final case class SortByScriptOptions( script: Script, diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/sort/options/HasFormat.scala b/modules/library/src/main/scala/zio/elasticsearch/query/sort/options/HasFormat.scala new file mode 100644 index 000000000..e3dae9561 --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/query/sort/options/HasFormat.scala @@ -0,0 +1,31 @@ +/* + * Copyright 2022 LambdaWorks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package zio.elasticsearch.query.sort.options + +private[elasticsearch] trait HasFormat[S <: HasFormat[S]] { + + /** + * Sets the date format for the [[zio.elasticsearch.query.sort.SortByField]]. This method is only applicable to fields + * of type `date`. + * + * @param value + * the `date` format to set + * @return + * an instance of the [[zio.elasticsearch.query.sort.SortByField]] enriched with the `format` parameter. + */ + def format(value: String): S +} diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/sort/options/HasMissing.scala b/modules/library/src/main/scala/zio/elasticsearch/query/sort/options/HasMissing.scala new file mode 100644 index 000000000..58275f3f7 --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/query/sort/options/HasMissing.scala @@ -0,0 +1,34 @@ +/* + * Copyright 2022 LambdaWorks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package zio.elasticsearch.query.sort.options + +import zio.elasticsearch.query.sort.Missing + +private[elasticsearch] trait HasMissing[S <: HasMissing[S]] { + + /** + * Sets the value to use when a document is missing a value for the field being sorted. + * + * @param value + * the `missing` value behaviour + * - [[Missing.First]]: treated as first + * - [[Missing.Last]]: treated as last + * @return + * an instance of the [[zio.elasticsearch.query.sort.SortByField]] enriched with the `missing` parameter. + */ + def missing(value: Missing): S +} diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/sort/options/HasMode.scala b/modules/library/src/main/scala/zio/elasticsearch/query/sort/options/HasMode.scala new file mode 100644 index 000000000..0b26483ae --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/query/sort/options/HasMode.scala @@ -0,0 +1,38 @@ +/* + * Copyright 2022 LambdaWorks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package zio.elasticsearch.query.sort.options + +import zio.elasticsearch.query.sort.SortMode + +private[elasticsearch] trait HasMode[S <: HasMode[S]] { + + /** + * Sets the `mode` parameter for the [[zio.elasticsearch.query.sort.Sort]]. The `mode` parameter controls how + * Elasticsearch selects a single value from a set of sorted documents. The default `mode` is `Average`. + * + * @param value + * the [[SortMode]] used to pick a value among the sorted set of documents: + * - [[SortMode.Avg]]: uses the average of all values as sort value. Only applicable for number based array fields + * - [[SortMode.Max]]: picks the highest value + * - [[SortMode.Median]]: uses the median of all values as sort value. Only applicable for number based array fields + * - [[SortMode.Min]]: picks the lowest value + * - [[SortMode.Sum]]: uses the sum of all values as sort value. Only applicable for number based array fields + * @return + * an instance of the [[zio.elasticsearch.query.sort.Sort]] enriched with the `mode` parameter. + */ + def mode(value: SortMode): S +} diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/sort/options/HasNumericType.scala b/modules/library/src/main/scala/zio/elasticsearch/query/sort/options/HasNumericType.scala new file mode 100644 index 000000000..a7d5c54bf --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/query/sort/options/HasNumericType.scala @@ -0,0 +1,37 @@ +/* + * Copyright 2022 LambdaWorks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package zio.elasticsearch.query.sort.options + +import zio.elasticsearch.query.sort.NumericType + +private[elasticsearch] trait HasNumericType[S <: HasNumericType[S]] { + + /** + * Sets the `numeric type` that should be used for sorting the field. With `numeric type` it is possible to cast the + * values from one type to another. + * + * @param value + * the [[NumericType]] that should be used for sorting the field + * - [[NumericType.Date]]: converts values to Date + * - [[NumericType.DateNanos]]: converts values to DateNanos + * - [[NumericType.Double]]: converts values to Double + * - [[NumericType.Long]]: converts values to Long + * @return + * an instance of the [[zio.elasticsearch.query.sort.SortByField]] enriched with the `numeric type` parameter. + */ + def numericType(value: NumericType): S +} diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/sort/options/HasOrder.scala b/modules/library/src/main/scala/zio/elasticsearch/query/sort/options/HasOrder.scala new file mode 100644 index 000000000..6968fdcda --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/query/sort/options/HasOrder.scala @@ -0,0 +1,34 @@ +/* + * Copyright 2022 LambdaWorks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package zio.elasticsearch.query.sort.options + +import zio.elasticsearch.query.sort.SortOrder + +private[elasticsearch] trait HasOrder[S <: HasOrder[S]] { + + /** + * Sets the `sort order` of the field. + * + * @param value + * the [[SortOrder]] of the field + * - [[SortOrder.Asc]]: sets ascending sorting order + * - [[SortOrder.Desc]]: sets descending sorting order + * @return + * an instance of the [[zio.elasticsearch.query.sort.Sort]] enriched with the `sort order` parameter. + */ + def order(value: SortOrder): S +} diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/sort/options/HasUnmappedType.scala b/modules/library/src/main/scala/zio/elasticsearch/query/sort/options/HasUnmappedType.scala new file mode 100644 index 000000000..0da46364b --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/query/sort/options/HasUnmappedType.scala @@ -0,0 +1,30 @@ +/* + * Copyright 2022 LambdaWorks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package zio.elasticsearch.query.sort.options + +private[elasticsearch] trait HasUnmappedType[S <: HasUnmappedType[S]] { + + /** + * Sets the `unmapped type` which is used when the mapped field doesn't exist in the index. + * + * @param value + * the type to use when the mapped field doesn't exist in the index + * @return + * an instance of the [[zio.elasticsearch.query.sort.SortByField]] enriched with the `unmapped type` parameter. + */ + def unmappedType(value: String): S +} diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/sort/package.scala b/modules/library/src/main/scala/zio/elasticsearch/query/sort/package.scala deleted file mode 100644 index 1ee2c219c..000000000 --- a/modules/library/src/main/scala/zio/elasticsearch/query/sort/package.scala +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright 2022 LambdaWorks - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package zio.elasticsearch.query - -package object sort { - private[elasticsearch] trait WithFormat[S <: WithFormat[S]] { - - /** - * Sets the date format for the [[SortByField]]. This method is only applicable to fields of type `date`. - * - * @param value - * the `date` format to set - * @return - * an instance of the [[SortByField]] enriched with the `format` parameter. - */ - def format(value: String): S - } - - private[elasticsearch] trait WithMode[S <: WithMode[S]] { - - /** - * Sets the `mode` parameter for the [[Sort]]. The `mode` parameter controls how Elasticsearch selects a single - * value from a set of sorted documents. The default `mode` is `Average`. - * - * @param value - * the [[SortMode]] used to pick a value among the sorted set of documents: - * - [[SortMode.Avg]]: uses the average of all values as sort value. Only applicable for number based array fields - * - [[SortMode.Max]]: picks the highest value - * - [[SortMode.Median]]: uses the median of all values as sort value. Only applicable for number based array - * fields - * - [[SortMode.Min]]: picks the lowest value - * - [[SortMode.Sum]]: uses the sum of all values as sort value. Only applicable for number based array fields - * @return - * an instance of the [[Sort]] enriched with the `mode` parameter. - */ - def mode(value: SortMode): S - } - - private[elasticsearch] trait WithMissing[S <: WithMissing[S]] { - - /** - * Sets the value to use when a document is missing a value for the field being sorted. - * - * @param value - * the `missing` value behaviour - * - [[Missing.First]]: treated as first - * - [[Missing.Last]]: treated as last - * @return - * an instance of the [[SortByField]] enriched with the `missing` parameter. - */ - def missing(value: Missing): S - } - - private[elasticsearch] trait WithNumericType[S <: WithNumericType[S]] { - - /** - * Sets the `numeric type` that should be used for sorting the field. With `numeric type` it is possible to cast the - * values from one type to another. - * - * @param value - * the [[NumericType]] that should be used for sorting the field - * - [[NumericType.Date]]: converts values to Date - * - [[NumericType.DateNanos]]: converts values to DateNanos - * - [[NumericType.Double]]: converts values to Double - * - [[NumericType.Long]]: converts values to Long - * @return - * an instance of the [[SortByField]] enriched with the `numeric type` parameter. - */ - def numericType(value: NumericType): S - } - - private[elasticsearch] trait WithOrder[S <: WithOrder[S]] { - - /** - * Sets the `sort order` of the field. - * - * @param value - * the [[SortOrder]] of the field - * - [[SortOrder.Asc]]: sets ascending sorting order - * - [[SortOrder.Desc]]: sets descending sorting order - * @return - * an instance of the [[Sort]] enriched with the `sort order` parameter. - */ - def order(value: SortOrder): S - } - - private[elasticsearch] trait WithUnmappedType[S <: WithUnmappedType[S]] { - - /** - * Sets the `unmapped type` which is used when the mapped field doesn't exist in the index. - * - * @param value - * the type to use when the mapped field doesn't exist in the index - * @return - * an instance of the [[SortByField]] enriched with the `unmapped type` parameter. - */ - def unmappedType(value: String): S - } -} diff --git a/modules/library/src/main/scala/zio/elasticsearch/request/options/HasFrom.scala b/modules/library/src/main/scala/zio/elasticsearch/request/options/HasFrom.scala new file mode 100644 index 000000000..67634d85c --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/request/options/HasFrom.scala @@ -0,0 +1,31 @@ +/* + * Copyright 2022 LambdaWorks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package zio.elasticsearch.request.options + +private[elasticsearch] trait HasFrom[R <: HasFrom[R]] { + + /** + * Sets the starting offset from where the [[zio.elasticsearch.ElasticRequest.SearchRequest]] or the + * [[zio.elasticsearch.ElasticRequest.SearchAndAggregateRequest]] return results. + * + * @param value + * a non-negative number to set the `from` parameter in the [[zio.elasticsearch.ElasticRequest]] + * @return + * an instance of the [[zio.elasticsearch.ElasticRequest]] enriched with the `from` parameter. + */ + def from(value: Int): R +} diff --git a/modules/library/src/main/scala/zio/elasticsearch/request/options/HasRefresh.scala b/modules/library/src/main/scala/zio/elasticsearch/request/options/HasRefresh.scala new file mode 100644 index 000000000..cead0b3f2 --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/request/options/HasRefresh.scala @@ -0,0 +1,50 @@ +/* + * Copyright 2022 LambdaWorks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package zio.elasticsearch.request.options + +private[elasticsearch] trait HasRefresh[R <: HasRefresh[R]] { + + /** + * Configures whether or not to refresh the index after the operation in the [[zio.elasticsearch.ElasticRequest]]. + * + * @param value + * should be `true` if the index should be refreshed after the executed operation, `false` otherwise + * @return + * an instance of the [[zio.elasticsearch.ElasticRequest]] enriched with the `refresh` parameter. + */ + def refresh(value: Boolean): R + + /** + * Sets `refresh` parameter to `false` in the [[zio.elasticsearch.ElasticRequest]]. Same as [[refresh]](false). + * + * @return + * a new instance of the [[zio.elasticsearch.ElasticRequest]] with the `refresh` parameter set to `false`. + * @see + * #refresh + */ + final def refreshFalse: R = refresh(false) + + /** + * Sets `refresh` parameter to `true` in the [[zio.elasticsearch.ElasticRequest]]. Same as [[refresh]](true). + * + * @return + * a new instance of the [[zio.elasticsearch.ElasticRequest]] with the `refresh` parameter set to `true`. + * @see + * #refresh + */ + final def refreshTrue: R = refresh(true) +} diff --git a/modules/library/src/main/scala/zio/elasticsearch/request/options/HasRouting.scala b/modules/library/src/main/scala/zio/elasticsearch/request/options/HasRouting.scala new file mode 100644 index 000000000..2574af5da --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/request/options/HasRouting.scala @@ -0,0 +1,32 @@ +/* + * Copyright 2022 LambdaWorks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package zio.elasticsearch.request.options + +import zio.elasticsearch._ + +private[elasticsearch] trait HasRouting[R <: HasRouting[R]] { + + /** + * Specifies a `routing` value to be used for this [[zio.elasticsearch.ElasticRequest]]. + * + * @param value + * the [[Routing]] value to set for the [[zio.elasticsearch.ElasticRequest]] + * @return + * an instance of the [[zio.elasticsearch.ElasticRequest]] enriched with the `routing` parameter. + */ + def routing(value: Routing): R +} diff --git a/modules/library/src/main/scala/zio/elasticsearch/request/options/HasSize.scala b/modules/library/src/main/scala/zio/elasticsearch/request/options/HasSize.scala new file mode 100644 index 000000000..9759728a0 --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/request/options/HasSize.scala @@ -0,0 +1,30 @@ +/* + * Copyright 2022 LambdaWorks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package zio.elasticsearch.request.options + +private[elasticsearch] trait HasSize[R <: HasSize[R]] { + + /** + * Sets the maximum number of results. + * + * @param value + * a non-negative number to set the `size` parameter in the [[zio.elasticsearch.ElasticRequest]] + * @return + * an instance of the [[zio.elasticsearch.ElasticRequest]] enriched with the `size` parameter. + */ + def size(value: Int): R +} diff --git a/modules/library/src/main/scala/zio/elasticsearch/request/options/HasSort.scala b/modules/library/src/main/scala/zio/elasticsearch/request/options/HasSort.scala new file mode 100644 index 000000000..8e839da6e --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/request/options/HasSort.scala @@ -0,0 +1,33 @@ +/* + * Copyright 2022 LambdaWorks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package zio.elasticsearch.request.options + +import zio.elasticsearch.query.sort.Sort + +private[elasticsearch] trait HasSort[R <: HasSort[R]] { + + /** + * Sets the sorting criteria for the [[zio.elasticsearch.ElasticRequest.SearchRequest]] or the + * [[zio.elasticsearch.ElasticRequest.SearchAndAggregateRequest]]. + * + * @param sorts + * one or more [[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 +} diff --git a/modules/library/src/main/scala/zio/elasticsearch/request/package.scala b/modules/library/src/main/scala/zio/elasticsearch/request/package.scala deleted file mode 100644 index 38503f24d..000000000 --- a/modules/library/src/main/scala/zio/elasticsearch/request/package.scala +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright 2022 LambdaWorks - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package zio.elasticsearch - -import zio.elasticsearch.query.sort.Sort - -package object request { - private[elasticsearch] trait HasRefresh[R <: HasRefresh[R]] { - - /** - * Configures whether or not to refresh the index after the operation in the [[ElasticRequest]]. - * - * @param value - * should be `true` if the index should be refreshed after the executed operation, `false` otherwise - * @return - * an instance of the [[ElasticRequest]] enriched with the `refresh` parameter. - */ - def refresh(value: Boolean): R - - /** - * Sets `refresh` parameter to `false` in the [[ElasticRequest]]. Same as [[refresh]](false). - * - * @return - * a new instance of the [[ElasticRequest]] with the `refresh` parameter set to `false`. - * @see - * #refresh - */ - final def refreshFalse: R = refresh(false) - - /** - * Sets `refresh` parameter to `true` in the [[ElasticRequest]]. Same as [[refresh]](true). - * - * @return - * a new instance of the [[ElasticRequest]] with the `refresh` parameter set to `true`. - * @see - * #refresh - */ - final def refreshTrue: R = refresh(true) - } - - private[elasticsearch] trait HasRouting[R <: HasRouting[R]] { - - /** - * Specifies a `routing` value to be used for this [[ElasticRequest]]. - * - * @param value - * the [[Routing]] value to set for the [[ElasticRequest]] - * @return - * an instance of the [[ElasticRequest]] enriched with the `routing` parameter. - */ - def routing(value: Routing): R - } - - private[elasticsearch] trait HasFrom[R <: HasFrom[R]] { - - /** - * Sets the starting offset from where the [[zio.elasticsearch.ElasticRequest.SearchRequest]] or the - * [[zio.elasticsearch.ElasticRequest.SearchAndAggregateRequest]] return results. - * - * @param value - * a non-negative number to set the `from` parameter in the [[ElasticRequest]] - * @return - * an instance of the [[ElasticRequest]] enriched with the `from` parameter. - */ - def from(value: Int): R - } - - private[elasticsearch] trait HasSize[R <: HasSize[R]] { - - /** - * Sets the maximum number of results. - * - * @param value - * a non-negative number to set the `size` parameter in the [[ElasticRequest]] - * @return - * an instance of the [[ElasticRequest]] enriched with the `size` parameter. - */ - def size(value: Int): R - } - - private[elasticsearch] trait WithSort[R <: WithSort[R]] { - - /** - * Sets the sorting criteria for the [[zio.elasticsearch.ElasticRequest.SearchRequest]] or the - * [[zio.elasticsearch.ElasticRequest.SearchAndAggregateRequest]]. - * - * @param sorts - * one or more [[zio.elasticsearch.query.sort.Sort]] objects that define the sorting criteria - * @return - * an instance of the [[ElasticRequest]] enriched with the sorting criteria. - */ - def sort(sorts: Sort*): R - } -} diff --git a/modules/library/src/main/scala/zio/elasticsearch/result/ElasticException.scala b/modules/library/src/main/scala/zio/elasticsearch/result/ElasticException.scala new file mode 100644 index 000000000..70aef9720 --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/result/ElasticException.scala @@ -0,0 +1,28 @@ +/* + * Copyright 2022 LambdaWorks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package zio.elasticsearch.result + +class ElasticException(message: String) extends RuntimeException(message) + +final case class DecodingException(message: String) extends ElasticException(message) + +case object UnauthorizedException extends ElasticException("Wrong credentials provided.") + +final case class VersionConflictException(succeeded: Int, failed: Int) + extends ElasticException( + s"There are $failed documents failed due to version conflict, but $succeeded documents are updated successfully." + ) 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 7c548b2cc..01ce51551 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/script/Script.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/script/Script.scala @@ -17,6 +17,7 @@ package zio.elasticsearch.script import zio.elasticsearch.ElasticPrimitive.ElasticPrimitiveOps +import zio.elasticsearch.script.options._ import zio.json.ast.Json import zio.json.ast.Json.Obj @@ -24,8 +25,8 @@ private[elasticsearch] final case class Script( source: String, params: Map[String, Any], lang: Option[String] -) extends WithLang[Script] - with WithParams[Script] { self => +) extends HasLang[Script] + with HasParams[Script] { self => def lang(value: String): Script = self.copy(lang = Some(value)) diff --git a/modules/library/src/main/scala/zio/elasticsearch/script/options/HasLang.scala b/modules/library/src/main/scala/zio/elasticsearch/script/options/HasLang.scala new file mode 100644 index 000000000..92700e754 --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/script/options/HasLang.scala @@ -0,0 +1,30 @@ +/* + * Copyright 2022 LambdaWorks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package zio.elasticsearch.script.options + +private[elasticsearch] trait HasLang[S <: HasLang[S]] { + + /** + * Sets the language used for analyzing the field values. + * + * @param value + * the language used for analyzing the field values. + * @return + * an instance of the [[zio.elasticsearch.script.Script]] enriched with the `lang` parameter. + */ + def lang(value: String): S +} diff --git a/modules/library/src/main/scala/zio/elasticsearch/script/options/HasParams.scala b/modules/library/src/main/scala/zio/elasticsearch/script/options/HasParams.scala new file mode 100644 index 000000000..590488f6c --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/script/options/HasParams.scala @@ -0,0 +1,30 @@ +/* + * Copyright 2022 LambdaWorks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package zio.elasticsearch.script.options + +private[elasticsearch] trait HasParams[S <: HasParams[S]] { + + /** + * Adds additional parameters to a script field. + * + * @param values + * a sequence of pairs of parameter names and their values to be added to the script field + * @return + * an instance of the [[zio.elasticsearch.script.Script]] enriched with the `params` parameter. + */ + def withParams(values: (String, Any)*): S +} diff --git a/modules/library/src/main/scala/zio/elasticsearch/script/package.scala b/modules/library/src/main/scala/zio/elasticsearch/script/package.scala deleted file mode 100644 index 4c726107f..000000000 --- a/modules/library/src/main/scala/zio/elasticsearch/script/package.scala +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2022 LambdaWorks - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package zio.elasticsearch - -package object script { - private[elasticsearch] trait WithLang[S <: WithLang[S]] { - - /** - * Sets the language used for analyzing the field values. - * - * @param value - * the language used for analyzing the field values. - * @return - * an instance of the [[Script]] enriched with the `lang` parameter. - */ - def lang(value: String): S - } - - private[elasticsearch] trait WithParams[S <: WithParams[S]] { - - /** - * Adds additional parameters to a script field. - * - * @param values - * a sequence of pairs of parameter names and their values to be added to the script field - * @return - * an instance of the [[Script]] enriched with the `params` parameter. - */ - def withParams(values: (String, Any)*): S - } -}