-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f29ee01
commit d352529
Showing
4 changed files
with
80 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
data-local*.json |