ElasticSearch7.x - Java API 操作

pom文件

<dependencies>
    <dependency>
        <groupId>org.elasticsearchgroupId>
        <artifactId>elasticsearchartifactId>
        <version>7.8.0version>
    dependency>
    
    <dependency>
        <groupId>org.elasticsearch.clientgroupId>
        <artifactId>elasticsearch-rest-high-level-clientartifactId>
        <version>7.8.0version>
    dependency>
    
    <dependency>
        <groupId>org.apache.logging.log4jgroupId>
        <artifactId>log4j-apiartifactId>
        <version>2.8.2version>
    dependency>
    <dependency>
        <groupId>org.apache.logging.log4jgroupId>
        <artifactId>log4j-coreartifactId>
        <version>2.8.2version>
    dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.coregroupId>
        <artifactId>jackson-databindartifactId>
        <version>2.9.9version>
    dependency>
    
    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>4.12version>
    dependency>
dependencies>

获取连接

// 创建客户端对象
RestHighLevelClient client = new RestHighLevelClient(
    RestClient.builder(new HttpHost("192.168.254.101", 9200, "http"))
);
...
// 关闭客户端连接
client.close();

索引操作

  1. 创建索引

    // 创建索引 - 请求对象
    CreateIndexRequest request = new CreateIndexRequest("user");
    // 发送请求,获取响应
    CreateIndexResponse response = client.indices().create(request, 
                                                           RequestOptions.DEFAULT);
    boolean acknowledged = response.isAcknowledged();
    // 响应状态
    System.out.println("操作状态 = " + acknowledged);
    
  2. 查看索引

    // 查询索引 - 请求对象
    GetIndexRequest request = new GetIndexRequest("user");
    // 发送请求,获取响应
    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());
    
  3. 删除索引

    // 删除索引 - 请求对象
    DeleteIndexRequest request = new DeleteIndexRequest("user");
    // 发送请求,获取响应
    AcknowledgedResponse response = client.indices().delete(request, 
    RequestOptions.DEFAULT);
    // 操作结果
    System.out.println("操作结果 : " + response.isAcknowledged());
    

文档操作

  1. 新增文档

    创建数据模型

    class User {
          
        private String name; 
        private Integer age; 
        private String sex; 
    
        public String getName() {
          
            return name; 
        } 
        public void setName(String name) {
         
            this.name = name; 
        } 
        public Integer getAge() {
         
            return age; 
        } 
        public void setAge(Integer age) {
         
            this.age = age; 
        } 
        public String getSex() {
          
            return sex; 
        } 
        public void setSex(String sex) {
          
            this.sex = sex; 
        } 
    }
    

    创建数据,添加到文档中

    // 新增文档 - 请求对象
    IndexRequest request = new IndexRequest();
    // 设置索引及唯一性标识
    request.index("user").id("1001");
    // 创建数据对象
    User user = new User();
    user.setName("zhangsan");
    user.setAge(30);
    user.setSex("男");
    ObjectMapper objectMapper = new ObjectMapper();
    String productJson = objectMapper.writeValueAsString(user);
    // 添加文档数据,数据格式为 JSON 格式
    request.source(productJson,XContentType.JSON);
    // 客户端发送请求,获取响应对象
    IndexResponse res

你可能感兴趣的:(ElasticSearch,java,elasticsearch)