Skip to content

Commit

Permalink
Refactor library core (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbulaja98 authored Sep 21, 2023
1 parent e55cea5 commit 653d1a5
Show file tree
Hide file tree
Showing 22 changed files with 155 additions and 74 deletions.
2 changes: 1 addition & 1 deletion docs/overview/requests/elastic_request_aggregate.md
Original file line number Diff line number Diff line change
Expand Up @@ -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._

Expand Down
2 changes: 1 addition & 1 deletion docs/overview/requests/elastic_request_refresh.md
Original file line number Diff line number Diff line change
Expand Up @@ -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._
```

Expand Down
2 changes: 1 addition & 1 deletion docs/overview/requests/elastic_request_search.md
Original file line number Diff line number Diff line change
Expand Up @@ -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._

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}

Expand All @@ -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))

Expand Down Expand Up @@ -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))

Expand All @@ -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))

Expand All @@ -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))

Expand All @@ -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("")
}

Expand All @@ -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))

Expand All @@ -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))

Expand All @@ -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))

Expand All @@ -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]

Expand All @@ -560,6 +569,7 @@ object ElasticRequest {
id: DocumentId,
routing: Option[Routing]
) extends ExistsRequest { self =>

def routing(value: Routing): ExistsRequest =
self.copy(routing = Some(value))
}
Expand All @@ -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))

Expand Down Expand Up @@ -621,6 +632,7 @@ object ElasticRequest {
searchAfter: Option[Json],
size: Option[Int]
) extends SearchRequest { self =>

def aggregate(aggregation: ElasticAggregation): SearchAndAggregateRequest =
SearchAndAggregate(
aggregation = aggregation,
Expand Down Expand Up @@ -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)))

Expand Down Expand Up @@ -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)))

Expand Down Expand Up @@ -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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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))

Expand All @@ -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))

Expand All @@ -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))

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
Loading

0 comments on commit 653d1a5

Please sign in to comment.