Creating a client in Scala

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.

Getting ready

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.

How to do it...

To create an Elasticsearch client and for create/search a document, we will perform the following steps:

  1. The first step is to add the elastic4s library to the build.sbt configuration via:
            libraryDependencies += "com.sksamuel.elastic4s" %% "elastic4s-
            core" % "5.0.0" 
    
  2. If you are using Maven, to enable the compilation in your 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> 
    
  3. To use the library, we need to import client classes and implicits:
            import com.sksamuel.elastic4s.{ElasticClient, 
            ElasticsearchClientUri} 
            import com.sksamuel.elastic4s.ElasticDsl._ 
    
  4. Now we can initialize the client, providing an Elasticsearch URI:
            object ClientSample extends App { 
              val uri =  
              ElasticsearchClientUri("elasticsearch://127.0.0.1:9300?  
              cluster.name=elasticsearch") 
              val client = ElasticClient.transport(uri) 
    
  5. To index a document, we execute 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 
    
  6. Now we can search for the document we indexed earlier:
            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"}}]}}) 
    

How it works...

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:

  • Multiple server endpoints, separated by commas (that is, elasticsearch://localhost:9300,boo:9876)
  • The other settings to be provided to the transporter with query arguments (that is, ?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:

  • Via 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) 
  • By providing a simple host and port:
        val client = ElasticClient.transport("127.0.0.1", 9300) 
  • By providing custom settings, ElasticsearchClientUri, and a list of plugin classes to the advanced customization:
        val client = ElasticClient.transport(settings, uri,  
        classOf[ReindexPlugin], classOf[ReindexPlugin]) 
  • Via an already defined Elasticsearch node:
        val node=... 
        val client = ElasticClient.fromNode(node) 
  • Via an already defined Elasticsearch client:
        val client = ElasticClient.fromClient(elasticSearchJavaClient) 

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.112.90