当前位置:   article > 正文

RestHighLevelClient的简单使用

resthighlevelclient

目录

一、引入依赖

<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>7.8.0</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.8.0</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

二、代码

1、添加索引结构到es
void addIndex() throws Exception {
    /**
     * GET /user
     * 作用:可以看到aliases、mappings、settings
     * 说明:我们下面使用的是直接定死的结构,而SpringBoot中使用的是注解的方式来创建索引结构,最终的原理都是下面的代码
     * {
     *     "user": {
     *         "aliases": {
     *             "user.aliases": {}
     *         },
     *         "mappings": {
     *             "properties": {
     *                 "age": {
     *                     "type": "integer"
     *                 },
     *                 "name": {
     *                     "type": "text",
     *                     "fields": {
     *                         "keyword": {
     *                             "type": "keyword"
     *                         }
     *                     }
     *                 },
     *                 "sex": {
     *                     "type": "keyword"
     *                 }
     *             }
     *         },
     *         "settings": {
     *             "index": {
     *                 "creation_date": "1649243890532",
     *                 "number_of_shards": "9",
     *                 "number_of_replicas": "2",
     *                 "uuid": "EPChtL_vQj2gHJbO5VTHqg",
     *                 "version": {
     *                     "created": "7060099"
     *                 },
     *                 "provided_name": "user"
     *             }
     *         }
     *     }
     * }
     */
    // 创建ES客户端对象
    RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
    // 定义索引名称
    CreateIndexRequest request = new CreateIndexRequest("user");
    // 添加aliases,对比上述结构来理解
    String aliaseStr = "{"user.aliases":{}}";
    Map aliases = JSONObject.parseObject(aliaseStr, Map.class);
    // 添加mappings,对比上述结构来理解
    String mappingStr = "{"properties":{"name":{"type":"text","fields":{"keyword":{"type":"keyword"}}},"sex":{"type":"keyword"},"age":{"type":"integer"}}}";
    Map mappings = JSONObject.parseObject(mappingStr, Map.class);
    // 添加settings,对比上述结构来理解
    String settingStr = "{"index":{"number_of_shards":"9","number_of_replicas":"2"}}";
    Map settings = JSONObject.parseObject(settingStr, Map.class);

    // 添加数据
    request.aliases(aliases);
    request.mapping(mappings);
    request.settings(settings);

    // 发送请求到ES
    CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
    // 处理响应结果
    System.out.println("添加索引是否成功:" + response.isAcknowledged());
    // 关闭ES客户端对象
    client.close();
}
  • 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
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
2、获取索引信息
void getIndexInfo() throws Exception {
    // 创建ES客户端对象
    RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
    // 定义索引名称
    GetIndexRequest request = new GetIndexRequest("user");
    // 发送请求到ES
    GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);
    // 处理响应结果
    System.out.println("aliases:" + response.getAliases());
    System.out.println("mappings:" + response.getMappings());
    System.out.println("settings:" + response.getSettings());
    // 关闭ES客户端对象
    client.close();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
3、删除索引
void deleteIndex() throws Exception {
    // 创建ES客户端对象
    RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
    // 定义索引名称
    DeleteIndexRequest request = new DeleteIndexRequest("user");
    // 发送请求到ES
    AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
    // 处理响应结果
    System.out.println("删除是否成功:" + response.isAcknowledged());
    // 关闭ES客户端对象
    client.close();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
4、添加数据到索引中
void insertDataToIndex() throws Exception {
    // 1、创建ES客户端对象
    RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
    // 2、创建请求对象
    User user = new User();
    user.setName("张三");
    user.setSex("男");
    user.setAge(22);
    // 定义请求对象
    IndexRequest request = new IndexRequest("user");
    // 设置文档id
    request.id("1000");
    // 将json格式字符串放在请求中
    request.source(JSONObject.toJSONString(user), XContentType.JSON);
    // 3、发送请求到ES
    IndexResponse response = client.index(request, RequestOptions.DEFAULT);
    // 4、处理响应结果
    System.out.println("数据插入结果:" + response.getResult());
    // 5、关闭ES客户端对象
    client.close();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
5、局部更新索引中的数据
void updateDataFromIndex() throws Exception {
    // 1、创建ES客户端对象
    RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
    // 2、定义请求对象
    User user = new User();
    user.setName("小美");
    user.setAge(20);
    UpdateRequest request = new UpdateRequest();
    request.index("user").id("1000");
    // 拓展:局部更新也可以这样写:request.doc(XContentType.JSON, "name", "李四", "age", 25);,其中"name"和"age"是User对象中的字段名称,而"小美"和20是对应的字段值
    request.doc(JSONObject.toJSONString(user), XContentType.JSON);
    // 3、发送请求到ES
    UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
    // 4、处理响应结果
    System.out.println("数据更新结果:" + response.getResult());
    // 5、关闭ES客户端对象
    client.close();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
6、根据文档id查询索引中的数据
void getDataById() throws Exception {
    // 1、创建ES客户端对象
    RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
    // 2、定义请求对象
    GetRequest request = new GetRequest("user");
    request.id("1000");
    // 3、发送请求到ES
    GetResponse response = client.get(request, RequestOptions.DEFAULT);
    // 4、处理响应结果
    System.out.println("查询结果:" + response.getSourceAsString());
    // 5、关闭ES客户端对象
    client.close();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
7、根据文档id删除索引中的数据
void deleteDataById() throws Exception {
    // 1、创建ES客户端对象
    RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
    // 2、定义请求对象
    DeleteRequest request = new DeleteRequest("user");
    request.id("1000");
    // 3、发送请求到ES
    DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
    // 4、处理响应结果
    System.out.println("删除是否成功:" + response.getResult());
    // 5、关闭ES客户端对象
    client.close();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
8、根据查询条件删除索引中的数据
void deleteDataByQuery() throws IOException {
    // 1、创建ES客户端对象
    RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
    // 2、定义请求对象
    DeleteByQueryRequest request = new DeleteByQueryRequest("user");
    request.setQuery(QueryBuilders.matchAllQuery());
    // 3、发送请求到ES
    BulkByScrollResponse response = client.deleteByQuery(request, RequestOptions.DEFAULT);
    // 4、处理响应结果
    System.out.println("删除失败结果:" + response.getBulkFailures());
    // 5、关闭ES客户端对象
    client.close();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
9、批量插入数据到索引中
void batchInsertDataToIndex() throws Exception {
    // 1、创建ES客户端对象
    RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
    // 2、定义请求对象
    // 准备测试数据
    List<User> userList = new ArrayList<>(10);
    for (int i = 0; i < 10; i++) {
        User user = new User();
        user.setName(i % 2 == 0 ? "张三" + i : "小美" + i);
        user.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/68669
推荐阅读
相关标签
  

闽ICP备14008679号