首页 > 代码库 > 通过Java的Domain类构建ElasticSearch的mapping

通过Java的Domain类构建ElasticSearch的mapping

通过给定一个Java的class类自行创建ElasticSearch的mapping

Order的domain类

public class Order {
    public String system_id;
    public String finger_id;
    public String merchant_id;
    public Double tx_money;
    public String pay_date;
    public String payment_status;
    public Date settle_date;
    public Long goods_count;
}

用于构建elasticsearch的mapping启动类

public class startMapping {
    private static Log log = LogFactory.getLog(startMapping.class);
    private static TransportClient client = null;
    private static IndicesAdminClient adminClient;  
    
    private static TransportClient getClient() throws UnknownHostException {
        Settings settings = Settings.settingsBuilder().put("cluster.name", Constant.CLUSTER).build();
        TransportClient client = TransportClient.builder().settings(settings).build();
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(Constant.ESHOST), Constant.ESPORT));
        return client;
    }
    
    static{
        try {
            client = getClient();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        //elasticsearch-mapping build
        DoMapping();
                
    }
    
    /**
     * 判断ElasticSearch中的索引是否存在
     */
    private static boolean exists(String index){ 
        adminClient = client.admin().indices();    
        IndicesExistsRequest request = new IndicesExistsRequest(index);  
        IndicesExistsResponse response = adminClient.exists(request).actionGet();  
        if (response.isExists()) {  
            return true;  
        }  
        return false;  
    }  
    
    private static void DoMapping() {
        if(exists(Constant.INDEX)){
            System.out.println(Constant.INDEX+"索引已经存在!");
            log.info(Constant.INDEX+"索引已经存在!");
            return;
        }
        String template = "{\n" 
                            +"\t\""+Constant.INDEX+"\":{\n" 
                                +"\t\t\"properties\":{\n"
                                        + "\t\t{#}\n" 
                                + "\t\t\t\t}\n"
                                + "\t\t}\n" 
                            +"}";
        String fieldString =  getClassMapping();
        template = template.replace("{#}",fieldString);
        client.admin().indices().prepareCreate(Constant.INDEX).addMapping(Constant.INDEX, template).get();
        System.out.println(template);
        System.out.println("创建ElasticSearch Mapping完成!!!");
        log.info("创建ElasticSearch Mapping完成!!!");
    }
    
    /**
     * 从类的字段映射处elasticsearch中的字段 
     * @return
     */
    public static String getClassMapping() {
        StringBuilder fieldstring = new StringBuilder();
        Field[] fields = Order.class.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
             fieldstring.append("\t\t\t\t\"" + fields[i].getName().toLowerCase() + "\": {\n");
             fieldstring.append("\t\t\t\t\t\t\"type\": \""
                        + GetElasticSearchMappingType(fields[i].getType().getSimpleName()) + "\n");
             if (i == fields.length-1) {
                    fieldstring.append("\t\t\t\t\t}\n");
                } else {
                    fieldstring.append("\t\t\t\t\t},\n");
                }
        }
        return fieldstring.toString();
    }

    private static String GetElasticSearchMappingType(String varType) {
        String es = "String";
        switch (varType) {
        case "Date":
            es = "date\"\n"+"\t\t\t\t\t\t,\"format\":\"yyyy-MM-dd HH:mm:ss\"\n"+"\t\t\t\t\t\t,\"null_value\":\"1900-01-01 00:00:01\"";
            break;
        case "Double":
            es = "double\"\n"+"\t\t\t\t\t\t,\"null_value\":\"NaN\"";
            break;
        case "Long":
            es = "long\"";
            break;
        default:
            es = "string\"";
            break;
        }
        return es;
    }  
    
}

 

关于elasticsearch mapping字段手动增加
  curl -XPUT http://192.168.46.165:9200/t_order/_mapping/t_order?pretty -d ‘{"properties":{"order_ip":{"type":"string"}}}‘

 

通过Java的Domain类构建ElasticSearch的mapping