一、准备数据
1.1、数据文档
字段名称备注createDatesendDate发送时间 datelongCode发送长号码 如 16092389287811Mobile如 13000000000corpName发送公司名称,需要分词检索smsContent下发短信内容,需要分词检索State短信下发状态 0 成功 1 失败 integerOperatorid运营商编号1移动2联通3电信 integerProvince省份ipAddr下发服务器IP地址replyTotal短信状态报告返回时长 integerFee扣费 integer
1.2、创建索引
PUT /sms-logs-index
{
"settings":
{
"number_of_replicas": 3
, "number_of_shards": 5
}
, "mappings":
{
"properties":
{
"createDate":
{
"type": "date"
,"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
,"senDate":
{
"type": "date"
,"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
,"longCode":
{
"type": "keyword"
}
,"moblie":
{
"type": "keyword"
}
,"corpName":
{
"type": "text"
,"analyzer": "ik_smart"
}
,"smsContent":
{
"type": "text"
,"analyzer": "ik_smart"
}
,"state":
{
"type": "integer"
}
,"opratorId":
{
"type": "integer"
}
,"province":
{
"type": "keyword"
}
,"ipAddr":
{
"type": "keyword"
}
,"replyTotal":
{
"type": "integer"
}
,"fee":
{
"type": "double"
}
}
}
}
package com.chb.test;
import com.chb.utils.ESClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.junit.Test;
import java.io.IOException;
public class SMSDemo {
//先定义索引名和类型名
String index = "sms-logs-index";
@Test
public void create_index() throws IOException {
Settings.Builder settings = Settings.builder()
.put("number_of_shards", 3)
.put("number_of_replicas", 1);
XContentBuilder mappings = JsonXContent.contentBuilder()
.startObject()
.startObject("properties")
.startObject("createDate")
.field("type", "text")
.endObject()
.startObject("sendDate")
.field("type", "date")
.field("format", "yyyy-MM-dd")
.endObject()
.startObject("longCode")
.field("type", "text")
.endObject()
.startObject("mobile")
.field("type", "text")
.endObject()
.startObject("corpName")
.field("type", "text")
.field("analyzer", "ik_max_word")
.endObject()
.startObject("smsContent")
.field("type", "text")
.field("analyzer", "ik_max_word")
.endObject()
.startObject("state")
.field("type", "integer")
.endObject()
.startObject("operatorid")
.field("type", "integer")
.endObject()
.startObject("province")
.field("type", "text")
.endObject()
.startObject("ipAddr")
.field("type", "text")
.endObject()
.startObject("replyTotal")
.field("type", "integer")
.endObject()
.startObject("fee")
.field("type", "integer")
.endObject()
.endObject()
.endObject();
CreateIndexRequest request = new CreateIndexRequest(index)
.settings(settings)
.mapping(mappings);
RestHighLevelClient client = ESClient.getClient();
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
System.out.println(response.toString());
}
}
1.3、添加数据
PUT /sms-logs-index/_doc/1
{
"createDate":"2020-09-16"
,"senDate":"2020-09-16"
,"longCode":"10201021"
,"moblie":13026254898
,"corpName":"上海智慧软件有限公司"
,"smsContent":"连接你我,智慧软件,让生活更美好"
,"state":"1"
,"opratorId":"1"
,"province":"上海"
,"ipAddr":"10.215.19.45"
,"replyTotal":"1"
,"fee":"0.1"
}
PUT /sms-logs-index/_doc/2
{
"createDate":"2020-09-16"
,"senDate":"2020-09-16"
,"longCode":"20165411010"
,"moblie":15248754897
,"corpName":"北京鑫鑫能源有限公司"
,"smsContent":"欢迎使用新能源,让世界更环保"
,"state":"1"
,"opratorId":"2"
,"province":"北京"
,"ipAddr":"10.245.29.280"
,"replyTotal":"0.6"
,"fee":"0.5"
}
PUT /sms-logs-index/_doc/3
{
"createDate":"2020-09-16"
,"senDate":"2020-09-16"
,"longCode":"5478434123"
,"moblie":18056587445
,"corpName":"中威集团"
,"smsContent":"中威集团,服务于你的身边!"
,"state":"0"
,"opratorId":"3"
,"province":"杭州"
,"ipAddr":"10.248.19.45"
,"replyTotal":"4"
,"fee":"20"
}
PUT /sms-logs-index/_doc/4
{
"createDate":"2020-09-16"
,"senDate":"2020-09-16"
,"longCode":"87454120"
,"moblie":13625789645
,"corpName":"爱美化妆品有限公司"
,"smsContent":"魅力,势不可挡,爱美爱美"
,"state":"1"
,"opratorId":"1"
,"province":"上海"
,"ipAddr":"10.258.19.45"
,"replyTotal":"1"
,"fee":"200"
}
PUT /sms-logs-index/_doc/5
{
"createDate":"2020-09-16"
,"senDate":"2020-09-16"
,"longCode":"24514635"
,"moblie":18545427895
,"corpName":"东东集团"
,"smsContent":"数据驱动,AI推动,新零售模型让你的购买更心怡!"
,"state":"1"
,"opratorId":"1"
,"province":"北京"
,"ipAddr":"10.254.19.45"
,"replyTotal":"1"
,"fee":"6000"
}
PUT /sms-logs-index/_doc/6
{
"createDate":"2020-09-16"
,"senDate":"2020-09-16"
,"longCode":"89451254"
,"moblie":13028457893
,"corpName":"大兴建筑有限公司"
,"smsContent":"我房建,你放心,大兴建筑!"
,"state":"1"
,"opratorId":"1"
,"province":"杭州"
,"ipAddr":"10.215.19.45"
,"replyTotal":"1"
,"fee":"500"
}
PUT /sms-logs-index/_doc/7
{
"createDate":"2020-09-16"
,"senDate":"2020-09-16"
,"longCode":"33656412674"
,"moblie":18956451203
,"corpName":"华丽网集团"
,"smsContent":"网络安全,华丽靠谱!"
,"state":"1"
,"opratorId":"3"
,"province":"上海"
,"ipAddr":"10.215.254.45"
,"replyTotal":"1"
,"fee":"2000"
}
PUT /sms-logs-index/_doc/8
{
"createDate":"2020-09-16"
,"senDate":"2020-09-16"
,"longCode":"56412345"
,"moblie":17055452369
,"corpName":"万事Ok公司"
,"smsContent":"万事Ok,找我没错!"
,"state":"0"
,"opratorId":"2"
,"province":"杭州"
,"ipAddr":"10.215.19.45"
,"replyTotal":"1"
,"fee":"200"
}
PUT /sms-logs-index/_doc/9
{
"createDate":"2020-09-16"
,"senDate":"2020-09-16"
,"longCode":"5784320"
,"moblie":15236964578
,"corpName":"花花派"
,"smsContent":"花开花落,魅力女性,买花选我!"
,"state":"1"
,"opratorId":"1"
,"province":"上海"
,"ipAddr":"10.265.19.45"
,"replyTotal":"1"
,"fee":"0.1"
}
PUT /sms-logs-index/_doc/10
{
"createDate":"2020-09-16"
,"senDate":"2020-09-16"
,"longCode":"54784641"
,"moblie":15625584654
,"corpName":"勾股科技有限公司"
,"smsContent":"智能算法,智慧生活,勾股科技!"
,"state":"1"
,"opratorId":"2"
,"province":"杭州"
,"ipAddr":"10.215.19.45"
,"replyTotal":"6"
,"fee":"4000"
}
PUT /sms-logs-index/_doc/11
{
"createDate":"2020-09-22"
,"senDate":"2020-09-22"
,"longCode":"458744536"
,"moblie":134625584654
,"corpName":"星雨文化传媒"
,"smsContent":"魅力宣传,星雨传媒!"
,"state":"1"
,"opratorId":"3"
,"province":"杭州"
,"ipAddr":"10.289.19.45"
,"replyTotal":"6"
,"fee":"500"
}
PUT /sms-logs-index/_doc/12
{
"createDate":"2020-09-22"
,"senDate":"2020-09-22"
,"longCode":"123546241"
,"moblie":156625584654
,"corpName":"哈雷天文用具公司"
,"smsContent":"天文研究,放心推动,哈雷天文!"
,"state":"1"
,"opratorId":"3"
,"province":"杭州"
,"ipAddr":"10.289.19.45"
,"replyTotal":"6"
,"fee":"500"
}