The first step for working with elastic4s is to create a connection client to call ElasticSearch. Similar to Java, the connection client is native and can be a node or a transport one.
You will need an up-and-running Elasticsearch installation, as we described in the Downloading and installing Elasticsearch recipe in Chapter 2, Downloading and Setup.
A Maven tool or an IDE that supports Scala programming, such as Eclipse (ScalaIDE) or IntelliJ IDEA, with the Scala plugin should be installed globally.
The code for this recipe can be found in the chapter_15/elastic4s_sample
directory and the reference file is ClientSample.scala
.
To create an Elasticsearch client and for create/search a document, we will perform the following steps:
elastic4s
library to the build.sbt
configuration via:libraryDependencies += "com.sksamuel.elastic4s" %% "elastic4s- core" % "5.0.0"
pom.xml
project, just add the following code:<dependency> <groupId>com.sksamuel.elastic4s</groupId> <artifactId>elastic4s-core_2.11</artifactId> <version>5.0.0</version> </dependency>
import com.sksamuel.elastic4s.{ElasticClient, ElasticsearchClientUri} import com.sksamuel.elastic4s.ElasticDsl._
object ClientSample extends App { val uri = ElasticsearchClientUri("elasticsearch://127.0.0.1:9300? cluster.name=elasticsearch") val client = ElasticClient.transport(uri)
indexInto
with the document in the following way:client.execute { indexInto("bands" / "artists") fields "name"- >"coldplay" }.await Thread.sleep(2000) //to be sure that the record is indexed
val resp = client.execute { search("bands" / "artists") query "coldplay" }.await println(resp) }
The result, if the document is available, will be as follows:
RichSearchResponse({"took":2,"timed_out":false,"_shards": {"total":5,"successful":5,"failed":0},"hits": {"total":1,"max_score":0.2876821,"hits": [{"_index":"bands","_type":"artists","_id":"AViBXXEWXe9IuvJzw- HT","_score":0.2876821,"_source":{"name":"coldplay"}}]}})
Elastic4s hides a lot of the boilerplate required for initializing an Elasticsearch client.
The simpler way to define a connection to Elasticsearch is via ElasticsearchClientUri
and this allows you to provide the following:
elasticsearch://localhost:9300,boo:9876
)?cluster.name=elasticsearch
)After having defined ElasticsearchClientUri
, you can create ElasticClient
, which is used for every Elasticsearch call.
You can initialize ElasticClient
in several ways:
ElasticsearchClientUri
, similar to a JDBC connection, which is very handy because you can store it as a simple string in your application configuration file:val client = ElasticClient.transport(uri)
val client = ElasticClient.transport("127.0.0.1", 9300)
ElasticsearchClientUri
, and a list of plugin classes to the advanced customization:val client = ElasticClient.transport(settings, uri, classOf[ReindexPlugin], classOf[ReindexPlugin])
val node=... val client = ElasticClient.fromNode(node)
val client = ElasticClient.fromClient(elasticSearchJavaClient)
TransportClient
athttps://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html.3.133.112.90