Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 7, 2023 15:45
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/cc9c8e3152895d6d1a99809228a184d0 to your computer and use it in GitHub Desktop.
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/9afe6d663722d7cf7847d207bb4e2e5209bbab05
// 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 NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// 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