ElasticSerach(四)

今天主要介绍ES API 

1、准备工作

1.1、创建 maven 工程引入依赖

复制代码
        <!--Java操作ES的客户端工具Jest-->
        <dependency>
            <groupId>io.searchbox</groupId>
            <artifactId>jest</artifactId>
            <version>5.3.3</version>
        </dependency>

        <!--Jest需要的依赖-->
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <version>4.5.2</version>
        </dependency>
        <!--Jest需要的依赖-->
        <dependency>
            <groupId>org.codehaus.janino</groupId>
            <artifactId>commons-compiler</artifactId>
            <version>3.0.16</version>
        </dependency>

        <!-- ElasticSearch依赖 -->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>6.6.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-to-slf4j</artifactId>
            <version>2.11.0</version>
        </dependency>
复制代码

1.2、编写方法获取 jest 连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//声明 jest 客户端工厂
private var jestFactory: JestClientFactory = null;
 
def build() = {
  jestFactory = new JestClientFactory
  jestFactory.setHttpClientConfig(new HttpClientConfig
  .Builder("http://hadoop201:9200") //ES 连接地址
    .multiThreaded(true) //开启多线程处理
    .maxTotalConnection(200) //对大连接数
    .connTimeout(10000) //链接等待时间
    .readTimeout(10000) //操作等待时间
    .build()
  )
}
 
//获取客户端
def getJestClient(): JestClient = {
  //如果连接工厂为空,调用 build() 创建工厂,否则直接返回对象
  if (jestFactory == null) {
    //创建客户端工厂对象
    build();
  }
  jestFactory.getObject
}

2、ES 插入数据

2.1、方式1 直接使用插入语句进行插入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// ES 插入数据 方式一
 def putIndex() = {
   //获取客户端链接
   val client: JestClient = getJestClient()
   //定义执行的 source
   val source: String =
     """{
       |  "id":101,
       |  "name":"peration meigong river",
       |     "doubanScore": 8.1,
       |   "actorList":
       |   [
       |     {"id":1,
       |       "name":"liu yi fei"
       |     }]
       |}""".stripMargin
   //创建插入的index,Bulider 的参数表示要插入的文档对象,底层会转换为 JSON 对象,也可以传入封装后的眼里类对象
   val index: Index = new Index.Builder(source)
     .index("movie_index")
     .`type`("movie")
     .id("4")
     .build()
 
   //使用客户端对象操作ES,execute 的参数是 Action 类型,Index 是 Action  的实现类
   client.execute(index)
 
   //关闭链接
   client.close()
 }

2.2、方式2 封装眼里类对象进行插入

定义样例类

1
2
//定义样例类
case class Movie(id: Long, name: String, doubanScore: Float, actorList: util.List[util.Map[String, Object]]) {

实现插入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// ES 插入数据 方式二,封装眼里类对象 插入文档
 def putIndex1() = {
   //获取客户端链接
   val client: JestClient = getJestClient()
   val actorList = new util.ArrayList[util.Map[String, Object]]()
   val actorMap = new util.HashMap[String, Object]()
   actorMap.put("id", "01")
   actorMap.put("name", "殷桃")
   actorList.add(actorMap)
   //封装成样例类对象
   val movie: Movie = Movie(102, "人世间", 9.5f, actorList)
 
   //创建Action 实现类 Index
   val index: Index = new Index.Builder(movie) //放入样例类对象
     .index("movie_index")
     .`type`("movie")
     .id("5")
     .build()
 
 
   client.execute(index)
 
   //关闭链接
   client.close()
 }

3、查询文档

3.1、根据ID查询

1
2
3
4
5
6
7
8
9
10
11
12
13
// 根据ID 查询数据
def queryById() = {
  //获取客户端链接
  val client: JestClient = getJestClient()
  //设置需要查询的 index & id
  val get: Get = new Get.Builder("movie_index", "5").build()
  //获取返回值对象
  val result: DocumentResult = client.execute(get)
  //此处直接通过返回值对象的 getJsonString 方法进行输出
  println(result.getJsonString)
  //关闭连接
  client.close()
}

3.2、查询多个文档 方式1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//   查询多个文档数据 方式1
def queryDos() = {
  //获取客户端链接
  val client: JestClient = getJestClient()
  val query: String =
    """
      |{
      |  "query": {
      |    "match": {
      |      "name": ""
      |    }
      |  },
      |  "sort": [
      |    {
      |      "doubanScore": {
      |        "order": "desc"
      |      }
      |    }
      |  ]
      |}
      |""".stripMargin
  //封装 search 对象
  val search: Search = new Search.Builder(query)
    .addIndex("movie_index")
    .build()
  val result: SearchResult = client.execute(search)
  val list: util.List[SearchResult#Hit[util.Map[String, Any], Void]] = result.getHits(classOf[util.Map[String, Any]])
  //将 java list  转为 scala 集合
  import scala.collection.JavaConverters._
  val list1: List[util.Map[String, Any]] = list.asScala.map(_.source).toList
  println(list1.mkString("\n"))
 
  //关闭连接
  client.close()
}

3.3、查询多个文档 方式2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//   查询多个文档数据 方式2
def queryDos1() = {
  //获取客户端链接
  val client: JestClient = getJestClient()
  //用于构建查询JSONM格式字符串
  val searchSourceBuilder: SearchSourceBuilder = new SearchSourceBuilder
  val boolQueryBuilder: BoolQueryBuilder = new BoolQueryBuilder()
  boolQueryBuilder.must(new MatchQueryBuilder("name", "人"))
  boolQueryBuilder.filter(new TermQueryBuilder("actorList.name", "殷桃"))
 
  searchSourceBuilder.query(boolQueryBuilder)
  searchSourceBuilder.from(0)
  searchSourceBuilder.size(1)
  searchSourceBuilder.sort("doubanScore", SortOrder.DESC)
 
  searchSourceBuilder.highlighter(new HighlightBuilder().field("name"))
 
  val query: String = searchSourceBuilder.toString()
  //  println(query)
 
  //封装 search 对象
  val search: Search = new Search.Builder(query)
    .addIndex("movie_index")
    .build()
  val result: SearchResult = client.execute(search)
  val list: util.List[SearchResult#Hit[util.Map[String, Any], Void]] = result.getHits(classOf[util.Map[String, Any]])
  //将 java list  转为 scala 集合
  import scala.collection.JavaConverters._
  val list1: List[util.Map[String, Any]] = list.asScala.map(_.source).toList
  println(list1.mkString("\n"))
 
  //关闭连接
  client.close()
}

 

posted @   晓枫的春天  阅读(45)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
历史上的今天:
2020-05-01 SpringBoot org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): mapper.xxxMapper.xxx() 错误解决
2020-05-01 IDEA 创建 MAPPER 模板
2020-05-01 SpringBoot 中 jdbctemplate 的使用
点击右上角即可分享
微信分享提示