Last active
February 3, 2026 20:20
-
-
Save dacr/cc9c8e3152895d6d1a99809228a184d0 to your computer and use it in GitHub Desktop.
Basic elastic4s (scala API for elasticsearch) tests using elasticsearch-cluster-runner. / published by https://github.com/dacr/code-examples-manager #28618179-8c06-4c07-a7f7-c53f6f2516a2/a6a748028a47c339abc639ba30144b8f3f2d052c
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. | |
| // 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 : 28618179-8c06-4c07-a7f7-c53f6f2516a2 | |
| // 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.`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 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.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 elasticPort = 9201 | |
| var client: ElasticClient = _ | |
| override def beforeAll: Unit = { | |
| client = ElasticClient(JavaClient(ElasticProperties(s"http://127.0.0.1:$elasticPort"))) | |
| } | |
| override def afterAll(): Unit = { | |
| client.close() | |
| } | |
| // ---------------------------------------------------------------------- | |
| "elasticsearch client application" should "be able to get cluster state information" in { | |
| client.execute { | |
| clusterState() | |
| } map { response => | |
| response.result.nodes.size should be > 0 | |
| } | |
| } | |
| // ---------------------------------------------------------------------- | |
| it should "be able to count the total number of crimes" in { | |
| val countFuture = client.execute { | |
| count("crimes") | |
| }.map(_.result.count) | |
| countFuture.map{ count => count should be > 6000000L} | |
| } | |
| // ---------------------------------------------------------------------- | |
| it should "be able to find homicides without arrest" in { | |
| val responseFuture = client.execute { | |
| search("crimes") | |
| .query { | |
| must( | |
| termQuery("Arrest", false), | |
| termQuery("PrimaryType", "homicide") | |
| ) | |
| } | |
| } | |
| responseFuture.map{response => | |
| response.result.hits.total.value should be > 0L | |
| } | |
| } | |
| // ---------------------------------------------------------------------- | |
| it should "be possible to count the number of distinct primary types" in { | |
| val responseFuture = client.execute { | |
| search("crimes").aggs { | |
| cardinalityAgg("crimesCountByType", "PrimaryType.keyword") | |
| } | |
| } | |
| responseFuture.map{response => | |
| response.result.aggregations.cardinality("crimesCountByType").value shouldBe 35d | |
| } | |
| } | |
| it should "be possible to count how many crimes for each primary type" in { | |
| val responseResult = client.execute { | |
| search("crimes").matchAllQuery().aggs { | |
| termsAgg("primaryTypesAgg", "PrimaryType.keyword").size(40) | |
| } | |
| } | |
| responseResult.map{response=> | |
| val rawresults = response.result.aggregations.data | |
| println(rawresults) | |
| rawresults.collect{case (primaryType,count:Long) => primaryType->count} | |
| }.map{results => | |
| results.size shouldBe 35 | |
| results.get("NARCOTICS") shouldBe > (0) | |
| } | |
| } | |
| } | |
| SimpleElasticsearchTest.execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment