• 通过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 = "{
    " 
                                +"	""+Constant.INDEX+"":{
    " 
                                    +"		"properties":{
    "
                                            + "		{#}
    " 
                                    + "				}
    "
                                    + "		}
    " 
                                +"}";
            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("				"" + fields[i].getName().toLowerCase() + "": {
    ");
                 fieldstring.append("						"type": ""
                            + GetElasticSearchMappingType(fields[i].getType().getSimpleName()) + "
    ");
                 if (i == fields.length-1) {
                        fieldstring.append("					}
    ");
                    } else {
                        fieldstring.append("					},
    ");
                    }
            }
            return fieldstring.toString();
        }
    
        private static String GetElasticSearchMappingType(String varType) {
            String es = "String";
            switch (varType) {
            case "Date":
                es = "date"
    "+"						,"format":"yyyy-MM-dd HH:mm:ss"
    "+"						,"null_value":"1900-01-01 00:00:01"";
                break;
            case "Double":
                es = "double"
    "+"						,"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"}}}'

  • 相关阅读:
    ubuntu18安装paddle的cpu版本错误
    博客园的推荐150名大牛的地址
    课堂翻译
    小组介绍
    Sunshine小组
    项目进程
    总结
    布置的开发项目
    一周总结
    每周学习
  • 原文地址:https://www.cnblogs.com/atomicbomb/p/6900166.html
Copyright © 2020-2023  润新知