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

import com.google.gson.Gson;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
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.common.xcontent.XContentType;
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));
            }*/

                // 创建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 indexBridge(List<LuKeBridge> luKeBridges) throws Exception {
        // 建立文档
        //prepareIndex一次创建一个索引，bulkIndex就可以将多个文档合并在一起之后，发送一次请求
        BulkRequestBuilder bulk = getClient().prepareBulk();
        for (LuKeBridge luKeBridge : luKeBridges) {
            String json = gson.toJson(luKeBridge);
            IndexRequestBuilder requestBuilder = transportClient.prepareIndex("basicbridgedata", "2018", luKeBridge.getId()).setSource(json, XContentType.JSON);
            bulk.add(requestBuilder);
        }
        BulkResponse bulkItemResponses = bulk.get();
        System.out.println(bulkItemResponses.status());

    }

    /**
     * @Description:
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date: 2019/4/28 0028
     */
    public static void indexHighWay(List<LuKeHighWay> luKeHighWays) throws Exception {
        // 建立文档
        BulkRequestBuilder bulk = getClient().prepareBulk();
        for (LuKeHighWay luKeHighWay : luKeHighWays) {
            String json = gson.toJson(luKeHighWay);
            IndexRequestBuilder requestBuilder = transportClient.prepareIndex("basichighwaydata", "2018", luKeHighWay.getId()).setSource(json, XContentType.JSON);
            bulk.add(requestBuilder);
        }
        BulkResponse bulkItemResponses = bulk.get();
        System.out.println(bulkItemResponses.status());
    }

    /**
     * @Description: 隧道索引表插入数据
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date:
     */
    public static void indexBasicTunnel(List<BasicTunnel> basicTunnels) throws Exception {
        // 建立文档
        BulkRequestBuilder bulk = getClient().prepareBulk();
        for (BasicTunnel basicTunnel : basicTunnels) {
            String json = gson.toJson(basicTunnel);
            IndexRequestBuilder requestBuilder = transportClient.prepareIndex("basictunneldata", "2018", basicTunnel.getId()).setSource(json, XContentType.JSON);
            bulk.add(requestBuilder);
        }
        BulkResponse bulkItemResponses = bulk.get();
        System.out.println(bulkItemResponses.status());
    }

    /**
     * @Description: 收费站索引表插入收据
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date: 2019/4/28 0028
     */
    public static void indexBaseRoadToll(List<BaseRoadToll> baseRoadTolls) throws Exception {
        // 建立文档
        //prepareIndex一次创建一个索引，bulkIndex就可以将多个文档合并在一起之后，发送一次请求
        BulkRequestBuilder bulk = getClient().prepareBulk();
        for (BaseRoadToll baseRoadToll : baseRoadTolls) {
            String json = gson.toJson(baseRoadToll);
            IndexRequestBuilder requestBuilder = transportClient.prepareIndex("baseroadtolldata", "2018", String.valueOf(baseRoadToll.getId())).setSource(json, XContentType.JSON);
            bulk.add(requestBuilder);
        }
        BulkResponse bulkItemResponses = bulk.get();
        System.out.println(bulkItemResponses.status());

    }

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

    /**
     * @Description: 桥梁全字段查询
     * @Param:
     * @return: ArrayList<LuKeBridge> luKeBridges
     * @Author: dongjili
     * @Date:
     */
    public static ArrayList<LuKeBridge> 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,
                    "id",
                    "qiaoliangmingcheng",
                    "qiaoliangdaima"
            ));
        }
        String[] bridgeNames = {
                "id",
                "quhuadaima",
                "quhuamingcheng",
                "qiaoliangmingcheng",
                "qiaoliangdaima",
                "zhongxinzhuanghao",
                "luxianbianma",
                "luxianmingcheng",
                "jishudengji",
                "quanchang",
                "kuajingzongchang",
                "dankongzuidakuajing",
                "qiaokuazuhe",
                "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"
        };
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        ArrayList<LuKeBridge> luKeBridges = 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.LuKeBridge");
            //获取私有的构造方法
            Constructor<?> constructor = clazz.getDeclaredConstructor();
            //暴力破解
            constructor.setAccessible(true);
            //实例化对象
            LuKeBridge luKeBridge = (LuKeBridge) constructor.newInstance();
            //获得私有的属性对象
            Field[] beanFiled = clazz.getDeclaredFields();
            luKeBridge.setId(hit.getId());
            for (int a = 1; a < bridgeNames.length; a++) {
                //暴力破解
                beanFiled[a].setAccessible(true);
                //把查询到的每一个字段的值赋给luKeBridge的每一个属性
                beanFiled[a].set(luKeBridge, sourceAsMap.get(bridgeNames[a]) == null ? "" : sourceAsMap.get(bridgeNames[a]).toString());
            }
            luKeBridges.add(luKeBridge);
        }
        return luKeBridges;
    }
    /**
     * @Description: 桥梁字段查询查询出一条结果就返回
     * @Param:
     * @return: ArrayList<LuKeBridge> luKeBridges
     * @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,
                    "qiaoliangmingcheng"
            ));
        }
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        if (hits.getHits().length>0){
            return true;
        }else {
            return false;
        }
    }
    /**
     * @Description: 路段全字段查询
     * @Param:
     * @return: ArrayList<LuKeHighWay> lukehighways
     * @Author: dongjili
     * @Date:
     */
    public static ArrayList<LuKeHighWay> 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,
                    "id",
                    "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"
        };
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        ArrayList<LuKeHighWay> lukehighways = 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.LuKeHighWay");
            //获取私有的构造方法
            Constructor<?> constructor = clazz.getDeclaredConstructor();
            //暴力破解
            constructor.setAccessible(true);
            //实例化对象
            LuKeHighWay luKeHighWay = (LuKeHighWay) constructor.newInstance();
            //获取私有的属性字段
            Field[] beanFiled = clazz.getDeclaredFields();
            luKeHighWay.setId(hit.getId());
            for (int a = 1; a < highwayNames.length; a++) {
                //暴力破解
                beanFiled[a].setAccessible(true);
                //给路段属性复制
                beanFiled[a].set(luKeHighWay, sourceAsMap.get(highwayNames[a]) == null ? "" : sourceAsMap.get(highwayNames[a]).toString());
            }
            lukehighways.add(luKeHighWay);
        }
        return lukehighways;
    }
    /**
     * @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,
                    "luxianmingcheng"
            ));
        }

        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<BasicTunnel> 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,
                    "id",
                    "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"
        };
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        ArrayList<BasicTunnel> basicTunnels = 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.BasicTunnel");
            //获取私有的构造方法
            Constructor<?> constructor = clazz.getDeclaredConstructor();
            //暴力破解
            constructor.setAccessible(true);
            //实例化对象
            BasicTunnel basicTunnel = (BasicTunnel) constructor.newInstance();
            //获取私有的属性字段
            Field[] beanFiled = clazz.getDeclaredFields();
            basicTunnel.setId(hit.getId());
            for (int a = 1; a < basicTunnelNames.length; a++) {
                //暴力破解
                beanFiled[a].setAccessible(true);
                //给路段属性复制
                beanFiled[a].set(basicTunnel, sourceAsMap.get(basicTunnelNames[a]) == null ? "" : sourceAsMap.get(basicTunnelNames[a]).toString());
            }
            basicTunnels.add(basicTunnel);
        }
        return basicTunnels;
    }
    /**
     * @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,
                    "suidaomingcheng"
            ));
        }

        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        String tunnelName="隧道明细表";
        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"
//            ));
            searchRequestBuilder.setQuery(QueryBuilders.matchAllQuery());
        }
        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;
        }

    }
}

