Skip to content

Commit

Permalink
Modify API and add Scaladoc
Browse files Browse the repository at this point in the history
  • Loading branch information
mvelimir committed May 4, 2023
1 parent 7428e18 commit 2c13572
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ object HttpExecutorSpec extends IntegrationSpec {
)
query = range(TestDocument.doubleField).gte(100.0)
res <- Executor
.execute(ElasticRequest.search(firstSearchIndex, query).includes(PartialTestDocument.schema))
.execute(ElasticRequest.search(firstSearchIndex, query).includes[PartialTestDocument])
items <- res.items
} yield assert(items.map(item => Right(item.raw)))(
hasSameElements(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,12 +561,8 @@ object ElasticRequest {
with HasSourceFiltering[SearchRequest] {
def aggregate(aggregation: ElasticAggregation): SearchAndAggregateRequest

def excludes(fields: String*): SearchRequest

def highlights(value: Highlights): SearchRequest

def includes(fields: String*): SearchRequest

def searchAfter(value: Json): SearchRequest
}

Expand Down Expand Up @@ -597,17 +593,22 @@ object ElasticRequest {
size = size
)

def excludes(fields: String*): SearchRequest =
self.copy(excluded = excluded.map(_ ++ fields).orElse(Some(fields.toList)))
def excludes(field: String, fields: String*): SearchRequest =
self.copy(excluded = excluded.map(_ ++ (field +: fields)).orElse(Some(field +: fields.toList)))

def from(value: Int): SearchRequest =
self.copy(from = Some(value))

def highlights(value: Highlights): SearchRequest =
self.copy(highlights = Some(value))

def includes(fields: String*): SearchRequest =
self.copy(included = included.map(_ ++ fields).orElse(Some(fields.toList)))
def includes(field: String, fields: String*): SearchRequest =
self.copy(included = included.map(_ ++ (field +: fields)).orElse(Some(field +: fields.toList)))

def includes[A](implicit schema: Schema.Record[A]): SearchRequest = {
val fields = getFieldNames(schema)
self.copy(included = included.map(_ ++ fields).orElse(Some(fields)))
}

def routing(value: Routing): SearchRequest =
self.copy(routing = Some(value))
Expand Down Expand Up @@ -671,17 +672,22 @@ object ElasticRequest {
searchAfter: Option[Json],
size: Option[Int]
) extends SearchAndAggregateRequest { self =>
def excludes(fields: String*): SearchAndAggregateRequest =
self.copy(excluded = excluded.map(_ ++ fields).orElse(Some(fields.toList)))
def excludes(field: String, fields: String*): SearchAndAggregateRequest =
self.copy(excluded = excluded.map(_ ++ (field +: fields)).orElse(Some(field +: fields.toList)))

def from(value: Int): SearchAndAggregateRequest =
self.copy(from = Some(value))

def highlights(value: Highlights): SearchAndAggregateRequest =
self.copy(highlights = Some(value))

def includes(fields: String*): SearchAndAggregateRequest =
self.copy(included = included.map(_ ++ fields).orElse(Some(fields.toList)))
def includes(field: String, fields: String*): SearchAndAggregateRequest =
self.copy(included = included.map(_ ++ (field +: fields)).orElse(Some(field +: fields.toList)))

def includes[A](implicit schema: Schema.Record[A]): SearchAndAggregateRequest = {
val fields = getFieldNames(schema)
self.copy(included = included.map(_ ++ fields).orElse(Some(fields)))
}

def routing(value: Routing): SearchAndAggregateRequest =
self.copy(routing = Some(value))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,64 @@ package zio.elasticsearch.request.options
import zio.schema.Schema

private[elasticsearch] trait HasSourceFiltering[R <: HasSourceFiltering[R]] {
def excludes(fields: String*): R

def includes(fields: String*): R
/**
* Specifies one or more fields to be excluded in the response of a [[zio.elasticsearch.ElasticRequest.SearchRequest]]
* or a [[zio.elasticsearch.ElasticRequest.SearchAndAggregateRequest]].
*
* @param field
* a field to be excluded
* @param fields
* fields to be excluded
* @return
* an instance of a [[zio.elasticsearch.ElasticRequest.SearchRequest]] or a
* [[zio.elasticsearch.ElasticRequest.SearchAndAggregateRequest]] with specified fields to be excluded.
*/
def excludes(field: String, fields: String*): R

/**
* Specifies one or more fields to be included in the response of a [[zio.elasticsearch.ElasticRequest.SearchRequest]]
* or a [[zio.elasticsearch.ElasticRequest.SearchAndAggregateRequest]].
*
* @param field
* a field to be included
* @param fields
* fields to be included
* @return
* an instance of a [[zio.elasticsearch.ElasticRequest.SearchRequest]] or a
* [[zio.elasticsearch.ElasticRequest.SearchAndAggregateRequest]] with specified fields to be included.
*/
def includes(field: String, fields: String*): R

/**
* Specifies fields to be included in the response of a [[zio.elasticsearch.ElasticRequest.SearchRequest]] or a
* [[zio.elasticsearch.ElasticRequest.SearchAndAggregateRequest]] based on the schema of a case class.
*
* @tparam A
* a case class whose fields will be included in the response
* @param schema
* a record schema of [[A]]
* @return
* an instance of a [[zio.elasticsearch.ElasticRequest.SearchRequest]] or a
* [[zio.elasticsearch.ElasticRequest.SearchAndAggregateRequest]] with specified fields to be excluded.
*/
def includes[A](implicit schema: Schema.Record[A]): R

protected final def getFieldNames(schema: Schema.Record[_]): List[String] = {
def extractInnerSchema(schema: Schema[_]): Schema[_] =
Schema.force(schema) match {
case schema: Schema.Sequence[_, _, _] => Schema.force(schema.elementSchema)
case schema => schema
}

final def includes(schema: Schema.Record[_]): R = {
def loop(schema: Schema.Record[_], prefix: Option[String]): List[String] =
schema.fields.toList.flatMap { field =>
Schema.force(field.schema) match {
extractInnerSchema(field.schema) match {
case schema: Schema.Record[_] => loop(schema, prefix.map(_ + "." + field.name).orElse(Some(field.name)))
case schema: Schema.Sequence[_, _, _] =>
Schema.force(schema.elementSchema) match {
case schema: Schema.Record[_] => loop(schema, prefix.map(_ + "." + field.name).orElse(Some(field.name)))
case _ => List(prefix.fold[String](field.name)(_ + "." + field.name))
}
case _ => List(prefix.fold[String](field.name)(_ + "." + field.name))
case _ => List(prefix.fold[String](field.name)(_ + "." + field.name))
}
}

includes(loop(schema, None): _*)
loop(schema, None)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,20 @@ object ElasticRequestDSLSpec extends ZIOSpecDefault {
.highlights(highlight(TestDocument.intField))
.sort(sortBy(TestDocument.intField).missing(First))
.from(10)
.includes()
.excludes() match {
.includes("stringField")
.excludes("intField") match {
case r: ElasticRequest.Search => r.toJson
}
val expected =
"""
|{
| "_source" : {
| "includes" : [],
| "excludes" : []
| "includes" : [
| "stringField"
| ],
| "excludes" : [
| "intField"
| ]
| },
| "query" : {
| "range" : {
Expand Down Expand Up @@ -242,16 +246,20 @@ object ElasticRequestDSLSpec extends ZIOSpecDefault {
.highlights(highlight(TestDocument.intField))
.sort(sortBy(TestDocument.intField).missing(First))
.from(10)
.includes()
.excludes() match {
.includes("stringField")
.excludes("intField") match {
case r: ElasticRequest.SearchAndAggregate => r.toJson
}
val expected =
"""
|{
| "_source" : {
| "includes" : [],
| "excludes" : []
| "includes" : [
| "stringField"
| ],
| "excludes" : [
| "intField"
| ]
| },
| "query" : {
| "range" : {
Expand Down

0 comments on commit 2c13572

Please sign in to comment.