package com.elephant.framework.galaxy.aroad.bigdata;

import com.google.gson.Gson;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 自定义elasticsearch工具类，
 * 1、实现elasticsearch集群的连接
 * 2、路段、桥梁索引数据插入
 * 3、路段、桥梁分词检索
 * 4、分词检索出来的数据是否是路段数据或者桥梁数据
 * @Param:
 * @return:
 * @Author: dongjili
 * @Date:
 */
public class EsUtil_fanshe {
    public static TransportClient transportClient;
    public static Gson gson = new Gson();

    /**
     * @Description: 创建连接
     * @Param:
     * @return: transportClient
     * @Author: dongjili
     * @Date:
     */
    public static TransportClient getClient() throws Exception {
        Settings settings = Settings.builder().put("cluster.name", "myes").build();
        if (transportClient == null) {
            synchronized (EsUtil_fanshe.class) {
                // 创建Client连接对象
                transportClient = new PreBuiltTransportClient(settings)
                        .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.6.100"), 9300))
                        .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.6.110"), 9300))
                        .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.6.120"), 9300));
            }

        }

        return transportClient;
    }

    /**
     * @Description: 释放资源
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date:
     */
    public static void close() {
        //释放资源
        if (transportClient != null) {
            transportClient.close();
        }
    }

    /**
     * @Description: 桥梁全字段查询
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date:
     */
    public static ArrayList<Basebridgealldata> queryBridge(String keyword, String index, String type, int start, int row) throws Exception {
        // 搜索数据
        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes(type).setFrom(start).setSize(row);
        //isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length> 0(去掉字符串两端的多余的空格的长度）
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            //multiMatchQuery多个字段匹配某一个值
            //multiMatchQuery(text,fields)其中的fields是字段的名字，可以写好几个，每一个中间用逗号分隔
            searchRequestBuilder.setQuery(QueryBuilders.multiMatchQuery(keyword,
                    "qiaoliangmingcheng",
                    "qiaoliangdaima"
            ));
        }
        String[] bridgeNames = {
                "id",
                "quhuadaima",
                "quhuamingcheng",
                "qiaoliangmingcheng",
                "qiaoliangdaima",
                "zhongxinzhuanghao",
                "luxianbianma",
                "luxianmingcheng",
                "jishudengji",
                "quanchang",
                "kuajingzongchang",
                "dankongzuidakuajing",
                "qiaoliangquankuan",
                "qiaomianjingkuan",
                "kuajingfenleidaima",
                "kuajingfenleileixing",
                "nianxianfenleidaima",
                "nianxianfenleileixing",
                "yuanshangbujiegou1daima",
                "yuanshangbujiegou1xingshi",
                "yuanshangbujiegou2daima",
                "yuanshangbujiegou2xingshi",
                "shangbujiegoudaima",
                "shangbujiegouxingshi",
                "shangbujiegoucailiaodaima",
                "shangbujiegoucailiaomingcheng",
                "qiaodunleixingdaima",
                "qiaodunleixingmingcheng",
                "shejihezaidaima",
                "shejihezaidengji",
                "kangzhendengjidaima",
                "kangzhendengji",
                "kuayuediwudaima",
                "kuayuediwuleixing",
                "kuayuemingcheng",
                "tonghangdengji",
                "fangzhuangleixing",
                "lijiaoleibie",
                "jianshedanwei",
                "shejidanwei",
                "shigongdanwei",
                "jianlidanwei",
                "xiujianniandu",
                "tongcheriqi",
                "guanyangdaima",
                "guanyangmingcheng",
                "jianguandanweimingcheng",
                "shoufeixingzhidaima",
                "shoufeixingzhi",
                "pingdingdengjidaima",
                "pingdingdengji",
                "pingdingdanwei",
                "pingdingriqi",
                "gaizaoniandu",
                "wangongriqi",
                "gaizaobuwei",
                "gaizaoshigongdanwei",
                "gongchengxingzhi",
                "bubuxiangmu",
                "binghaidaima",
                "binghaibuwei",
                "binghaimiaoshu",
                "guanzhicuoshidaima",
                "guanzhicuoshi",
                "beizhu",
                "zhuqiaokongshu",
                "zhuqiaozhukua",
                "zhuqiaobiankua",
                "qianyinqiaochang",
                "houyinqiaochang",
                "qiaoxiajingkong",
                "xiabujiegoudaima",
                "xiabujiegoumingcheng",
                "qiaotaidaima",
                "qiaotaimingcheng",
                "zhizuodaima",
                "zhizuomingcheng",
                "qiaomianpuzhuangdaima",
                "qiaomianpuzhuangmingcheng",
                "shensuofengdaima",
                "shensuofengmingcheng",
                "wanpoxiedaima",
                "wanpoxiemingcheng",
                "yuyingliqiao",
                "zaixiuzaijian",
                "zhadaomianji",
                "shejihongshuipinlv",
                "zongzaojia",
                "maishezhuanghao",
                "shifouzhadaoqiao",
                "shifoushangkuaqiao",
                "guanlidanweimingcheng",
                "guanlidanweidaima",
                "biangengyuanyindaima",
                "biangengyuanyin",
                "biangengshijian",
                "xiangmuxingzhi",
                "xiangmumingcheng",
                "jihuaxiangmubianma",
                "xiangmuleibie",
                "xiangmudaima",
                "xiangmuluxianbianma",
                "xiangmuluxianmingcheng",
                "xiangmuqiaoliangzhuanghao",
                "xiangmubiangengyuanyin",
                "xiangmubiangengshijian",
                "touzilaiyuan",
                "qiaoliangsuozaiweizhi",
                "shifoukuanluzhaiqiao",
                "shifouzaizhangdaqiaolaingmuluzhong",
                "tianbaodanweimingcheng",
                "shape",
                "qiaokuazuhe"
        };
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        ArrayList<Basebridgealldata> basebridgealldataList = new ArrayList<>();
        for (SearchHit hit : hits) {
            //System.out.println(hit.getSourceAsString());
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            //LuKeBridge luKeBridge = new LuKeBridge();
            //获取该类在堆区的Class对象
            Class<?> clazz = Class.forName("com.elephant.framework.galaxy.aroad.bigdata.Basebridgealldata");
            //获取私有的构造方法
            Constructor<?> constructor = clazz.getDeclaredConstructor();
            //暴力破解
            constructor.setAccessible(true);
            //实例化对象
            Basebridgealldata basebridgealldata = (Basebridgealldata) constructor.newInstance();
            //获得私有的属性对象
            Field[] beanFiled = clazz.getDeclaredFields();
            basebridgealldata.setId(hit.getId());
            for (int a = 1; a < bridgeNames.length; a++) {
                //暴力破解
                beanFiled[a].setAccessible(true);
                //把查询到的每一个字段的值赋给luKeBridge的每一个属性
                beanFiled[a].set(basebridgealldata, sourceAsMap.get(bridgeNames[a]) == null ? "" : sourceAsMap.get(bridgeNames[a]).toString());
            }
            basebridgealldataList.add(basebridgealldata);
        }
        return basebridgealldataList;
    }
    /**
     * @Description: termquery()桥梁代码词条查询
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date: 2019/5/10 0010
     */
    public static ArrayList<Basebridgealldata> queryBridgeQLDM(String keyword, String index, String type, int start, int row) throws Exception {
        // 搜索数据
        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes(type).setFrom(start).setSize(row);
        //isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length> 0(去掉字符串两端的多余的空格的长度）
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            //multiMatchQuery多个字段匹配某一个值
            //multiMatchQuery(text,fields)其中的fields是字段的名字，可以写好几个，每一个中间用逗号分隔
            searchRequestBuilder.setQuery(QueryBuilders.termQuery("qiaoliangdaima",keyword));
        }
        String[] bridgeNames = {
                "id",
                "quhuadaima",
                "quhuamingcheng",
                "qiaoliangmingcheng",
                "qiaoliangdaima",
                "zhongxinzhuanghao",
                "luxianbianma",
                "luxianmingcheng",
                "jishudengji",
                "quanchang",
                "kuajingzongchang",
                "dankongzuidakuajing",
                "qiaoliangquankuan",
                "qiaomianjingkuan",
                "kuajingfenleidaima",
                "kuajingfenleileixing",
                "nianxianfenleidaima",
                "nianxianfenleileixing",
                "yuanshangbujiegou1daima",
                "yuanshangbujiegou1xingshi",
                "yuanshangbujiegou2daima",
                "yuanshangbujiegou2xingshi",
                "shangbujiegoudaima",
                "shangbujiegouxingshi",
                "shangbujiegoucailiaodaima",
                "shangbujiegoucailiaomingcheng",
                "qiaodunleixingdaima",
                "qiaodunleixingmingcheng",
                "shejihezaidaima",
                "shejihezaidengji",
                "kangzhendengjidaima",
                "kangzhendengji",
                "kuayuediwudaima",
                "kuayuediwuleixing",
                "kuayuemingcheng",
                "tonghangdengji",
                "fangzhuangleixing",
                "lijiaoleibie",
                "jianshedanwei",
                "shejidanwei",
                "shigongdanwei",
                "jianlidanwei",
                "xiujianniandu",
                "tongcheriqi",
                "guanyangdaima",
                "guanyangmingcheng",
                "jianguandanweimingcheng",
                "shoufeixingzhidaima",
                "shoufeixingzhi",
                "pingdingdengjidaima",
                "pingdingdengji",
                "pingdingdanwei",
                "pingdingriqi",
                "gaizaoniandu",
                "wangongriqi",
                "gaizaobuwei",
                "gaizaoshigongdanwei",
                "gongchengxingzhi",
                "bubuxiangmu",
                "binghaidaima",
                "binghaibuwei",
                "binghaimiaoshu",
                "guanzhicuoshidaima",
                "guanzhicuoshi",
                "beizhu",
                "zhuqiaokongshu",
                "zhuqiaozhukua",
                "zhuqiaobiankua",
                "qianyinqiaochang",
                "houyinqiaochang",
                "qiaoxiajingkong",
                "xiabujiegoudaima",
                "xiabujiegoumingcheng",
                "qiaotaidaima",
                "qiaotaimingcheng",
                "zhizuodaima",
                "zhizuomingcheng",
                "qiaomianpuzhuangdaima",
                "qiaomianpuzhuangmingcheng",
                "shensuofengdaima",
                "shensuofengmingcheng",
                "wanpoxiedaima",
                "wanpoxiemingcheng",
                "yuyingliqiao",
                "zaixiuzaijian",
                "zhadaomianji",
                "shejihongshuipinlv",
                "zongzaojia",
                "maishezhuanghao",
                "shifouzhadaoqiao",
                "shifoushangkuaqiao",
                "guanlidanweimingcheng",
                "guanlidanweidaima",
                "biangengyuanyindaima",
                "biangengyuanyin",
                "biangengshijian",
                "xiangmuxingzhi",
                "xiangmumingcheng",
                "jihuaxiangmubianma",
                "xiangmuleibie",
                "xiangmudaima",
                "xiangmuluxianbianma",
                "xiangmuluxianmingcheng",
                "xiangmuqiaoliangzhuanghao",
                "xiangmubiangengyuanyin",
                "xiangmubiangengshijian",
                "touzilaiyuan",
                "qiaoliangsuozaiweizhi",
                "shifoukuanluzhaiqiao",
                "shifouzaizhangdaqiaolaingmuluzhong",
                "tianbaodanweimingcheng",
                "shape",
                "qiaokuazuhe"
        };
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        ArrayList<Basebridgealldata> basebridgealldataList = new ArrayList<>();
        for (SearchHit hit : hits) {
            //System.out.println(hit.getSourceAsString());
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            //LuKeBridge luKeBridge = new LuKeBridge();
            //获取该类在堆区的Class对象
            Class<?> clazz = Class.forName("com.elephant.framework.galaxy.aroad.bigdata.Basebridgealldata");
            //获取私有的构造方法
            Constructor<?> constructor = clazz.getDeclaredConstructor();
            //暴力破解
            constructor.setAccessible(true);
            //实例化对象
            Basebridgealldata basebridgealldata = (Basebridgealldata) constructor.newInstance();
            //获得私有的属性对象
            Field[] beanFiled = clazz.getDeclaredFields();
            basebridgealldata.setId(hit.getId());
            for (int a = 1; a < bridgeNames.length; a++) {
                //暴力破解
                beanFiled[a].setAccessible(true);
                //把查询到的每一个字段的值赋给luKeBridge的每一个属性
                beanFiled[a].set(basebridgealldata, sourceAsMap.get(bridgeNames[a]) == null ? "" : sourceAsMap.get(bridgeNames[a]).toString());
            }
            basebridgealldataList.add(basebridgealldata);
        }
        return basebridgealldataList;
    }
    /**
     * @Description: 桥梁字段查询查询出一条结果就返回
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date:
     */
    public static boolean queryBridgeOne(String keyword, String index, String type, int start, int row) throws Exception {
        // 搜索数据
        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes(type).setFrom(start).setSize(row);
        //isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length> 0(去掉字符串两端的多余的空格的长度）
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            //multiMatchQuery多个字段匹配某一个值
            //multiMatchQuery(text,fields)其中的fields是字段的名字，可以写好几个，每一个中间用逗号分隔
            searchRequestBuilder.setQuery(QueryBuilders.multiMatchQuery(keyword,
                    "id",
                    "qiaoliangmingcheng",
                    "qiaoliangdaima"
            ));
        }
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        if (hits.getHits().length>0){
            return true;
        }else {
            return false;
        }

    }

    /**
     * @Description: 路段全字段查询
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date:
     */
    public static ArrayList<Basehighwayalldata> queryHighWay(String keyword, String index, String type, int start, int row) throws Exception {
        //搜索数据
        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes(type).setFrom(start).setSize(row);
        //isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length> 0(去掉字符串两端的多余的空格的长度）
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            //multiMatchQuery多个字段匹配某一个值
            //multiMatchQuery(text,fields)其中的fields是字段的名字，可以写好几个，每一个中间用逗号分隔
            searchRequestBuilder.setQuery(QueryBuilders.multiMatchQuery(keyword,
                    "luxianbianhao",
                    "luxianmingcheng",
                    "qidianmingcheng",
                    "zhidianmingcheng"
            ));
        }
        String[] highwayNames = {"id",
                "luxianbianhao",
                "quhuadaima",
                "luxianmingcheng",
                "qidianmingcheng",
                "zhidianmingcheng",
                "Qidianzhuanghao",
                "zhidianzhuanghao",
                "licheng",
                "lichengjisuan",
                "sanDlicheng",
                "dengjidaima",
                "dengjimingcheng",
                "chedaodaima",
                "chedaofenlei",
                "mianchengdaima",
                "mianchengfenlei",
                "lujikuandu",
                "lumiankuandu",
                "shejishisu",
                "xiujianniandu",
                "gaijianniandu",
                "chengguanlu",
                "duantoulu",
                "chongfubianma",
                "chongfuxuhao",
                "qidianzhuanghao",
                "zhongdianzhuanghao",
                "yanghulicheng",
                "qingyulicheng",
                "kelvhaolicheng",
                "yilvhualicheng",
                "dimaodaima",
                "dimaohanzi",
                "handongshuliang",
                "maisheqidianzhuanghao",
                "maishezhidianzhuanghao",
                "quhuamingchengshi",
                "quhuamingchengxian",
                "quhuamingchengxiang",
                "luduanxuhao",
                "guanyangdanweileibie",
                "beizhu",
                "kaigongriqi",
                "jungongriqi",
                "qidianfenjiedianleixing",
                "zhidianfenjiedianleixing",
                "shengjichurukou",
                "GBMlicheng",
                "yangbanlicheng",
                "tongcheriqi",
                "luxiandifangmingcheng",
                "jianshexingzhidaima",
                "jianshexingzhihanzi",
                "duanlianleixing",
                "luduanleixingdaima",
                "luduanleixing",
                "guanyangdanweimingcheng",
                "MQI",
                "pingdingdengji",
                "shoufeixingzhi",
                "biangengyuanyindaima",
                "biangengyuanyin",
                "biangengshijian",
                "xiangmuxingzhi",
                "xiangmumingcheng",
                "xiangmudaima",
                "xiangmuqidianmingcheng",
                "xiangmuzhidianmingcheng",
                "xiangmuleibie",
                "jihuaxiangmubianma",
                "xiangmuluxianbianma",
                "xiangmuluxianmingcheng",
                "xiangmubiangengyuanyin",
                "xiangmubiangengshijian",
                "touzilaiyuan",
                "miancenghoudu",
                "zuijinyicidaxiuniandu",
                "zuijinyicizhongxiuniandu",
                "guodaotiaozhengqianluxianbianma",
                "guodaozhuanghaochuandiyuliulicheng",
                "nianpingjunrijiaotongliang",
                "shifouanganxiangongluguanlijieyang",
                "luxiantiaozhengleixing",
                "shangnianluxianbianma",
                "shangnianluxianmingcheng",
                "shangnianluxianqidianzhuanghao",
                "shangnianluxianzhidianzhuanghao",
                "tianbaodanweimingcheng",
                "shape"
        };
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        ArrayList<Basehighwayalldata> basehighwayalldataList = new ArrayList<>();
        for (SearchHit hit : hits) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            //LuKeHighWay luKeHighWay = new LuKeHighWay();
            //获取该类在堆区的Class对象
            Class<?> clazz = Class.forName("com.elephant.framework.galaxy.aroad.bigdata.Basehighwayalldata");
            //获取私有的构造方法
            Constructor<?> constructor = clazz.getDeclaredConstructor();
            //暴力破解
            constructor.setAccessible(true);
            //实例化对象
            Basehighwayalldata basehighwayalldata = (Basehighwayalldata) constructor.newInstance();
            //获取私有的属性字段
            Field[] beanFiled = clazz.getDeclaredFields();
            basehighwayalldata.setId(hit.getId());
            for (int a = 1; a < highwayNames.length; a++) {
                //暴力破解
                beanFiled[a].setAccessible(true);
                //给路段属性复制
                beanFiled[a].set(basehighwayalldata, sourceAsMap.get(highwayNames[a]) == null ? "" : sourceAsMap.get(highwayNames[a]).toString());
            }
            basehighwayalldataList.add(basehighwayalldata);
        }
        return basehighwayalldataList;
    }
    /**
    * @Description: termquery()路线编号词条查询
    * @Param:
    * @return:
    * @Author: dongjili
    * @Date: 2019/5/10 0010
    */
    public static ArrayList<Basehighwayalldata> queryHighWayLXBH(String keyword, String index, String type, int start, int row) throws Exception {
        //搜索数据
        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes(type).setFrom(start).setSize(row);
        //isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length> 0(去掉字符串两端的多余的空格的长度）
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            //multiMatchQuery多个字段匹配某一个值
            //multiMatchQuery(text,fields)其中的fields是字段的名字，可以写好几个，每一个中间用逗号分隔
            searchRequestBuilder.setQuery(QueryBuilders.termQuery("luxianbianhao",keyword));
            /*searchRequestBuilder.setQuery(QueryBuilders.matchQuery("luxianbianhao",keyword));*/
        }
        String[] highwayNames = {"id",
                "luxianbianhao",
                "quhuadaima",
                "luxianmingcheng",
                "qidianmingcheng",
                "zhidianmingcheng",
                "qidianzhuanghaoa",
                "zhidianzhuanghao",
                "licheng",
                "lichengjisuan",
                "sanDlicheng",
                "dengjidaima",
                "dengjimingcheng",
                "chedaodaima",
                "chedaofenlei",
                "mianchengdaima",
                "mianchengfenlei",
                "lujikuandu",
                "lumiankuandu",
                "shejishisu",
                "xiujianniandu",
                "gaijianniandu",
                "chengguanlu",
                "duantoulu",
                "chongfubianma",
                "chongfuxuhao",
                "qidianzhuanghaob",
                "zhongdianzhuanghao",
                "yanghulicheng",
                "qingyulicheng",
                "kelvhaolicheng",
                "yilvhualicheng",
                "dimaodaima",
                "dimaohanzi",
                "handongshuliang",
                "maisheqidianzhuanghao",
                "maishezhidianzhuanghao",
                "quhuamingchengshi",
                "quhuamingchengxian",
                "quhuamingchengxiang",
                "luduanxuhao",
                "guanyangdanweileibie",
                "beizhu",
                "kaigongriqi",
                "jungongriqi",
                "qidianfenjiedianleixing",
                "zhidianfenjiedianleixing",
                "shengjichurukou",
                "gbmlicheng",
                "yangbanlicheng",
                "tongcheriqi",
                "luxiandifangmingcheng",
                "jianshexingzhidaima",
                "jianshexingzhihanzi",
                "duanlianleixing",
                "luduanleixingdaima",
                "luduanleixing",
                "guanyangdanweimingcheng",
                "mqi",
                "pingdingdengji",
                "shoufeixingzhi",
                "biangengyuanyindaima",
                "biangengyuanyin",
                "biangengshijian",
                "xiangmuxingzhi",
                "xiangmumingcheng",
                "xiangmudaima",
                "xiangmuqidianmingcheng",
                "xiangmuzhidianmingcheng",
                "xiangmuleibie",
                "jihuaxiangmubianma",
                "xiangmuluxianbianma",
                "xiangmuluxianmingcheng",
                "xiangmubiangengyuanyin",
                "xiangmubiangengshijian",
                "touzilaiyuan",
                "miancenghoudu",
                "zuijinyicidaxiuniandu",
                "zuijinyicizhongxiuniandu",
                "guodaotiaozhengqianluxianbianma",
                "guodaozhuanghaochuandiyuliulicheng",
                "nianpingjunrijiaotongliang",
                "shifouanganxiangongluguanlijieyang",
                "luxiantiaozhengleixing",
                "shangnianluxianbianma",
                "shangnianluxianmingcheng",
                "shangnianluxianqidianzhuanghao",
                "shangnianluxianzhidianzhuanghao",
                "tianbaodanweimingcheng",
                "shape"
        };
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        ArrayList<Basehighwayalldata> basehighwayalldataList = new ArrayList<>();
        for (SearchHit hit : hits) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            //LuKeHighWay luKeHighWay = new LuKeHighWay();
            //获取该类在堆区的Class对象
            Class<?> clazz = Class.forName("com.elephant.framework.galaxy.aroad.bigdata.Basehighwayalldata");
            //获取私有的构造方法
            Constructor<?> constructor = clazz.getDeclaredConstructor();
            //暴力破解
            constructor.setAccessible(true);
            //实例化对象
            Basehighwayalldata basehighwayalldata = (Basehighwayalldata) constructor.newInstance();
            //获取私有的属性字段
            Field[] beanFiled = clazz.getDeclaredFields();
            basehighwayalldata.setId(hit.getId());
            for (int a = 1; a < highwayNames.length; a++) {
                //暴力破解
                beanFiled[a].setAccessible(true);
                //给路段属性复制
                beanFiled[a].set(basehighwayalldata, sourceAsMap.get(highwayNames[a]) == null ? "" : sourceAsMap.get(highwayNames[a]).toString());
            }
            basehighwayalldataList.add(basehighwayalldata);
        }
        return basehighwayalldataList;
    }

    /**
     * @Description: 路段全字段查询, 查询出一条结果就返回
     * @Param:
     * @return: ArrayList<LuKeHighWay> lukehighways
     * @Author: dongjili
     * @Date:
     */
    public static boolean queryHighWayOne(String keyword, String index, String type, int start, int row) throws Exception {
        //搜索数据
        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes(type).setFrom(start).setSize(row);
        //isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length> 0(去掉字符串两端的多余的空格的长度）
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            //multiMatchQuery多个字段匹配某一个值
            //multiMatchQuery(text,fields)其中的fields是字段的名字，可以写好几个，每一个中间用逗号分隔
            searchRequestBuilder.setQuery(QueryBuilders.multiMatchQuery(keyword,
                    "id",
                    "luxianbianhao",
                    "luxianmingcheng",
                    "qidianmingcheng",
                    "zhidianmingcheng"
            ));
        }
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        if (hits.getHits().length>0){
            return true;
        }else {
            return false;
        }

    }

    /**
     * @Description: 隧道全字段查询
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date: 2019/4/28 0028
     */
    public static List<Basetunnelalldata> queryTunnel(String keyword, String index, String type, int start, int row) throws Exception {
        //搜索数据
        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes(type).setFrom(start).setSize(row);
        //isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length> 0(去掉字符串两端的多余的空格的长度）
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            //multiMatchQuery多个字段匹配某一个值
            //multiMatchQuery(text,fields)其中的fields是字段的名字，可以写好几个，每一个中间用逗号分隔
            searchRequestBuilder.setQuery(QueryBuilders.multiMatchQuery(keyword,
                    "suidaomingcheng"
            ));
        }
        String[] basicTunnelNames = {
                "id",
                "suidaomingcheng",
                "suidaodaima",
                "zhongxinzhuanghao",
                "luxianbianma",
                "luxianmingcheng",
                "jishudengji",
                "changdu",
                "jingkuan",
                "jinggao",
                "changdufenleidaima",
                "fenleimingcheng",
                "shuixiasuidao",
                "xiujianniandu",
                "jianshedanwei",
                "shejidanwei",
                "shigongdanwei",
                "jianlidanwei",
                "tongcheshijian",
                "shuidaoyanghudengji",
                "guanyangdanweixingzhi",
                "guanyangdanweimingcheng",
                "jianguandanwei",
                "pingdingdengji",
                "pingdingriqi",
                "pingdingdanwei",
                "pingdingdengjitujian",
                "pingdingriqitujian",
                "pingdingdanweitujian",
                "pingdingdengjijidian",
                "pingdingriqijidian",
                "pingdingdanweijidian",
                "pingdingdengjiqita",
                "pingdingriqiqita",
                "pingdingdanweiqita",
                "gaijianniandu",
                "wangongriqi",
                "gaizaibuwei",
                "gongchengxingzhi",
                "binghaibuwei",
                "binghaimiaoshu",
                "xianjizhengqudaima",
                "shifouzaizhangdasuidaomuluzhong",
                "beizhu",
                "shape"
        };
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        List<Basetunnelalldata> basetunnelalldataList = new ArrayList<>();
        for (SearchHit hit : hits) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            //LuKeHighWay luKeHighWay = new LuKeHighWay();
            //获取该类在堆区的Class对象
            Class<?> clazz = Class.forName("com.elephant.framework.galaxy.aroad.bigdata.Basetunnelalldata");
            //获取私有的构造方法
            Constructor<?> constructor = clazz.getDeclaredConstructor();
            //暴力破解
            constructor.setAccessible(true);
            //实例化对象
            Basetunnelalldata basetunnelalldata = (Basetunnelalldata) constructor.newInstance();
            //获取私有的属性字段
            Field[] beanFiled = clazz.getDeclaredFields();
            basetunnelalldata.setId(hit.getId());
            for (int a = 1; a < basicTunnelNames.length; a++) {
                //暴力破解
                beanFiled[a].setAccessible(true);
                //给路段属性复制
                beanFiled[a].set(basetunnelalldata, sourceAsMap.get(basicTunnelNames[a]) == null ? "" : sourceAsMap.get(basicTunnelNames[a]).toString());
            }
            basetunnelalldataList.add(basetunnelalldata);
        }
        return basetunnelalldataList;
    }
    /**
     * @Description: termquery()隧道代码词条匹配查询
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date: 2019/5/10 0010
     */
    public static List<Basetunnelalldata> queryTunnelSDDM(String keyword, String index, String type, int start, int row) throws Exception {
        //搜索数据
        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes(type).setFrom(start).setSize(row);
        //isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length> 0(去掉字符串两端的多余的空格的长度）
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            //multiMatchQuery多个字段匹配某一个值
            //multiMatchQuery(text,fields)其中的fields是字段的名字，可以写好几个，每一个中间用逗号分隔
            searchRequestBuilder.setQuery(QueryBuilders.termQuery("suidaodaima",keyword));
        }
        String[] basicTunnelNames = {
                "id",
                "suidaomingcheng",
                "suidaodaima",
                "zhongxinzhuanghao",
                "luxianbianma",
                "luxianmingcheng",
                "jishudengji",
                "changdu",
                "jingkuan",
                "jinggao",
                "changdufenleidaima",
                "fenleimingcheng",
                "shuixiasuidao",
                "xiujianniandu",
                "jianshedanwei",
                "shejidanwei",
                "shigongdanwei",
                "jianlidanwei",
                "tongcheshijian",
                "shuidaoyanghudengji",
                "guanyangdanweixingzhi",
                "guanyangdanweimingcheng",
                "jianguandanwei",
                "pingdingdengji",
                "pingdingriqi",
                "pingdingdanwei",
                "pingdingdengjitujian",
                "pingdingriqitujian",
                "pingdingdanweitujian",
                "pingdingdengjijidian",
                "pingdingriqijidian",
                "pingdingdanweijidian",
                "pingdingdengjiqita",
                "pingdingriqiqita",
                "pingdingdanweiqita",
                "gaijianniandu",
                "wangongriqi",
                "gaizaibuwei",
                "gongchengxingzhi",
                "binghaibuwei",
                "binghaimiaoshu",
                "xianjizhengqudaima",
                "shifouzaizhangdasuidaomuluzhong",
                "beizhu",
                "shape"
        };
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        List<Basetunnelalldata> basetunnelalldataList = new ArrayList<>();
        for (SearchHit hit : hits) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            //LuKeHighWay luKeHighWay = new LuKeHighWay();
            //获取该类在堆区的Class对象
            Class<?> clazz = Class.forName("com.elephant.framework.galaxy.aroad.bigdata.Basetunnelalldata");
            //获取私有的构造方法
            Constructor<?> constructor = clazz.getDeclaredConstructor();
            //暴力破解
            constructor.setAccessible(true);
            //实例化对象
            Basetunnelalldata basetunnelalldata = (Basetunnelalldata) constructor.newInstance();
            //获取私有的属性字段
            Field[] beanFiled = clazz.getDeclaredFields();
            basetunnelalldata.setId(hit.getId());
            for (int a = 1; a < basicTunnelNames.length; a++) {
                //暴力破解
                beanFiled[a].setAccessible(true);
                //给路段属性复制
                beanFiled[a].set(basetunnelalldata, sourceAsMap.get(basicTunnelNames[a]) == null ? "" : sourceAsMap.get(basicTunnelNames[a]).toString());
            }
            basetunnelalldataList.add(basetunnelalldata);
        }
        return basetunnelalldataList;
    }
    /**
     * @Description: 隧道全字段查询，查询出一条就返回
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date: 2019/4/28 0028
     */
    public static boolean queryTunnelOne(String keyword, String index, String type, int start, int row) throws Exception {
        //搜索数据
        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes(type).setFrom(start).setSize(row);
        //isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length> 0(去掉字符串两端的多余的空格的长度）
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            //multiMatchQuery多个字段匹配某一个值
            //multiMatchQuery(text,fields)其中的fields是字段的名字，可以写好几个，每一个中间用逗号分隔
            searchRequestBuilder.setQuery(QueryBuilders.multiMatchQuery(keyword,
                    "id",
                    "suidaomingcheng"
            ));
        }
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        if (hits.getHits().length>0){
            return true;
        }else {
            return false;
        }

    }

    /**
     * @Description: 收费站全字段查询
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date: 2019/4/28 0028
     */
    public static ArrayList<BaseRoadToll_String> queryBaseRoadTollString(String keyword, String index, String type, int start, int row) throws Exception {
        //搜索数据
        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes(type).setFrom(start).setSize(row);
        //isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length> 0(去掉字符串两端的多余的空格的长度）
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            //multiMatchQuery多个字段匹配某一个值
            //multiMatchQuery(text,fields)其中的fields是字段的名字，可以写好几个，每一个中间用逗号分隔
            searchRequestBuilder.setQuery(QueryBuilders.multiMatchQuery(keyword,
                    "id",
                    "name"
            ));
        }
        String[] baseRoadTollNames = {"id",
                "roadCode",
                "roadName",
                "number",
                "code",
                "name",
                "shortName",
                "tecLevel",
                "locationType",
                "tollDirection",
                "entNum",
                "entEtcNum",
                "entMtcNum",
                "entMixNum",
                "expNum",
                "expEtcNum",
                "expMtcNum",
                "expMixNum",
                "managementUnit",
                "longitude",
                "latitude"
        };
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        ArrayList<BaseRoadToll_String> baseRoadTollStrings = new ArrayList<>();
        for (SearchHit hit : hits) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            //LuKeHighWay luKeHighWay = new LuKeHighWay();
            //获取该类在堆区的Class对象
            Class<?> clazz = Class.forName("com.elephant.framework.galaxy.aroad.bigdata.BaseRoadToll_String");
            //获取私有的构造方法
            Constructor<?> constructor = clazz.getDeclaredConstructor();
            //暴力破解
            constructor.setAccessible(true);
            //实例化对象
            BaseRoadToll_String baseRoadTollString = (BaseRoadToll_String) constructor.newInstance();
            //获取私有的属性字段
            Field[] beanFiled = clazz.getDeclaredFields();
            baseRoadTollString.setId(hit.getId());
            for (int a = 1; a < baseRoadTollNames.length; a++) {
                //暴力破解
                beanFiled[a].setAccessible(true);
                //给路段属性复制
                beanFiled[a].set(baseRoadTollString, sourceAsMap.get(baseRoadTollNames[a]) == null ? "" : sourceAsMap.get(baseRoadTollNames[a]).toString());
            }
            baseRoadTollStrings.add(baseRoadTollString);

        }
        return baseRoadTollStrings;
    }

    /**
     * @Description: 收费站全字段查询，查询出一条数据就返回
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date: 2019/4/28 0028
     */
    public static boolean queryBaseRoadTollOne(String keyword, String index, String type, int start, int row) throws Exception {
        //搜索数据
        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes(type).setFrom(start).setSize(row);
        //isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length> 0(去掉字符串两端的多余的空格的长度）
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            //multiMatchQuery多个字段匹配某一个值
            //multiMatchQuery(text,fields)其中的fields是字段的名字，可以写好几个，每一个中间用逗号分隔
            searchRequestBuilder.setQuery(QueryBuilders.multiMatchQuery(keyword,
                    "name"
            ));
        }
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        if (hits.getHits().length>0){
            return true;
        }else {
            return false;
        }

    }

    /**
     * @Description: 服务区全字段查询
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date: 2019/5/8 0008
     */
    public static ArrayList<BaseHighWayService> queryBaseHighWayService(String keyword, String index, String type, int start, int row) throws Exception {
        //搜索数据
        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes(type).setFrom(start).setSize(row);
        //isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length> 0(去掉字符串两端的多余的空格的长度）
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            //multiMatchQuery多个字段匹配某一个值
            //multiMatchQuery(text,fields)其中的fields是字段的名字，可以写好几个，每一个中间用逗号分隔
            searchRequestBuilder.setQuery(QueryBuilders.multiMatchQuery(keyword,
                    "fwssmc"
            ));
        }
        String[] baseRoadTollNames = {
                "id",
                "lxbh",
                "lxmc",
                "zh1",
                "fwsslx",
                "fwssmc",
                "csyysj",
                "bjxs",
                "jyms",
                "zdmj",
                "tccmj",
                "tcwsl",
                "fwssgnSFycs",
                "fwssgnSfyjyss",
                "fwssgnSfyjqss",
                "fwssgnSfyclcdss",
                "fwssgnSfycyss",
                "fwssgnSfycs",
                "fwssgnSfyzsss",
                "fwssgnSfyqcwx",
                "bz",
                "lxbm",
                "zh",
                "jd",
                "wd"
        };
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        ArrayList<BaseHighWayService> baseHighWayServices = new ArrayList<>();
        for (SearchHit hit : hits) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            //LuKeHighWay luKeHighWay = new LuKeHighWay();
            //获取该类在堆区的Class对象
            Class<?> clazz = Class.forName("com.elephant.framework.galaxy.aroad.bigdata.BaseHighWayService");
            //获取私有的构造方法
            Constructor<?> constructor = clazz.getDeclaredConstructor();
            //暴力破解
            constructor.setAccessible(true);
            //实例化对象
            BaseHighWayService baseHighWayService = (BaseHighWayService) constructor.newInstance();
            //获取私有的属性字段
            Field[] beanFiled = clazz.getDeclaredFields();
            baseHighWayService.setId(hit.getId());
            for (int a = 1; a < baseRoadTollNames.length; a++) {
                //暴力破解
                beanFiled[a].setAccessible(true);
                //给路段属性复制
                beanFiled[a].set(baseHighWayService, sourceAsMap.get(baseRoadTollNames[a]) == null ? "" : sourceAsMap.get(baseRoadTollNames[a]).toString());
            }
            baseHighWayServices.add(baseHighWayService);

        }
        return baseHighWayServices;
    }

    /**
     * @Description: 服务区全字段查询，查询出一条数据就返回
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date: 2019/5/8 0008
     */
    public static boolean queryBaseHighWayServiceOne(String keyword, String index, String type, int start, int row) throws Exception {
        //搜索数据
        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes(type).setFrom(start).setSize(row);
        //isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length> 0(去掉字符串两端的多余的空格的长度）
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            //multiMatchQuery多个字段匹配某一个值
            //multiMatchQuery(text,fields)其中的fields是字段的名字，可以写好几个，每一个中间用逗号分隔
            searchRequestBuilder.setQuery(QueryBuilders.multiMatchQuery(keyword,
                    "fwssmc"
            ));
        }
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        if (hits.getHits().length>0){
            return true;
        }else {
            return false;
        }
    }
}

