上篇谈到:elasticsearch本身是一个完整的后台系统,对其的操作使用是通过终端api进行的。elasticsearch本身提供了多种编程语言的api,包括java的esjava。而elastic4s是一套基于esjava之上的scala api。

先看看scala 终端 ElasticClient的构建过程:

  1. import com.sksamuel.elastic4s.ElasticDsl._
  2. val esjava = JavaClient(ElasticProperties("http://localhost:9200"))
  3. val client = ElasticClient(esjava)

先构建JavaClient,JavaClient包嵌了个esjava的RestClient进行具体的操作:

  1. class JavaClient(client: RestClient) extends HttpClient {
  2. ...
  3. //send request to elasticsearch
  4. override def send(req: ElasticRequest, callback: Either[Throwable, HttpResponse] => Unit): Unit = {
  5. if (logger.isDebugEnabled) {
  6. logger.debug("Executing elastic request {}", Show[ElasticRequest].show(req))
  7. }
  8.  
  9. val l = new ResponseListener {
  10. override def onSuccess(r: org.elasticsearch.client.Response): Unit = callback(Right(fromResponse(r)))
  11. override def onFailure(e: Exception): Unit = e match {
  12. case re: ResponseException => callback(Right(fromResponse(re.getResponse)))
  13. case t => callback(Left(JavaClientExceptionWrapper(t)))
  14. }
  15. }
  16.  
  17. val request = new Request(req.method, req.endpoint)
  18. req.params.foreach { case (key, value) => request.addParameter(key, value) }
  19. req.entity.map(apacheEntity).foreach(request.setEntity)
  20. //perform actual request sending
  21. client.performRequestAsync(request, l)
  22. }
  23. ...
  24. }

上面这个RestClient即是elasticsearch提供的javaClient。而elastic4s的具体操作是通过RestClient.performRequestAsync进行的,如下:

  1. public class RestClient implements Closeable {
  2. ...
  3. /**
  4. * Sends a request to the Elasticsearch cluster that the client points to.
  5. * The request is executed asynchronously and the provided
  6. * {@link ResponseListener} gets notified upon request completion or
  7. * failure. Selects a host out of the provided ones in a round-robin
  8. * fashion. Failing hosts are marked dead and retried after a certain
  9. * amount of time (minimum 1 minute, maximum 30 minutes), depending on how
  10. * many times they previously failed (the more failures, the later they
  11. * will be retried). In case of failures all of the alive nodes (or dead
  12. * nodes that deserve a retry) are retried until one responds or none of
  13. * them does, in which case an {@link IOException} will be thrown.
  14. *
  15. * @param request the request to perform
  16. * @param responseListener the {@link ResponseListener} to notify when the
  17. * request is completed or fails
  18. */
  19. public void performRequestAsync(Request request, ResponseListener responseListener) {
  20. try {
  21. FailureTrackingResponseListener failureTrackingResponseListener = new FailureTrackingResponseListener(responseListener);
  22. InternalRequest internalRequest = new InternalRequest(request);
  23. performRequestAsync(nextNodes(), internalRequest, failureTrackingResponseListener);
  24. } catch (Exception e) {
  25. responseListener.onFailure(e);
  26. }
  27. }
  28. ...
  29.  
  30. }

另外,ElasticProperties是一个javaClient与ES连接的参数结构,包括IP地址:

  1. /**
  2. * Contains the endpoints of the nodes to connect to, as well as connection properties.
  3. */
  4. case class ElasticProperties(endpoints: Seq[ElasticNodeEndpoint], options: Map[String, String] = Map.empty)

ElasticProperties包含了ES地址ElasticNodeEndPoint及其它连接参数(如果需要的话),如下:

  1. it should "support prefix path with trailing slash" in {
  2. ElasticProperties("https://host1:1234,host2:2345/prefix/path/") shouldBe
  3. ElasticProperties(Seq(ElasticNodeEndpoint("https", "host1", , Some("/prefix/path")), ElasticNodeEndpoint("https", "host2", , Some("/prefix/path"))))
  4. }

当elastic4s完成了与elasticsearch的连接之后,就可以把按ES要求组合的Json指令发送到后台ES去执行了。elastic4s提供了一套DSL, 一种嵌入式语言,可以帮助用户更方便的用编程模式来组合ES的指令Json。当然,用户也可以直接把字符类的Json直接通过ElasticClient发送到后台ES。下面是一个简单可以运行的elastic4s示范:

  1. import com.sksamuel.elastic4s.http.JavaClient
  2. import com.sksamuel.elastic4s.requests.common.RefreshPolicy
  3. import com.sksamuel.elastic4s.{ElasticClient, ElasticProperties}
  4.  
  5. object HttpClientExampleApp extends App {
  6.  
  7. // you must import the DSL to use the syntax helpers
  8. import com.sksamuel.elastic4s.ElasticDsl._
  9. val esjava = JavaClient(ElasticProperties("http://localhost:9200"))
  10. val client = ElasticClient(esjava)
  11.  
  12. client.execute {
  13. bulk(
  14. indexInto("books" ).fields("title" -> "重庆火锅的十种吃法", "content" -> "在这部书里描述了火锅的各种烹饪方式"),
  15. indexInto("books" ).fields("title" -> "中国火锅大全", "content" -> "本书是全国中式烹饪中有关火锅的各种介绍")
  16. ).refresh(RefreshPolicy.WaitFor)
  17. }.await
  18.  
  19. val json =
  20. """
  21. |{
  22. | "query" : {
  23. | "match" : {"title" : "火锅"}
  24. | }
  25. |}
  26. |""".stripMargin
  27. val response = client.execute {
  28. search("books").source(json) // .matchQuery("title", "火锅")
  29. }.await
  30.  
  31. // prints out the original json
  32. println(response.result.hits.hits.head.sourceAsString)
  33.  
  34. client.close()
  35.  
  36. }

search(2)- elasticsearch scala终端:elastic4s的更多相关文章

  1. elastic search book [ ElasticSearch book es book]

    谁在使用ELK 维基百科, github都使用 ELK (ElasticSearch es book) ElasticSearch入门 Elasticsearch入门,这一篇就够了==>http ...

  2. ElasticSearch、Logstash、Kibana 搭建高效率日志管理系统

    ELK (ElasticSearch.LogStash以及Kibana)三者组合是一个非常强大的工具,这里我们来实现监控日志文件并且收到日志到ElasticSearch搜索引擎,利用Kibana可视化 ...

  3. ElasticSearch大数据分布式弹性搜索引擎使用

    阅读目录: 背景 安装 查找.下载rpm包 .执行rpm包安装 配置elasticsearch专属账户和组 设置elasticsearch文件所有者 切换到elasticsearch专属账户测试能否成 ...

  4. Elasticsearch问题总结

    1.ES大量做FULL GC,日志如下: [2016-12-15 14:53:21,496][WARN ][monitor.jvm ] [vsp4] [gc][old][94725][4389] du ...

  5. Elasticsearch(入门篇)——Query DSL与查询行为

    ES提供了丰富多彩的查询接口,可以满足各种各样的查询要求.更多内容请参考:ELK修炼之道 Query DSL结构化查询 Query DSL是一个Java开源框架用于构建类型安全的SQL查询语句.采用A ...

  6. scala 学习心得

    scala 安装步骤 文件下载地址:www.scala-lang.org(Please report bugs at https://issues.scala-lang.org/. We welcom ...

  7. ElasticSearch大数据分布式弹性搜索引擎使用—从0到1

    阅读目录: 背景 安装 查找.下载rpm包 .执行rpm包安装 配置elasticsearch专属账户和组 设置elasticsearch文件所有者 切换到elasticsearch专属账户测试能否成 ...

  8. [Elasticsearch] 控制相关性 (一) - 后面的相关度分值理论计算

    从第一章翻译Elasticsearch官方指南Controlling Relevance一章. 控制相关度(Controlling Relevance) 对于仅处理结构化数据(比方日期.数值和字符枚举 ...

  9. 解剖 Elasticsearch 集群 - 之三

    解剖 Elasticsearch 集群 - 之三 本篇文章是一系列涵盖 Elasticsearch 底层架构和原型示例的其中一篇.在本篇文章中,我们会讨论 Elasticsearch 如何提供准实时搜 ...

随机推荐

  1. 安装centos7后不能联网

    我们在安装centos的minimal版本后,在使用yum安装工具时会提示:cannot find a valid baseurl or repo:base/7/x86_64 这是因为不能联网导致的, ...

  2. Mybatis 常见面试题

    1.什么是Redis?简述它的优缺点? Redis本质上是一个Key-Value类型的内存数据库,很像memcached,整个数据库统统加载在内存当中进行操作,定期通过异步操作把数据库数据flush到 ...

  3. RxJava操作符实践:8_算术和聚合操作之3_min

    发射原始Observable的最小值. Min操作符操作一个发射数值的Observable并发射单个值:最小的那个值. RxJava中,min属于rxjava-math模块. min接受一个可选参数, ...

  4. Mybatis 入门之resultMap与resultType讲解实例

    resultMap:适合使用返回值是自定义实体类的情况 resultType:适合使用返回值得数据类型是非自定义的,即jdk的提供的类型 resultMap : type:映射实体类的数据类型 id: ...

  5. <JZOJ4726>种花

    挺有意思的贪心 神奇的贪心 #include<cstdio> #include<iostream> #include<cstring> #include<al ...

  6. 吴裕雄--天生自然 人工智能机器学习实战代码:LASSO回归

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model from s ...

  7. Spring Cloud Alibaba-MyShop-项目介绍

    本节视频 [视频]Spring Cloud Alibaba-MyShop-项目介绍 开发环境 操作系统:Windows 10 Enterprise 开发工具:Intellij IDEA 数据库:MyS ...

  8. mac 下openOffice服务的安装

    1.安装准备 安装 Homebrew 及 Homebrew-Cask Homebrew 是一个Mac上的包管理工具.使用Homebrew可以很轻松的安装缺少的依赖. Homebrew-Cask是建立在 ...

  9. 直播问答App乃虚火,调侃知识终不能长久盈利

    ​ 随着王思聪在微博宣布"我.我乐意",一款叫"冲顶大会"的App冲到了大众面前,紧接着"芝士超人"携10亿元奖金从天而降,瞬间之内,在线答 ...

  10. linux 的uuid码

    在提到这个之前,有个概念,就是什么是uuid呢? UUID码全称是通用唯一识别码 (Universally Unique Identifier, UUID),它 是一个软件建构的标准,亦为自由软件基金 ...