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

Add refresh to GetById request #106

Merged
merged 5 commits into from
Mar 9, 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 @@ -37,8 +37,9 @@ final case class RepositoriesElasticsearch(elasticsearch: Elasticsearch) {
def findById(organization: String, id: String): Task[Option[GitHubRepo]] =
for {
routing <- routingOf(organization)
res <-
elasticsearch.execute(ElasticRequest.getById(Index, DocumentId(id)).routing(routing)).documentAs[GitHubRepo]
res <- elasticsearch
.execute(ElasticRequest.getById(Index, DocumentId(id)).routing(routing))
.documentAs[GitHubRepo]
} yield res

def create(repository: GitHubRepo): Task[CreationOutcome] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ object ElasticRequest {
Bulk.of(requests = requests: _*)

def create[A: Schema](index: IndexName, doc: A): Create =
Create(index = index, document = Document.from(doc), refresh = false, routing = None)
Create(index = index, document = Document.from(doc), refresh = None, routing = None)

def create[A: Schema](index: IndexName, id: DocumentId, doc: A): CreateWithId =
CreateWithId(index = index, id = id, document = Document.from(doc), refresh = false, routing = None)
CreateWithId(index = index, id = id, document = Document.from(doc), refresh = None, routing = None)

def createIndex(name: IndexName): CreateIndex =
CreateIndex(name = name, definition = None)
Expand All @@ -53,10 +53,10 @@ object ElasticRequest {
CreateIndex(name = name, definition = Some(definition))

def deleteById(index: IndexName, id: DocumentId): DeleteById =
DeleteById(index = index, id = id, refresh = false, routing = None)
DeleteById(index = index, id = id, refresh = None, routing = None)

def deleteByQuery(index: IndexName, query: ElasticQuery[_]): DeleteByQuery =
DeleteByQuery(index = index, query = query, refresh = false, routing = None)
DeleteByQuery(index = index, query = query, refresh = None, routing = None)

def deleteIndex(name: IndexName): DeleteIndex =
DeleteIndex(name = name)
Expand All @@ -65,23 +65,23 @@ object ElasticRequest {
Exists(index = index, id = id, routing = None)

def getById(index: IndexName, id: DocumentId): GetById =
GetById(index = index, id = id, routing = None)
GetById(index = index, id = id, refresh = None, routing = None)

def search(index: IndexName, query: ElasticQuery[_]): Search =
Search(index = index, query = query, routing = None)

def upsert[A: Schema](index: IndexName, id: DocumentId, doc: A): CreateOrUpdate =
CreateOrUpdate(index = index, id = id, document = Document.from(doc), refresh = false, routing = None)
CreateOrUpdate(index = index, id = id, document = Document.from(doc), refresh = None, routing = None)

sealed trait BulkRequest extends ElasticRequest[Unit] with HasRefresh[Unit] with HasRouting[Unit]

private[elasticsearch] final case class Bulk(
requests: List[BulkableRequest[_]],
index: Option[IndexName],
refresh: Boolean,
refresh: Option[Boolean],
routing: Option[Routing]
) extends BulkRequest { self =>
def refresh(value: Boolean): Bulk = self.copy(refresh = value)
def refresh(value: Boolean): Bulk = self.copy(refresh = Some(value))

def refreshFalse: Bulk = refresh(false)

Expand Down Expand Up @@ -111,18 +111,18 @@ object ElasticRequest {

object Bulk {
def of(requests: BulkableRequest[_]*): Bulk =
Bulk(requests = requests.toList, index = None, refresh = false, routing = None)
Bulk(requests = requests.toList, index = None, refresh = None, routing = None)
}

sealed trait CreateRequest extends BulkableRequest[DocumentId] with HasRefresh[DocumentId] with HasRouting[DocumentId]

private[elasticsearch] final case class Create(
index: IndexName,
document: Document,
refresh: Boolean,
refresh: Option[Boolean],
routing: Option[Routing]
) extends CreateRequest { self =>
def refresh(value: Boolean): Create = self.copy(refresh = value)
def refresh(value: Boolean): Create = self.copy(refresh = Some(value))

def refreshFalse: Create = refresh(false)

Expand All @@ -140,10 +140,10 @@ object ElasticRequest {
index: IndexName,
id: DocumentId,
document: Document,
refresh: Boolean,
refresh: Option[Boolean],
routing: Option[Routing]
) extends CreateWithIdRequest { self =>
def refresh(value: Boolean): CreateWithId = self.copy(refresh = value)
def refresh(value: Boolean): CreateWithId = self.copy(refresh = Some(value))

def refreshFalse: CreateWithId = refresh(false)

Expand All @@ -165,10 +165,10 @@ object ElasticRequest {
index: IndexName,
id: DocumentId,
document: Document,
refresh: Boolean,
refresh: Option[Boolean],
routing: Option[Routing]
) extends CreateOrUpdateRequest { self =>
def refresh(value: Boolean): CreateOrUpdate = self.copy(refresh = value)
def refresh(value: Boolean): CreateOrUpdate = self.copy(refresh = Some(value))

def refreshFalse: CreateOrUpdate = refresh(false)

Expand All @@ -185,10 +185,10 @@ object ElasticRequest {
private[elasticsearch] final case class DeleteById(
index: IndexName,
id: DocumentId,
refresh: Boolean,
refresh: Option[Boolean],
routing: Option[Routing]
) extends DeleteByIdRequest { self =>
def refresh(value: Boolean): DeleteById = self.copy(refresh = value)
def refresh(value: Boolean): DeleteById = self.copy(refresh = Some(value))

def refreshFalse: DeleteById = refresh(false)

Expand All @@ -205,10 +205,10 @@ object ElasticRequest {
private[elasticsearch] final case class DeleteByQuery(
index: IndexName,
query: ElasticQuery[_],
refresh: Boolean,
refresh: Option[Boolean],
routing: Option[Routing]
) extends DeleteByQueryRequest { self =>
def refresh(value: Boolean): DeleteByQuery = self.copy(refresh = value)
def refresh(value: Boolean): DeleteByQuery = self.copy(refresh = Some(value))

def refreshFalse: DeleteByQuery = refresh(false)

Expand All @@ -231,13 +231,20 @@ object ElasticRequest {
def routing(value: Routing): Exists = self.copy(routing = Some(value))
}

sealed trait GetByIdRequest extends ElasticRequest[GetResult] with HasRouting[GetResult]
sealed trait GetByIdRequest extends ElasticRequest[GetResult] with HasRefresh[GetResult] with HasRouting[GetResult]

private[elasticsearch] final case class GetById(
index: IndexName,
id: DocumentId,
refresh: Option[Boolean],
routing: Option[Routing]
) extends GetByIdRequest { self =>
def refresh(value: Boolean): GetById = self.copy(refresh = Some(value))

def refreshFalse: GetById = refresh(false)

def refreshTrue: GetById = refresh(true)

def routing(value: Routing): GetById = self.copy(routing = Some(value))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC
val uri = (r.index match {
case Some(index) => uri"${config.uri}/$index/$Bulk"
case None => uri"${config.uri}/$Bulk"
}).withParams(getQueryParams(List(("refresh", Some(r.refresh)), ("routing", r.routing))))
}).withParams(getQueryParams(List(("refresh", r.refresh), ("routing", r.routing))))

sendRequest(
request.post(uri).contentType(ApplicationJson).body(r.body)
Expand All @@ -86,7 +86,7 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC

private def executeCreate(r: Create): Task[DocumentId] = {
val uri = uri"${config.uri}/${r.index}/$Doc"
.withParams(getQueryParams(List(("refresh", Some(r.refresh)), ("routing", r.routing))))
.withParams(getQueryParams(List(("refresh", r.refresh), ("routing", r.routing))))

sendRequestWithCustomResponse[ElasticCreateResponse](
request
Expand All @@ -112,7 +112,7 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC

private def executeCreateWithId(r: CreateWithId): Task[CreationOutcome] = {
val uri = uri"${config.uri}/${r.index}/$Create/${r.id}"
.withParams(getQueryParams(List(("refresh", Some(r.refresh)), ("routing", r.routing))))
.withParams(getQueryParams(List(("refresh", r.refresh), ("routing", r.routing))))

sendRequest(
request
Expand Down Expand Up @@ -144,7 +144,7 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC

private def executeCreateOrUpdate(r: CreateOrUpdate): Task[Unit] = {
val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}"
.withParams(getQueryParams(List(("refresh", Some(r.refresh)), ("routing", r.routing))))
.withParams(getQueryParams(List(("refresh", r.refresh), ("routing", r.routing))))

sendRequest(request.put(uri).contentType(ApplicationJson).body(r.document.json)).flatMap { response =>
response.code match {
Expand All @@ -156,7 +156,7 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC

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

sendRequest(request.delete(uri)).flatMap { response =>
response.code match {
Expand All @@ -170,7 +170,7 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC
private def executeDeleteByQuery(r: DeleteByQuery): Task[DeletionOutcome] = {
val uri =
uri"${config.uri}/${r.index}/$DeleteByQuery".withParams(
getQueryParams(List(("refresh", Some(r.refresh)), ("routing", r.routing)))
getQueryParams(List(("refresh", r.refresh), ("routing", r.routing)))
)

sendRequest(
Expand Down Expand Up @@ -209,7 +209,9 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC
}

private def executeGetById(r: GetById): Task[GetResult] = {
val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}".withParams(getQueryParams(List(("routing", r.routing))))
val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}".withParams(
getQueryParams(List(("refresh", r.refresh), ("routing", r.routing)))
)

sendRequestWithCustomResponse[ElasticGetResponse](
request
Expand Down