Skip to content

Commit

Permalink
Pass routing parameter when executing delete by query (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvelimir authored Feb 23, 2023
1 parent be81a04 commit 8264f47
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,11 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC
}
}

def executeDeleteByQuery(r: DeleteByQueryRequest): Task[DeletionOutcome] = {
private def executeDeleteByQuery(r: DeleteByQueryRequest): Task[DeletionOutcome] = {
val uri =
uri"${config.uri}/${r.index}/$DeleteByQuery".withParams(getQueryParams(List(("refresh", Some(r.refresh)))))
uri"${config.uri}/${r.index}/$DeleteByQuery".withParams(
getQueryParams(List(("refresh", Some(r.refresh)), ("routing", r.routing)))
)

sendRequest(
request
Expand Down
22 changes: 21 additions & 1 deletion modules/library/src/main/scala/zio/elasticsearch/Routing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@
package zio.elasticsearch

import zio.elasticsearch.ElasticRequest._
import zio.elasticsearch.ElasticRequestType.{Bulk, Create, CreateWithId, DeleteById, Exists, GetById, Upsert}
import zio.elasticsearch.ElasticRequestType.{
Bulk,
Create,
CreateWithId,
DeleteById,
DeleteByQuery,
Exists,
GetById,
Upsert
}
import zio.prelude.Assertion.isEmptyString
import zio.prelude.Newtype

Expand Down Expand Up @@ -63,6 +72,17 @@ object Routing extends Newtype[String] {
}
}

implicit val deleteByQueryWithRouting: WithRouting[DeleteByQuery] = new WithRouting[DeleteByQuery] {
def withRouting[A](
request: ElasticRequest[A, DeleteByQuery],
routing: Routing
): ElasticRequest[A, DeleteByQuery] =
request match {
case Map(r, mapper) => Map(withRouting(r, routing), mapper)
case r: DeleteByQueryRequest => r.copy(routing = Some(routing))
}
}

implicit val existsWithRouting: WithRouting[Exists] = new WithRouting[Exists] {
def withRouting[A](request: ElasticRequest[A, Exists], routing: Routing): ElasticRequest[A, Exists] =
request match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,18 @@ object HttpElasticExecutorSpec extends WireMockSpec {
test("deleting by query request") {
val addStubMapping = ZIO.serviceWith[WireMockServer](
_.addStubMapping(
post(urlEqualTo("/repositories/_delete_by_query?refresh=true"))
post(urlEqualTo("/repositories/_delete_by_query?refresh=true&routing=routing"))
.willReturn(aResponse.withStatus(StatusCode.Ok.code))
.build
)
)

assertZIO(
addStubMapping *> ElasticRequest.deleteByQuery(index = index, query = matchAll).refreshTrue.execute
addStubMapping *> ElasticRequest
.deleteByQuery(index = index, query = matchAll)
.refreshTrue
.routing(Routing("routing"))
.execute
)(
equalTo(Deleted)
)
Expand Down

0 comments on commit 8264f47

Please sign in to comment.