Skip to content

Commit

Permalink
Make basic requests to Opensearch
Browse files Browse the repository at this point in the history
  • Loading branch information
Willdotwhite committed Apr 16, 2024
1 parent f29ee01 commit d352529
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
5 changes: 5 additions & 0 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ dependencies {
// DB
implementation("org.litote.kmongo:kmongo:4.11.0")

// SE
implementation("org.apache.httpcomponents.core5:httpcore5:5.2.4")
implementation("org.apache.httpcomponents.core5:httpcore5-h2:5.2.4")
implementation("org.opensearch.client:opensearch-java:2.10.0")

// Discord bot
implementation("org.javacord:javacord:3.8.0")

Expand Down
8 changes: 5 additions & 3 deletions api/src/main/kotlin/com/gmtkgamejam/routing/PostRoutes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.gmtkgamejam.models.posts.dtos.PostItemReportDto
import com.gmtkgamejam.models.posts.dtos.PostItemUnableToContactReportDto
import com.gmtkgamejam.models.posts.dtos.PostItemUpdateDto
import com.gmtkgamejam.respondJSON
import com.gmtkgamejam.search.OpenSearch
import com.gmtkgamejam.services.AuthService
import com.gmtkgamejam.services.FavouritesService
import com.gmtkgamejam.services.PostService
Expand All @@ -20,12 +21,11 @@ import io.ktor.server.auth.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import java.time.format.DateTimeFormatter
import java.time.LocalDateTime
import org.bson.conversions.Bson
import org.litote.kmongo.*
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import kotlin.math.min
import kotlin.reflect.KClass
import kotlin.reflect.full.memberProperties
import kotlin.text.Regex.Companion.escape

Expand All @@ -51,6 +51,7 @@ fun Application.configurePostRouting() {
posts.map { it.isFavourite = favouritesList.postIds.contains(it.id) }
}

// posts.map { OpenSearch.index(it) }
call.respond(posts)
}

Expand Down Expand Up @@ -157,6 +158,7 @@ fun Application.configurePostRouting() {
post.updatedAt = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))

service.updatePost(post)
OpenSearch.update(post)
return@put call.respond(post)
}

Expand Down
69 changes: 69 additions & 0 deletions api/src/main/kotlin/com/gmtkgamejam/search/Opensearch.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.gmtkgamejam.search

import com.fasterxml.jackson.databind.node.ObjectNode
import com.gmtkgamejam.models.posts.PostItem
import org.apache.hc.core5.http.HttpHost
import org.opensearch.client.json.jackson.JacksonJsonpMapper
import org.opensearch.client.opensearch.OpenSearchClient
import org.opensearch.client.opensearch._types.query_dsl.Query
import org.opensearch.client.opensearch.core.IndexRequest
import org.opensearch.client.opensearch.core.SearchRequest
import org.opensearch.client.opensearch.core.UpdateRequest
import org.opensearch.client.opensearch.core.search.Hit
import org.opensearch.client.transport.OpenSearchTransport
import org.opensearch.client.transport.httpclient5.ApacheHttpClient5TransportBuilder

// TODO: Convert to module for inject()
object OpenSearch {

private val client: OpenSearchClient

init {
val transport: OpenSearchTransport = ApacheHttpClient5TransportBuilder
.builder(HttpHost("http", "localhost", 9200))
.setMapper(JacksonJsonpMapper())
.build()

client = OpenSearchClient(transport)
}


/**
* Perform a search request against OpenSearch and return the documents in the result
*/
fun search(index: String, query: Query): List<Hit<ObjectNode>> {
// If there wasn't a result in the cache (or it was too old),
// call OpenSearch and update the cache with the result
val searchRequest = SearchRequest.Builder()
.index(index)
.query(query)
.size(10000)
.build()

return client.search(searchRequest, ObjectNode::class.java).hits().hits()
}

// TODO: Handle response from client.index
fun index(postItem: PostItem) {
val indexRequest = IndexRequest.Builder<PostItem>()
.index("posts")
.document(postItem)
.build()

client.index(indexRequest)
}

// TODO: Do we need this _and_ index?
fun update(postItem: PostItem) {
val upsertRequest: UpdateRequest<PostItem, PostItem> = UpdateRequest.Builder<PostItem, PostItem>()
.index("posts")
.id(postItem.id)
.doc(postItem)
.docAsUpsert(true)
.build()

client.update(upsertRequest, PostItem::class.java)
}

fun delete(postItem: PostItem): Nothing = TODO()
}
1 change: 1 addition & 0 deletions db/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data-local*.json

0 comments on commit d352529

Please sign in to comment.