diff --git a/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala b/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala index 8be6123f8..e8504ca87 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala @@ -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 diff --git a/modules/library/src/main/scala/zio/elasticsearch/Routing.scala b/modules/library/src/main/scala/zio/elasticsearch/Routing.scala index e90a4a1ed..28cc7cd9b 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/Routing.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/Routing.scala @@ -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 @@ -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 { diff --git a/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala index 1dbf694ca..ab9589aa7 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala @@ -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) )