Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/b8597a00373400db3dfedcc38bee52dc to your computer and use it in GitHub Desktop.
Save dacr/b8597a00373400db3dfedcc38bee52dc to your computer and use it in GitHub Desktop.
Basic elastic4s (scala API for elasticsearch) tests using elasticsearch-cluster-runner in cluster mode / published by https://github.com/dacr/code-examples-manager #366bfbc9-d3a0-4044-8b18-31de0d32c9ec/8cf10011a9068ec88f69ae4cf47844d6e2e1fa4e
// summary : Basic elastic4s (scala API for elasticsearch) tests using elasticsearch-cluster-runner in cluster mode
// keywords : scala, elasticsearch, elastic4s, cluster-runner, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 366bfbc9-d3a0-4044-8b18-31de0d32c9ec
// created-on : 2018-10-14T17:44:43Z
// managed-by : https://github.com/dacr/code-examples-manager
// execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc'
import $ivy.`org.codelibs:elasticsearch-cluster-runner:7.3.2.1`
import $ivy.`com.sksamuel.elastic4s::elastic4s-core:7.3.1`
import $ivy.`com.sksamuel.elastic4s::elastic4s-client-esjava:7.3.1`
import $ivy.`com.sksamuel.elastic4s::elastic4s-json-json4s:7.3.1`
import $ivy.`org.json4s::json4s-native:3.6.7`
import $ivy.`org.json4s::json4s-ext:3.6.7`
import $ivy.`org.scalatest::scalatest:3.0.8`
import org.slf4j.{Logger, LoggerFactory}
import com.sksamuel.elastic4s.{ElasticClient, ElasticProperties}
import com.sksamuel.elastic4s.http.JavaClient
import com.sksamuel.elastic4s.ElasticDsl._
import com.sksamuel.elastic4s.json4s.ElasticJson4s.Implicits._
import org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner, ElasticsearchClusterRunner.newConfigs
import org.elasticsearch.common.settings.Settings.Builder
import org.json4s.{DefaultFormats, native}
import org.json4s.ext.JavaTimeSerializers
import org.scalatest._
import scala.jdk.CollectionConverters._
object SimpleElasticsearchTest extends AsyncFlatSpec with Matchers with BeforeAndAfterAll {
override def suiteName = "SimpleElasticsearchTest"
val runner = new ElasticsearchClusterRunner()
val elasticPort = 9242
var client: ElasticClient = null
val clusterName = "testcluster"
override def beforeAll: Unit = {
val configs =
newConfigs
.numOfNode(1)
.basePath("elastic-data")
.clusterName(clusterName)
//.disableESLogger()
runner.onBuild(
new ElasticsearchClusterRunner.Builder() {
override def build(number:Int, settingsBuilder:Builder):Unit = {
settingsBuilder.put("cluster.routing.allocation.disk.threshold_enabled", false)
settingsBuilder.put("http.port", elasticPort) // For this port, OK because we have just 1 node
}
}
).build(configs)
runner.ensureYellow()
val settings = runner.node().settings().names()
println("***** "+settings.asScala.mkString(", "))
client = ElasticClient( JavaClient(ElasticProperties(s"http://127.0.0.1:$elasticPort")) )
}
override def afterAll(): Unit = {
client.close()
runner.close()
runner.clean()
}
"elastic4s client application" should "connect a dynamically started elasticsearch node" in {
client.execute {
clusterState()
} map { response =>
response.result.clusterName shouldBe clusterName
}
}
it should "be able to search for everything" in {
note("but at this point, elasticsearch is empty...")
client.execute {
search("")
} map { searchResponse =>
searchResponse.result.totalHits shouldBe 0
}
}
}
SimpleElasticsearchTest.execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment