如何使用 Scala 客户端开始使用 Elastic Search
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27203498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get started with Elastic Search using scala client
提问by swaheed
Hi i am new in Elastic Search and i want to use it with scala so i found some codes examples on github, but there was very complex examples were given as for a beginner I spend my whole day in trying to understand this tutorialbut at the end i am confused how to start this is,its very complex to understand same as with other Scala client examples
嗨,我是 Elastic Search 的新手,我想将它与 Scala 一起使用,所以我在 github 上找到了一些代码示例,但是对于初学者来说,给出了非常复杂的示例,我花了一整天的时间试图理解本教程,但是在最后我很困惑如何开始这是,它的理解与其他 Scala 客户端示例相同非常复杂
- https://github.com/scalastuff/esclient
- https://github.com/bsadeh/scalastic
- https://github.com/gphat/wabisabialso i tried this but it contains error and i posted it here as well https://stackoverflow.com/questions/27145015/scalagetstatuscode-getresponsebody-is-not-a-member-of-dispatch-future
- https://github.com/scalastuff/esclient
- https://github.com/bsadeh/scalastic
- https://github.com/gphat/wabisabi我也试过了,但它包含错误,我也在这里发布了https://stackoverflow.com/questions/27145015/scalagetstatuscode-getresponsebody-is-not-a-member-未来派送
All these examples are very complex for a new learner like me as i go through first chapter of Elastic Search from its guidethen I want to do these same things pro-grammatically with Scala.Please suggest me some starting point from where can i start learning and also there is a request do not mark this question as nonconstructive first i tried myself after then i am posting this question,Please i need help i want to learn elastic search using scala
所有这些示例对于像我这样的新学习者来说都非常复杂,因为我从其指南中阅读了 Elastic Search 的第一章,然后我想用 Scala 以编程方式做这些相同的事情。请建议我从哪里开始学习的一些起点并且还有一个请求,请不要先将此问题标记为非建设性问题,然后我尝试了自己,然后我发布了此问题,请我需要帮助,我想使用 Scala 学习弹性搜索
回答by monkHyman
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.
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)
}
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.
如果这太复杂,那不是因为 Scala 客户端太复杂,而是因为您对 Elasticsearch 或 Scala 还不够了解。您正在查看的 Scala 客户端是一个典型的 DSL,因此它使用了一些 Scala 技巧,可以很好地用作客户端,但在幕后不一定容易理解。
Here are some good links to understanding Elasticsearch:
这里有一些很好的链接来理解 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
- 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.
在使用任何 Scala 驱动程序之前,您至少应该了解索引/类型、查询 DSL 以及节点在 Elasticsearch 中的基本概念。查看您可以使用 HTTP 接口发送的 JSON 也可能会有所帮助,因为这样更容易了解发生了什么,因为 Elasticsearch 文档一开始可能很繁重。
回答by Nagaraj Vittal
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:
带有基本身份验证的 Scala 代码到 ES:
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

![在 Scala 中将元素添加到 Seq[String]](/res/img/loading.gif)