Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass routing parameter when executing delete by query #96

Merged
merged 3 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we put Map at the bottom? Not sure about consistency with existing code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is the order for all routing and refresh instances, so we would have to change it there too

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mea culpa 🙋‍♂️

}
}

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