本篇在Elasticsearch中創(chuàng)建基礎數(shù)據(jù)省艳,為*精確查詢*系列提供數(shù)據(jù)初始化赠橙。
1. 創(chuàng)建索引及類型
- 索引名:order
- 類型名:order
- 創(chuàng)建腳本:
PUT /order/order/_mapping
{
"properties": {
"dateId": {#日期
"type": "keyword"
}
,"retailerCode": {#客戶代碼
"type": "keyword"
}
,"orderCode": {#訂單號
"type": "keyword"
}
,"barCode": {#規(guī)格代碼
"type": "keyword"
}
,"saleNum": {#銷量
"type": "double"
}
,"saleMoney": {#銷售額
"type": "double"
}
,"needNum": {
"type": "double"
}
,"updateTime": {
"type": "date"
}
,"cityCode": {#地市代碼
"type": "keyword"
}
,"levelCode": {#客戶級別
"type": "integer"
}
}
}
- ES 5.*之后度硝,把string字段設置為了過時字段沐兵,引入text,keyword字段,這兩個字段都可以存儲字符串使用
-
keyword:存儲數(shù)據(jù)時候涯贞,不會分詞建立索引,適合精確匹配
-
text:存儲數(shù)據(jù)時候悉抵,會自動分詞肩狂,并生成索引,適合全文檢索
2. 插入數(shù)據(jù)
- 通過程序模擬數(shù)據(jù)導入姥饰,先導入相關的包:Elasticsearch api包傻谁,包括json序列表包
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.1.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.45</version>
</dependency>
- 通過RestHighLevelClient模擬數(shù)據(jù)并插入,代碼如下:
package com.ctitc.es;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.joda.time.DateTime;
import com.alibaba.fastjson.JSONObject;
import com.ctitc.util.Order;
/**
* 使用高級api
*
* @author zongbo
*
*/
public class HighRestClientForOrder {
public static int count = 0;
public static void main(String[] args) {
RestHighLevelClient client = null;
try {
client = new RestHighLevelClient(RestClient.builder(
new HttpHost("192.168.1.191", 9200, "http")
// , new HttpHost("192.168.1.192", 9200, "http") 多個用逗號分隔
));
long startTimeall = System.currentTimeMillis();
for (int cityIndex = 0; cityIndex < 10; cityIndex++) {// 模擬地市
for (int barIndex = 0; barIndex < 100; barIndex++) {// 模擬規(guī)格
long startTime = System.currentTimeMillis();
List<Order> list = getOrderList(cityIndex, barIndex);
long endTime1 = System.currentTimeMillis();
System.out.println("查詢時間:" + (endTime1 - startTime) + "ms");
BulkResponse reponse = save(client, list);
if (reponse.hasFailures()) {
System.out.println("本次請求有錯誤 " + reponse.buildFailureMessage());
}
long endTime = System.currentTimeMillis();
System.out.println("單次遷移時間: " + (endTime - startTime) + "ms列粪,查詢時間:" + (endTime1 - startTime)
+ "ms审磁,插入es時間:" + (endTime - endTime1));
}
}
long endTimeall = System.currentTimeMillis(); // 獲取結束時間
System.out.println("所有運行時間: " + (endTimeall - startTimeall) + "ms");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (client != null) {
try {
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private static List<Order> getOrderList(int cityIndex, int barIndex) throws SQLException {
String cityCode = "city_" + cityIndex;
String barCode = "bar_" + barIndex;
DateTime dt = DateTime.now();
List<Order> list = new ArrayList<Order>();
for (int i = 0; i < 20; i++) {// 模擬日期
String dateId = dt.plusDays(i).toString("yyyyMMdd");
for (int j = 1; j < 30; j++) {// 模擬檔位
int levelCode = j;
for (int x = 0; x < 200; x++) {// 模擬零售戶
String retailerCode = "retailer_" + x;
int orderTime = x % 9 == 0 ? 2 : 1;
for (int y = 0; y < orderTime; y++) {// 模擬零售戶訂購次數(shù)
Order o = new Order();
o.setOrderCode("order_" + count++);
o.setUpdateTime(new Date());
o.setBarCode(barCode);
o.setCityCode(cityCode);
o.setDateId(dateId);
o.setLevelCode(levelCode);
o.setRetailerCode(retailerCode);
o.setSaleMoney((1000.0 + (int) (Math.random() * 100)));
o.setSaleNum((5.0 + (int) (Math.random() * 10)));// 條 5-15
o.setNeedNum((15.0 + (int) (Math.random() * 10)));// 條 5-15
list.add(o);
}
}
}
}
return list;
}
private static BulkResponse save(RestHighLevelClient client, List<Order> list) throws IOException {
BulkRequest request = new BulkRequest();
request.timeout(TimeValue.timeValueMinutes(1)); // 設置超時谈飒,等待所有節(jié)點確認索引已打開(使用TimeValue形式)
// request.timeout("2m"); //設置超時,等待所有節(jié)點確認索引已打開(使用字符串形式)
for (Order o : list) {
// 添加到請求中 索引:retailer 類型:base _id:retaielr.getRetailerCode()
request.add(new IndexRequest("order", "order").source(JSONObject.toJSONString(o), XContentType.JSON));
}
// 執(zhí)行請求
return client.bulk(request);
}
}
- 其中Order是與es mapping字段一樣的javaBean
package com.ctitc.util;
import java.util.Date;
public class Order {
private String retailerCode;
private String dateId;
private String orderCode;
private String barCode;
private Double saleNum;
private Double saleMoney;
private Double needNum;
private int levelCode;
private String cityCode;
private Date updateTime ;
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public String getRetailerCode() {
return retailerCode;
}
public void setRetailerCode(String retailerCode) {
this.retailerCode = retailerCode;
}
public String getDateId() {
return dateId;
}
public void setDateId(String dateId) {
this.dateId = dateId;
}
public String getOrderCode() {
return orderCode;
}
public void setOrderCode(String orderCode) {
this.orderCode = orderCode;
}
public String getBarCode() {
return barCode;
}
public void setBarCode(String barCode) {
this.barCode = barCode;
}
public Double getSaleNum() {
return saleNum;
}
public void setSaleNum(Double saleNum) {
this.saleNum = saleNum;
}
public Double getSaleMoney() {
return saleMoney;
}
public void setSaleMoney(Double saleMoney) {
this.saleMoney = saleMoney;
}
public Double getNeedNum() {
return needNum;
}
public void setNeedNum(Double needNum) {
this.needNum = needNum;
}
public int getLevelCode() {
return levelCode;
}
public void setLevelCode(int levelCode) {
this.levelCode = levelCode;
}
public String getCityCode() {
return cityCode;
}
public void setCityCode(String cityCode) {
this.cityCode = cityCode;
}
}