【ES】JavaAPI学习-03 文档新增 修改

在这里插入图片描述

前言

本节实现ES的文档的新增,修改

实现

对象创建

我们创建User.java实体类如下:

User.java

package com.zwy.es;

public class User {

    private String name;

    private String sex;

    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

插入

我们有ESTest_Doc_Insert.java 实现类如下:

package com.zwy.es;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

public class ESTest_Doc_Insert {
    public static void main(String[] args) throws IOException {

        //
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );

        // 插入数据
        IndexRequest request = new IndexRequest();
        request.index("users").id("1001");

        // 定义数据对象
        User user = new User();
        user.setName("张三");
        user.setAge(30);
        user.setSex("男");

        // 向ES插入数据,必须将数据转换为JSON格式
        ObjectMapper mapper = new ObjectMapper();
        String userJson = mapper.writeValueAsString(user);
        request.source(userJson, XContentType.JSON);  // 将数据放进request中,并告知数据类型

        IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);

        System.out.println(response.getResult());

        // 关闭ES客户端
        esClient.close();

    }
}

代码概要:

创建IndexRequest对象,这个对象将用于定义我们要执行的索引(插入)请求。

IndexRequest request = new IndexRequest();

指定要插入文档的索引和ID。

request.index("users").id("1001");

定义用户对象,并设置其属性。

User user = new User();
user.setName(“张三”);
user.setAge(30);
user.setSex(“男”);

这里假设有一个名为User的类,包含name、age和sex三个属性。

使用Jackson库的ObjectMapper将用户对象转换为JSON字符串。

ObjectMapper mapper = new ObjectMapper();
String userJson = mapper.writeValueAsString(user);

将JSON字符串设置到IndexRequest对象中,并指定数据类型为JSON。

request.source(userJson, XContentType.JSON);

使用esClient.index()方法发送索引请求,并获取响应。

IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);

打印响应的结果如下:
System.out.println(response.getResult());

这段主要通过Elasticsearch的向"users"索引中插入一个具有指定ID的文档,文档的内容是从一个用户对象转换而来的JSON字符串,插入成功后,会打印出响应的结果,如"created"或"updated"等,如果文档ID已经存在,这将被视为更新操作。

修改

我们有 ESTest_Doc_Update.java 实现类如下:

package com.zwy.es;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

public class ESTest_Doc_Update {
    public static void main(String[] args) throws IOException {

        //
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );

        // 插入数据
        UpdateRequest request = new UpdateRequest();
        request.index("users").id("1001");
        request.doc(XContentType.JSON, "sex", "女");  // 修改文档数据

        UpdateResponse response = esClient.update(request, RequestOptions.DEFAULT);

        System.out.println(response.getResult());

        // 关闭ES客户端
        esClient.close();

    }
}

创建UpdateRequest对象,这个对象将用于定义我们要执行的更新请求。

UpdateRequest request = new UpdateRequest();

指定要更新文档的索引和ID。

request.index("users").id("1001");

在请求中指定要更新的文档字段及其新值。这里我们只想修改"sex"字段的值为"女"。

request.doc(XContentType.JSON, "sex", "女");

使用esClient.update()方法发送更新请求,并获取响应。

UpdateResponse response = esClient.update(request, RequestOptions.DEFAULT);

打印响应的结果。
System.out.println(response.getResult());

这段主要通过Elasticsearch向"users"索引中的指定ID文档进行更新操作,这里我们只修改了"sex"字段的值为"女",更新成功后,会打印出响应的结果,如"updated"等,如果文档ID不存在,更新操作将会失败。

注意:这个例子中的更新操作是直接替换整个文档,如果想根据某些条件部分更新文档,可以使用脚本或者 partial documents 进行更新,例如,你可以使用ScriptedUpsert和script参数来编写一个Painless脚本进行更新。

文件结构

我们文件结构如下:

在这里插入图片描述

运行

main()方法 运行 ESTest_Doc_Insert.java:
在这里插入图片描述

然后可以打开POSTMAN,定义一个GET 请求,地址为我们刚刚定义的:

http://127.0.0.1:9200/users/_doc/1001

我们请求结果如下:
在这里插入图片描述
然后发送请求有:

在这里插入图片描述
可以看到查询成功!

运行 ESTest_Doc_Update.java ,结果如下:

在这里插入图片描述

可以看到修改成功,之后我们再POSTMAN发送请求:

在这里插入图片描述
可以看到由于此前的修改,查询可以发现此时张三修改为了女。

总结

文档插入和更新是Elasticsearch中两种常见的数据操作。

文档插入是指将新的文档添加到Elasticsearch索引中,在插入过程中,如果文档ID已经存在,Elasticsearch会根据设置的冲突解决策略进行处理。默认情况下,如果文档ID已存在,Elasticsearch会替换原有文档。

插入文档通常步骤:
创建IndexRequest对象,并指定索引名和文档ID(可选)。
设置要插入的文档内容,通常是将对象转换为JSON字符串。
使用esClient.index()方法发送插入请求,并获取响应。处理响应结果。

文档更新是指修改Elasticsearch索引中已存在的文档内容。更新操作可以是完全替换整个文档,也可以是部分更新文档的某些字段。

更新文档通常步骤:
创建UpdateRequest对象,并指定索引名和文档ID。
设置要更新的文档内容或脚本。如果是部分更新,可以使用doc()方法指定要更新的字段和值;如果是使用脚本进行复杂更新,可以使用script()方法指定脚本。
使用esClient.update()方法发送更新请求,并获取响应,处理响应结果。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

锥栗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值