通过hotel-demo演示RestClient操作Elasticsearch

指南https://www.bilibili.com/read/cv11763184

一、 什么是RestClient

ES官方提供了各种不同语言的客户端,用来操作ES。这些客户端的本质就是组装DSL语句,通过http请求发送给ES。官方文档地址:https://www.elastic.co/guide/en/elasticsearch/client/index.html

本套视频教程所有配套资料索取方式如下:
关注微信公众号:黑马程序员,回复关键词:领取资源02
还可获得1000+G全套自学java资源,
学习q群535894053,告别孤单,共同进步!
2021版java最新最全学习路线图
2022黑马程序员Java学习路线图
如何下载资料
黑马程序员教程资源下载指南

二、示例

1、准备课前资料

在这里插入图片描述
tb_hotel.sql和hotel-demo可以通过该地址下载

2、创建索引

注意all字段用了字段拷贝copy是为了将多个关键词合并到一个字段进行模糊搜索

# 创建索引库
PUT /hotel
{
   
  "mappings": {
   
    "properties": {
   
      "id":{
   
        "type": "keyword"
      },
      "name":{
   
        "type": "text",
        "analyzer": "ik_max_word",
        "copy_to": "all"
      },
      "address":{
   
        "type": "keyword",
        "index": false
      },
      "price":{
   
        "type":"keyword"
      },
      "score":{
   
        "type":"keyword"
      },
      "brand":{
   
        "type":"keyword",
        "copy_to": "all"
      },
      "city":{
   
        "type":"keyword"
      },
      "starName":{
   
        "type":"keyword"
      },
      "business":{
   
        "type":"keyword",
        "copy_to": "all"
      },
      "location":{
   
        "type":"geo_point"
      },
      "pic":{
   
        "type":"keyword",
        "index": false
      },
      "all":{
   
        "type": "text",
        "analyzer": "ik_max_word"
      }
    }
  }
}

3、初始化JavaRestClient

3.1、引入es的RestHighLevelClient

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>

3.2、因为springBoot默认ES版本是7.6.2,所以我们要覆盖默认版本

<properties>
    <java.version>1.8</java.version>
    <elasticsearch.version>7.15.1</elasticsearch.version>
</properties>

3.3、初始化RestHighLevelClient

RestHighLevelClient client = new RestHighLevelClient(
               RestClient.builder(
                       new HttpHost("192.168.0.130",9200,"http")));
// 或者
RestHighLevelClient client = new RestHighLevelClient(
              RestClient.builder(
                      HttpHost.create("http://192.168.0.130:9200)));

测试类中

/**
 * 初始化客户端
 * 每个测试方法执行前执行
 */
@BeforeEach
public void initClient(){
   
    client = new RestHighLevelClient(
            RestClient.builder(
                    new HttpHost("elasticsearch",9200,"http")));
}

/**
 * 关闭客户端
 * 每个测试方法执行完之后执行
 */
@AfterEach
public void closeClient(){
   
    try {
   
        client.close();
    } catch (IOException e) {
   
        e.printStackTrace();
    }
}

4、JavaRestClient操作

4.1、创建索引

在这里插入图片描述

  • HotelConstants
package cn.itcast.hotel.cn.itcast.hotel.constant;

public class HotelConstants {
   
    public static final String MAPPING_TEMPLATE = "{\n" +
            "  \"mappings\": {\n" +
            "    \"properties\": {\n" +
            "      \"id\": {\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"name\":{\n" +
            "        \"type\": \"text\",\n" +
            "        \"analyzer\": \"ik_max_word\",\n" +
            "        \"copy_to\": \"all\"\n" +
            "      },\n" +
            "      \"address\":{\n" +
            "        \"type\": \"keyword\",\n" +
            "        \"index\": false\n" +
            "      },\n" +
            "      \"price\":{\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "      \"score\":{\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "      \"brand\":{\n" +
            "        \"type\": \"keyword\",\n" +
            "        \"copy_to\": \"all\"\n" +
            "      },\n" +
            "      \"city\":{\n" +
            "        \"type\": \"keyword\",\n" +
            "        \"copy_to\": \"all\"\n" +
            "      },\n" +
            "      \"starName\":{\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"business\":{\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"location\":{\n" +
            "        \"type\": \"geo_point\"\n" +
            "      },\n" +
            "      \"pic\":{\n" +
            "        \"type\": \"keyword\",\n" +
            "        \"index\": false\n" +
            "      },\n" +
            "      \"all\":{\n" +
            "        \"type\": \"text\",\n" +
            "        \"analyzer\": \"ik_max_word\"\n" +
            "      }\n" +
            "    }\n" +
            "  }\n" +
            "}";
}

  • createIndex
// 添加索引库
@Test
 public void createIndex1() throws IOException {
   
     IndicesClient indicesClient = client.indices();
     CreateIndexRequest request = new CreateIndexRequest("hotel");
     //设置映射
     request.source(HotelConstants.MAPPING_TEMPLATE, XContentType.JSON);
     //发送hettp请求
     CreateIndexResponse response = indicesClient.create(request, RequestOptions.DEFAULT);
     //索引,名字
     System.out.println(response.index());


 }
  • 创建方法二
@Test
void createIndex2() throws IOException {
   
    // 创建索引
    CreateIndexRequest request = new CreateIndexRequest("hotel");
    // 添加设置
    request.settings(Settings.builder()
            .put("index.number_of_shards", 1)
            .put("index.number_of_replicas", 0)
    );
    // 添加映射

    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值