score:4
Simple elastic search client
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.5.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.5.0</version>
</dependency>
Scala code to ES with basic auth:
import org.apache.http.HttpHost
import org.apache.http.auth.{AuthScope, Credentials, UsernamePasswordCredentials}
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions
import org.elasticsearch.client._
import org.apache.http.client.CredentialsProvider
import org.apache.http.impl.client.BasicCredentialsProvider
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder
import org.elasticsearch.client.RestClient
import org.elasticsearch.client.RestClientBuilder
val credentials = new UsernamePasswordCredentials("<username>", "<password>");
val credentialsProvider:CredentialsProvider = new BasicCredentialsProvider
credentialsProvider.setCredentials(AuthScope.ANY, credentials)
val client = RestClient.builder(new HttpHost("<host>", 9200,"https")).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
override def customizeHttpClient(httpClientBuilder: HttpAsyncClientBuilder): HttpAsyncClientBuilder = httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
}).build
val request = new Request(
"GET",
/_cat/aliases?format=JSON )
val response = client.performRequest(request);
println("Response:"+response.getEntity.getContent)
client.close
score:18
The Elastic4s project contains, near the top of the readme, a simple example on how to use the driver. This example is a complete Scala program that you can execute.
import com.sksamuel.elastic4s.ElasticClient
import com.sksamuel.elastic4s.ElasticDsl._
object Test extends App {
val client = ElasticClient.local
// await is a helper method to make this operation sync instead of async
// You would normally avoid doing this in a real program as it will block
client.execute { index into "bands/artists" fields "name"->"coldplay" }.await
val resp = client.execute { search in "bands/artists" query "coldplay" }.await
println(resp)
}
If this is too complicated, then that is not because the Scala client is too complicated, but because you don't yet understand enough about Elasticsearch or Scala. The Scala client you are looking at is a typical DSL so it uses some Scala tricks that make it nice to use as a client, but not necessarily easy to to understand under the covers.
Here are some good links to understanding Elasticsearch:
- http://spinscale.github.io/elasticsearch/2012-03-jugm.html#/20
- http://exploringelasticsearch.com/
- http://joelabrahamsson.com/elasticsearch-101/
- http://www.slideshare.net/karmi/your-data-your-search-elasticsearch-euruko-2011
Before you use any of the Scala drivers, you should at least understand the basic concepts of an index/type, the query DSL, and what a node is in Elasticsearch. It might also be helpful to look at the JSON that you can send with the HTTP interface as that is a bit easier to see what is going on, because the Elasticsearch docs can be heavy going at first.
Source: stackoverflow.com
Related Query
- how to get started with Elastic Search using scala client
- How to get rid of downcast using synchronous send with Scala Actors?
- How do I get all the keys that are stored in the Cassandra 2.0.1 column family with scala using hector?
- How do I get started with Scala
- How to get started with Akka Streams?
- How to update a mongo record using Rogue with MongoCaseClassField when case class contains a scala Enumeration
- How to load JSON file using Play with Scala
- How to get logging working in scala unit tests with testng, slf4s, and logback
- How to get Scala imports working in IntelliJ IDEA with the Play framework?
- How do I get Intellij IDEA 12.0 to work with Play Framework 2.1.0 app and Scala 2.10.0?
- How to implement breadth first search in Scala with FP
- How do I get started developing for a web using Scala?
- How to get creation date of a file using Scala
- How can I deserialize from JSON with Scala using *non-case* classes?
- How to open TCP connection with TLS in scala using akka
- Using vs code, how to get scala format to work and format my code?
- How to get ScalaTest correctly reporting tests results when using scalacheck with Propspec and PropertyCheck?
- How to get system IP address using in scala code?
- How to create new instance of Scala class with context bound via Java reflection using only zero argument constructor?
- Scala - how to get the 'type' for a field using reflection api
- How can I get random data generated for scala case classes with the ability to "change some values" for unit testing?
- Using type lambdas together with higher-kinded types in Scala: how to get compiler to infer types correctly?
- How to get the proper return type when using a filter based on type in Scala
- scala (scanLeft) - how to (functionally) get a Map with cumulated values / frequency
- How to Compile Apache Spark with Scala 2.11.1 using SBT?
- How to set resource requirements on a container with the fabric8 kubernetes Java & Scala client API
- How to get return type string of a method using Scala reflection?
- How to get the set of rows which contains null values from dataframe in scala using filter
- Scala - how come using a super-type with two generic parameters cause the scala type checker to treat the child-type differently?
- How to send Json from client with missing fields for its corresponding Case Class after using Json.format function
More Query from same tag
- Create dynamic function name on the fly in Scala
- Akka Typed testing - Expect message of type without knowing message details
- scala merge option sequences
- How do I add dependencies to my sbt file so that I can use apache.commons in my project?
- Issue accessing to DataSet in scala
- Spark column wise word count
- Difference between passing "*" and '*' arguments to split
- Pass array as separate arguments for case class
- For Comprehension Return type
- Copy multiple files from one folder to another
- Pattern matching on a shapeless type cast ok?
- Testing a utility function by writing a unit test in apache spark scala
- Random shuffle not working for Range
- Why map transformation is narrow?
- Why case class is named 'case'?
- How do add values of selective rows from a list in an functional style?
- How does type inference work for a selection with placeholder syntax?
- Slick MTable.getTables always fails with Unexpected exception[JdbcSQLException: Invalid value 7 for parameter columnIndex [90008-60]]
- How to know which Future fail when doing Future.sequence?
- Conditional invocation of a method in Scala
- State not persisted between requests
- Classify every instance of a RDD | Apache Spark Scala
- GUI creation in Scala but WITHOUT Swing/AWT
- Scala-redis subscribes to * but receives zero messages
- Get Simple Remote Akka Application Running
- How to calculate number of occurrence of a character at beginning in a List of String using Scala
- Lift Framework BindHelpers.attr Question (or better practice?)
- PySpark and Scala
- Scala Anorm zero to many the right way
- Spring finds a test class on a jar and I get a NoUniqueBeanDefinitionException