Last active
February 3, 2026 20:20
-
-
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/2d2bc9c5ff42276a9ef2b68491fb847779585858
This file contains hidden or 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
| // 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 License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // 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