Skip to content

Instantly share code, notes, and snippets.

@samspills
Created February 17, 2023 19:48
Show Gist options
  • Save samspills/a457aa55f447b4820576685330be73f0 to your computer and use it in GitHub Desktop.
Save samspills/a457aa55f447b4820576685330be73f0 to your computer and use it in GitHub Desktop.
opensearch reproduction start
//> using lib "org.typelevel::cats-effect:3.4.7"
//> using lib "org.http4s::http4s-core:0.23.18"
//> using lib "org.http4s::http4s-dsl:0.23.18"
//> using lib "org.http4s::http4s-ember-client:0.23.18"
//> using lib "org.http4s::http4s-client:0.23.18"
//> using lib "org.http4s::http4s-circe:0.23.18"
//> using lib "io.circe::circe-core:0.14.4"
//> using lib "io.circe::circe-literal:0.14.4"
import cats.effect.kernel.Resource
import cats.effect.{IO, IOApp}
import org.http4s.ember.client.EmberClientBuilder
import org.http4s.client.Client
import fs2.io.net.Network
import io.circe._
import io.circe.literal._
import org.http4s.Request
import org.http4s.Headers
import org.http4s.headers._
import org.http4s.MediaType
import org.http4s.implicits._
import org.http4s.dsl.io._
import org.http4s._
import org.http4s.circe._
// pull and run opensearch docker image before running
// instructions here: https://hub.docker.com/r/opensearchproject/opensearch
object OpensearchRepro extends IOApp.Simple {
val openSearchUrl = uri"https://localhost:9200"
val sampleRequest = Request[IO](
method = GET,
uri = openSearchUrl,
headers = Headers(
Authorization(BasicCredentials("admin", "admin")),
Accept(MediaType.application.json),
)
)
def makeRequest(client: Client[IO]): IO[Unit] =
client.expect[String](sampleRequest).flatMap(IO.println)
// PUT more-movies
// { "settings": { "number_of_shards": 6, "number_of_replicas": 2 } }
def createIndex(client: Client[IO]): IO[Unit] = {
val indexRequest = Request[IO](
method = PUT,
uri = openSearchUrl / "test-index",
headers = Headers(
Authorization(BasicCredentials("admin", "admin")),
Accept(MediaType.application.json),
)
).withEntity(json"""{"settings": { "number_of_shards": 1, "number_of_replicas": 1 } }""")
client.expect[String](indexRequest).flatMap(IO.println)
}
// PUT <index>/_doc
// { "A JSON": "document" }
def indexDoc(client: Client[IO]): IO[Unit] = {
val indexRequest = Request[IO](
method = POST,
uri = openSearchUrl / "test-index" / "_doc",
headers = Headers(
Authorization(BasicCredentials("admin", "admin")),
Accept(MediaType.application.json),
)
).withEntity(json"""{ "id": 1, "text": "field"}""")
client.expect[String](indexRequest).flatMap(IO.println)
}
val run: IO[Unit] = {
Resource
.eval(Network[IO].tlsContext.insecure)
.flatMap { tls =>
EmberClientBuilder
.default[IO]
.withTLSContext(tls)
.build
}.use(client => indexDoc(client))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment