Elasticsearch 教程-精确查询(1)-数据准备

本篇在Elasticsearch中创建基础数据,为*精确查询*系列提供数据初始化。

1. 创建索引及类型

  • 索引名:order
  • 类型名:order
  • 创建脚本:
PUT /order/order/_mapping
{
    "properties": {
        "dateId": {#日期
            "type": "keyword"
        }
        ,"retailerCode": {#客户代码
            "type": "keyword"
        }
        ,"orderCode": {#订单号
            "type": "keyword"
        }
        ,"barCode": {#规格代码
            "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:存储数据时候,不会分词建立索引,适合精确匹配
  • text:存储数据时候,会自动分词,并生成索引,适合全文检索

2. 插入数据

  • 通过程序模拟数据导入,先导入相关的包:Elasticsearch api包,包括json序列表包

    
        org.elasticsearch.client
        elasticsearch-rest-high-level-client
        6.1.1
    
    
        com.alibaba
        fastjson
        1.2.45
    
  • 通过RestHighLevelClient模拟数据并插入,代码如下:
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++) {// 模拟规格

                    long startTime = System.currentTimeMillis();

                    List 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 getOrderList(int cityIndex, int barIndex) throws SQLException {
        String cityCode = "city_" + cityIndex;
        String barCode = "bar_" + barIndex;

        DateTime dt = DateTime.now();
        List list = new ArrayList();
        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++) {// 模拟零售户订购次数
                        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 list) throws IOException {
        BulkRequest request = new BulkRequest();
        request.timeout(TimeValue.timeValueMinutes(1)); // 设置超时,等待所有节点确认索引已打开(使用TimeValue形式)
        // request.timeout("2m"); //设置超时,等待所有节点确认索引已打开(使用字符串形式)
        for (Order o : list) {
            // 添加到请求中 索引:retailer 类型:base _id:retaielr.getRetailerCode()
            request.add(new IndexRequest("order", "order").source(JSONObject.toJSONString(o), XContentType.JSON));
        }
        // 执行请求
        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;
    }
    
    
    
}

你可能感兴趣的:(Elasticsearch 教程-精确查询(1)-数据准备)