如何使用scala客户端开始使用Elastic Search

时间:2014-11-29 14:46:19

标签: scala elasticsearch elastic4s

嗨,我是Elastic Search的新手,我想在scala中使用它,所以我在github上找到了一些代码示例,但是有一个非常复杂的例子给了初学者我花了一整天时间来试图理解{{ 3}}但最后我很困惑如何开始这个,它与其他Scala客户端示例的理解相同非常复杂

  1. this tutorial
  2. https://github.com/scalastuff/esclient
  3. https://github.com/bsadeh/scalastic我也试过这个,但它包含错误,我也在这里发布了https://github.com/gphat/wabisabi
  4. 对于像我这样的新学习者来说,所有这些例子都非常复杂,因为我经历了https://stackoverflow.com/questions/27145015/scalagetstatuscode-getresponsebody-is-not-a-member-of-dispatch-future然后我想用Scala进行语法编写这些相同的事情。请给我一些起点,从哪里开始学习并且有一个请求不要将此问题标记为非建设性我先尝试自己,然后我发布此问题,请我需要帮助我想学习弹性搜索使用scala

2 个答案:

答案 0 :(得分:13)

Elastic4s项目在自述文件的顶部附近包含一个关于如何使用驱动程序的简单示例。这个例子是一个完整的Scala程序,你可以执行它。

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)

}

如果这太复杂了,那不是因为Scala客户端过于复杂,而是因为您还不了解Elasticsearch或Scala。您正在查看的Scala客户端是一个典型的DSL,因此它使用了一些Scala技巧,可以很好地用作客户端,但不一定容易理解。

以下是了解Elasticsearch的一些很好的链接:

在使用任何Scala驱动程序之前,您至少应该了解索引/类型的基本概念,查询DSL以及Elasticsearch中的节点。查看可以使用HTTP接口发送的JSON可能也会有所帮助,因为这样可以更容易地看到发生了什么,因为Elasticsearch文档最初可能很重要。

答案 1 :(得分:4)

简单的弹性搜索客户端

  <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>

具有基本身份验证的ES的scala代码:

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