elasticsearch operation (API mode)

Note: In addition to using the commands that come with es to operate index libraries and documents (reference: http://t.csdn.cn/4zpmi), you can add related dependencies in IDEA and use the corresponding API to operate.

Preparation

Build a SpringBoot project, MyBatis-Plus used by DAO, to operate on the student table in the database.

insert image description here

pom.xml file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.10.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>org.hzy</groupId>
    <artifactId>es_essay_demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <elasticsearch.version>7.12.1</elasticsearch.version>
    </properties>

    <dependencies>
        <!--spring web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--mybatis-plus依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>

        <!--数据库连接驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!--lombok依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!--测试依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--FastJson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.71</version>
        </dependency>

        <!--commons依赖-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
    </dependencies>
</project>

On top of this, import the RestHighLevelClient dependency and specify the version, otherwise the version that comes with SpringBoot will be used, relying on this dependency to implement a series of operations on ES.

    <properties>
        <elasticsearch.version>7.12.1</elasticsearch.version>
    </properties>
    
	<!--RestHighLevelClient依赖-->
	<dependency>
	    <groupId>org.elasticsearch.client</groupId>
	    <artifactId>elasticsearch-rest-high-level-client</artifactId>
	</dependency>

Write the index library DSL statement related to the student class. Note that the creation date and enrollment date in the student class are deliberately combined into a joinInfo field, which needs to be taken into account when adding documents later;

PUT /student
{
    
    
  "mappings": {
    
    
    "properties": {
    
    
      "id": {
    
    
        "type": "keyword"
      },
        
      "username":{
    
    
        "type": "keyword",
        "copy_to": "all"
      },
        
      "password":{
    
    
        "type": "keyword",
        "index": false
      },
        
      "name":{
    
    
        "type": "text",
        "analyzer": "ik_max_word",
        "copy_to": "all"
      },

      "gender":{
    
    
        "type": "keyword"
      },
        
      "image":{
    
    
        "type": "keyword"
      },
        
      "job":{
    
    
        "type": "integer"
      },
	  
	  "joinInfo":{
    
    
		"type": "keyword"
	  },
        
      "updateTime":{
    
    
        "type": "keyword"
      },
        
        
      "all":{
    
    
        "type": "text",
        "analyzer": "ik_max_word"
      }
    }
  }
}

Store the DSL statement of the above index library with a constant in IDEA for later use. Note that the previous "PUT /student" needs to be removed, and only the information in the outermost curly braces is needed;

public class Constants {
    
    

    public static final String CREATE_INDEX_STRING = "{\n" +
            "  \"mappings\": {\n" +
            "    \"properties\": {\n" +
            "      \"id\": {\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "        \n" +
            "      \"username\":{\n" +
            "        \"type\": \"keyword\",\n" +
            "        \"copy_to\": \"all\"\n" +
            "      },\n" +
            "        \n" +
            "      \"password\":{\n" +
            "        \"type\": \"keyword\",\n" +
            "        \"index\": false\n" +
            "      },\n" +
            "        \n" +
            "      \"name\":{\n" +
            "        \"type\": \"text\",\n" +
            "        \"analyzer\": \"ik_max_word\",\n" +
            "        \"copy_to\": \"all\"\n" +
            "      },\n" +
            "\n" +
            "      \"gender\":{\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "        \n" +
            "      \"image\":{\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "        \n" +
            "      \"job\":{\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "\t  \n" +
            "\t  \"joinInfo\":{\n" +
            "\t\t\"type\": \"keyword\"\n" +
            "\t  },\n" +
            "        \n" +
            "      \"updateTime\":{\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "        \n" +
            "        \n" +
            "      \"all\":{\n" +
            "        \"type\": \"text\",\n" +
            "        \"analyzer\": \"ik_max_word\"\n" +
            "      }\n" +
            "    }\n" +
            "  }\n" +
            "}";
}

Index library (index) operations

For the convenience of subsequent operations, the definition, creation, and closure of RestHighLevelClient are placed outside the method, and the following operations are all implemented in the test class;

    /**
     * 定义连接
     */
    private RestHighLevelClient client;

    /**
     * 初始化客户端
     */
    @BeforeEach
    public void init(){
    
    
        client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.126.128:9200")));
    }

    /**
     * 关闭客户端
     * @throws IOException
     */
    @AfterEach
    public void close() throws IOException {
    
    
        client.close();
    }

Create an index library

    /**
     *  创建索引
     */
    @Test
    public void addIndex() throws IOException {
    
    
        // 1.创建请求
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("student");

        // 2.编写DSL语句
        createIndexRequest.source(CREATE_INDEX_STRING, XContentType.JSON);

        // 3.发起请求
        client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
    }

execution succeed

insert image description here

Go to ikbana to see that the index library has been created

insert image description here

get index library

    /**
     * 获取索引库
     * @throws IOException
     */
    @Test
    public void getIndex() throws IOException {
    
    
        // 1.创建请求
        GetIndexRequest getIndexRequest = new GetIndexRequest("student");

        // 2.发起请求
        boolean exists = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT);

        System.out.println("exists = " + exists);
    }

The result returned by this method is boolean type, only to know whether the index library exists;

insert image description here

delete index library

    /**
     * 删除索引库
     * @throws IOException
     */
    @Test
    public void deleteIndex() throws IOException {
    
    
        // 1. 创建请求
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("student");

        // 2. 发起请求
        client.indices().delete(deleteIndexRequest,RequestOptions.DEFAULT);
    }

Delete the index database successfully;

insert image description here

Get the index library again and return false;

insert image description here

summary

The index library has no modification operations

Document Operations

Again, the document operation is equivalent to the data operation of the database, that is, the CRUD operation of analog data;

new document

Note that because the records queried in the database are not in one-to-one correspondence with the fields of ES documents, another object needs to be created to piece them together;

(Student class)

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.io.Serializable;

@Data
@TableName("tb_student")
public class Student implements Serializable {
    
    
    private Integer id;
    private String username;
    private String password;
    private String name;
    private Integer gender;
    private String image;
    private Integer job;
    private String entryDate;
    private String createTime;
    private String updateTime;
}

(StudentDoc class, one-to-one correspondence with ES documents)

import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
public class StudentDoc implements Serializable {
    
    
    private Integer id;
    private String username;
    private String password;
    private String name;
    private Integer gender;
    private String image;
    private Integer job;
    private String joinInfo;
    private String updateTime;

    public StudentDoc(Student student) {
    
    
        this.id = student.getId();
        this.username = student.getUsername();
        this.password = student.getPassword();
        this.name = student.getName();
        this.gender = student.getGender();
        this.image = student.getImage();
        this.job = student.getJob();
        this.joinInfo = "[创建日期:" + student.getCreateTime() + "], [加入日期:" + student.getEntryDate() +"]";
        this.updateTime = student.getUpdateTime();
    }
}
    /**
     * 新增文档(数据来自数据库,然后新增到ES)
     */
    @Test
    public void addDoc() throws IOException {
    
    
        // 1.查询数据(ID为1的记录)
        Student student = studentService.getById(1);
		
		// 1.1 拼凑文档
        StudentDoc studentDoc = new StudentDoc(student);

        // 2.创建请求
        IndexRequest request = new IndexRequest("student").id(student.getId().toString());

        // 3.编写DSL语句
        request.source(JSON.toJSONString(studentDoc),XContentType.JSON);

        // 4.发送请求
        client.index(request,RequestOptions.DEFAULT);
    }

Execution completed;

insert image description here

View the kibana platform, enter the command, and you can find the corresponding document, indicating that the new document was successfully added;

insert image description here

query document

    /**
     * 查询文档
     */
    @Test
    public void getDoc() throws IOException {
    
    
        // 1.创建请求,查询ID为1的文档
        GetRequest request = new GetRequest("student").id("1");

        // 2.发送请求
        GetResponse response = client.get(request, RequestOptions.DEFAULT);

        // 3.获取返回值
        String json = response.getSourceAsString();

        // 4.解析返回值为java对象
        StudentDoc studentDoc = JSON.parseObject(json, StudentDoc.class);

        System.out.println("studentDoc = " + studentDoc);
    }

query complete;

insert image description here

delete document

    /**
     * 删除文档
     */
    @Test
    public void delDoc() throws IOException {
    
    
        // 1.创建请求,删除ID为1的文档
        DeleteRequest request = new DeleteRequest("student").id("1");

        // 2.发起请求
        client.delete(request, RequestOptions.DEFAULT);
    }

Execution completed;

insert image description here
Query again, the result is null (note, no error will be reported);

insert image description here

update document

There are two ways to update documents. The first is global update, which is actually adding new documents. The code is the same. The second is partial update. The code is as follows:

    /**
     * 更新文档(方式二:局部更新)
     */
    @Test
    public void updateDoc() throws IOException {
    
    
        // 1.创建请求,修改ID为1的文档
        UpdateRequest request = new UpdateRequest("student","1");

        // 2.指定要修改的字段
        request.doc("name","徐志摩",
                "username","xuzhimo");

        // 3.发送请求
        client.update(request,RequestOptions.DEFAULT);
    }

Execution completed;

insert image description here
Find the document with ID 1 again, and you can see that the document has been updated;

insert image description here

Batch import documents

Import object data in the database to ES in batches;

    /**
     * 批量导入文档(数据从数据库中查询获取)
     */
    @Test
    public void addDocs() throws IOException {
    
    
        // 1.获取所有学生的数据
        List<Student> students = studentService.list();

        // 2.创建Bulk请求
        BulkRequest bulkRequest = new BulkRequest();

        // 3.添加数据到Bulk中
        for (Student student : students) {
    
    
            // 3.1 创建Doc对象
            StudentDoc studentDoc = new StudentDoc(student);

            // 3.2 创建新增文档请求并将Doc对象放入
            bulkRequest.add(new IndexRequest("student").id(studentDoc.getId().toString())
                    .source(JSON.toJSONString(studentDoc),XContentType.JSON));
        }

        // 4.发送请求
        client.bulk(bulkRequest,RequestOptions.DEFAULT);
    }

Execution completed without error;

insert image description here

Just search for two records, you can see that they have been added;

insert image description here
insert image description here

Summarize

For the operation of the index library, the request is XxxIndexRequest ("index library name"), the creation is Create, the deletion is Delete, and the acquisition is Get (initiate the request, the method name is exists);

For document operations, the request is XxxRequest ("index library name", "ID"), adding is Index, deleting is Delete, querying is Get, modifying is Update, and there is also a batch operation, which is BulkRequest;

Guess you like

Origin blog.csdn.net/qq_42108331/article/details/131884799