package org.example.test;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.example.entity.Person;
import org.example.entity.SmsLogsIndex;
import org.example.utils.ESClient;
import org.junit.Test;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
public class MyTest {
ObjectMapper objectMapper = new ObjectMapper();
RestHighLevelClient esClient = ESClient.getClient();
String index = "sms-logs-index";
String type = "sms-logs-type";
@Test
public void createIndex() throws Exception {
// 1. 准备关于索引的setting
Settings.Builder settings = Settings.builder()
.put("number_of_shards", 3)
.put("number_of_replicas", 1);
// 2. 准备关于索引的结构mappings
XContentBuilder xContentBuilder = JsonXContent.contentBuilder()
.startObject()
.startObject("properties")
.startObject("createDate")
.field("type", "date")
.field("format", "yyyy-MM-dd HH:mm:ss")
.endObject()
.startObject("sendDate")
.field("type", "date")
.field("format", "yyyy-MM-dd HH:mm:ss")
.endObject()
.startObject("longCode")
.field("type", "keyword")
.endObject()
.startObject("mobile")
.field("type", "keyword")
.endObject()
.startObject("corpName")
.field("type", "text")
.endObject()
.startObject("smsContent")
.field("type", "text")
.endObject()
.startObject("state")
.field("type", "integer")
.endObject()
.startObject("operatorId")
.field("type", "integer")
.endObject()
.startObject("province")
.field("type", "keyword")
.endObject()
.startObject("ipAddr")
.field("type", "keyword")
.endObject()
.startObject("replyTotal")
.field("type", "integer")
.endObject()
.startObject("fee")
.field("type", "integer")
.endObject()
.endObject()
.endObject();
// 3. 将settings 和 mappings封装到一个Request中
CreateIndexRequest request = new CreateIndexRequest(index)
.settings(settings)
.mapping(type, xContentBuilder);
// 4. 通过client对象连接es并执行创建索引
CreateIndexResponse createIndexResponse = esClient.indices().create(request, RequestOptions.DEFAULT);
System.out.println(createIndexResponse.toString());
}
@Test
public void bulkCreateDoc() throws IOException {
// 1.准备多个json数据
Date now = new Date();
SmsLogsIndex index1 = new SmsLogsIndex(1, now, now, "1069000001", "13800000001", "北京公司", "尊敬的用户,您的会议将在今天上午10点开始,请准时参加会议。如果有任何问题,请联系会议组织者。", 0, 1, "北京", "192.168.0.1", 15, 10);
SmsLogsIndex index2 = new SmsLogsIndex(2, now, now, "1069000002", "13800000002", "上海企业", "您好,由于项目需求变化,项目截止日期已延长至下个月15日,请知悉并安排好工作计划。", 1, 2, "上海", "192.168.0.2", 30, 20);
SmsLogsIndex index3 = new SmsLogsIndex(3, now, now, "1069000003", "13800000003", "广州科技", "尊敬的客户,今晚将进行服务器维护,从晚上11点至次日凌晨3点,期间可能会有短暂的服务中断。", 0, 3, "广东", "192.168.0.3", 45, 15);
SmsLogsIndex index4 = new SmsLogsIndex(4, now, now, "1069000004", "13800000004", "浙江有限责任公司", "亲爱的客户,祝您新年快乐!感谢您一年来对我们的支持与厚爱,期待在新的一年里继续为您提供优质服务。", 1, 1, "浙江", "192.168.0.4", 60, 25);
SmsLogsIndex index5 = new SmsLogsIndex(5, now, now, "1069000005", "13800000005", "江苏股份有限公司", "欢迎加入我们的服务!我们致力于为您提供最优质的体验。如果您有任何疑问,请随时联系我们的客服团队。", 0, 2, "江苏", "192.168.0.5", 75, 30);
SmsLogsIndex index6 = new SmsLogsIndex(6, now, now, "1069000006", "13800000006", "福建集团", "尊敬的客户,您的预约已确认,请于下周三上午9点准时到达我们的办公室。如需更改预约,请提前通知我们。", 1, 3, "福建", "192.168.0.6", 90, 35);
SmsLogsIndex index7 = new SmsLogsIndex(7, now, now, "1069000007", "13800000007", "湖南有限公司", "尊敬的客户,您的账单已到期,请尽快登录我们的官网进行支付,以免影响您的服务使用。", 0, 1, "湖南", "192.168.0.7", 105, 40);
SmsLogsIndex index8 = new SmsLogsIndex(8, now, now, "1069000008", "13800000008", "四川解决方案", "您好,我们很高兴地通知您,我们的新产品已经发布,欢迎您登录官网查看详细信息并进行购买。", 1, 2, "四川", "192.168.0.8", 120, 45);
SmsLogsIndex index9 = new SmsLogsIndex(9, now, now, "1069000009", "13800000009", "河南控股", "您的密码已成功更改,如果不是您本人操作,请立即联系我们的客服团队以确保账户安全。", 0, 3, "河南", "192.168.0.9", 135, 50);
SmsLogsIndex index10 = new SmsLogsIndex(10, now, now, "1069000010", "13800000010", "湖北集团", "您的验证码是123456,请在页面中输入此验证码以完成验证。请勿将此验证码告知他人。", 1, 1, "湖北", "192.168.0.10",150, 55);
SmsLogsIndex index11 = new SmsLogsIndex(11, now, now, "1069000011", "13800000011", "山东伙伴", "尊敬的用户,您的账户对账单已生成,请登录您的账户查看详细信息。如有疑问,请联系我们的客服团队。", 0, 2, "山东", "192.168.0.11", 165, 60);
SmsLogsIndex index12 = new SmsLogsIndex(12, now, now, "1069000012", "13800000012", "辽宁合伙", "您好,您的机票已成功预订,航班信息已发送至您的邮箱,请查收。如有任何疑问,请联系航空公司客服。", 1, 3, "辽宁", "192.168.0.12", 180, 65);
SmsLogsIndex index13 = new SmsLogsIndex(13, now, now, "1069000013", "13800000013", "黑龙江公司", "我们已收到您的付款,感谢您的及时支付。如有任何问题或疑问,请随时联系我们的客服团队。", 0, 1, "黑龙江", "192.168.0.13", 195, 70);
SmsLogsIndex index14 = new SmsLogsIndex(14, now, now, "1069000014", "13800000014", "陕西有限公司", "亲爱的客户,特惠活动正在进行中,欢迎登录我们的官网查看详细信息并享受优惠。", 1, 2, "陕西", "192.168.0.14", 210, 75);
SmsLogsIndex index1