Elasticsearch(十四)elasticsearch用代码从数据库里查询导入到elasticsearch中并查询结果

时间:2021-09-06 08:20:35

最近做了个小例子:将数据库中数据导入到elasticsearch中,然后查询一条特定数据,看是不是比较快

前情:在数据库中查询数据的速度是3.36s.
用代码从数据库中查出来再插入elasticsearch也许会比较慢,后续再学习用工具导入。
环境:elasticsearch5.3.0+jdk8+maven3.5

类似数据库,我们需要先给elasticsearch中建立一个索引,放置我们需要导入的数据

    http://localhost:9200/cqp  PUT


{

    "mappings":{
        "riskpremrate":{
            "properties":{
                "abateTime":{
                    "type":"date",
                    "format":"yyyy-MM-dd||epoch_millis"
                },
                "effectiveTime":{
                    "type":"date",
                    "format":"yyyy-MM-dd||epoch_millis"
                },
                "insertTimeForHis":{
                    "type":"date",
                    "format":"yyyy-MM-dd||epoch_millis"
                },
                "operateTimeForHis":{
                    "type":"date",
                    "format":"yyyy-MM-dd||epoch_millis"
                },
                "accidentType":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "actualValueLower":{
                    "type":"double"
                },
                "actualValueUpper":{
                    "type":"double"
                },
                "agreementProportion":{
                    "type":"double"
                },
                "ageLower":{
                    "type":"double"
                },
                "ageUpper":{
                    "type":"double"
                },
                "airBagsLower":{
                    "type":"integer"
                },
                "airBagsUpper":{
                    "type":"integer"
                },
                "amountLower":{
                    "type":"double"
                },
                "amountUpper":{
                    "type":"double"
                },
                "appointRepairShop":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "agentCode":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "branchCode":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "brandCode":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "buisnessNo":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "businessNature":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "carKindCode":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "carOwnerType":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "claimAdjustReason":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "clauseType":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "comCode":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "contractNo":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "countryNature":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "customerTypeCode":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "dtckFlag":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "deductible":{
                    "type":"double"
                },
                "deductiblerate":{
                    "type":"double"
                },
                "displacementLower":{
                    "type":"double"
                },
                "displacementUpper":{
                    "type":"double"
                },
                "driveramountLower":{
                    "type":"double"
                },
                "driveramountUpper":{
                    "type":"double"
                },
                "drivingYearsLower":{
                    "type":"double"
                },
                "drivingYearsUpper":{
                    "type":"double"
                },
                "editionNo":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "factorCode":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "factorValue":{
                    "type":"double"
                },
                "factorValue2":{
                    "type":"double"
                },
                "familyCode":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "groupCode":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "handler1Code":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "handlerCode":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "insuredCode":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "insuredFlag051029":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "insuredType":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "insuredType_T":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "isNolocalCar":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "isOwnerInsurant":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "kindCode":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "lastDamagedLower":{
                    "type":"double"
                },
                "lastDamagedLower1":{
                    "type":"double"
                },
                "lastDamagedLower2":{
                    "type":"double"
                },
                "lastDamagedLower3":{
                    "type":"double"
                },
                "lastDamagedUpper":{
                    "type":"double"
                },
                "lastDamagedUpper1":{
                    "type":"double"
                },
                "lastDamagedUpper2":{
                    "type":"double"
                },
                "lastDamagedUpper3":{
                    "type":"double"
                },
                "lastPaidRateLower":{
                    "type":"double"
                },
                "lastPaidRateUpper":{
                    "type":"double"
                },
                "lastPlatPaidRateLower":{
                    "type":"double"
                },
                "lastPlatPaidRateUpper":{
                    "type":"double"
                },
                "modeCode":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "modelCode":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "nodamYearsLower":{
                    "type":"double"
                },
                "nodamYearsLower1":{
                    "type":"double"
                },
                "nodamYearsLower2":{
                    "type":"double"
                },
                "nodamYearsLower3":{
                    "type":"double"
                },
                "nodamYearsUpper":{
                    "type":"double"
                },
                "nodamYearsUpper1":{
                    "type":"double"
                },
                "nodamYearsUpper2":{
                    "type":"double"
                },
                "nodamYearsUpper3":{
                    "type":"double"
                },
                "nrrFlag":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "passengeramountLower":{
                    "type":"double"
                },
                "passengeramountUpper":{
                    "type":"double"
                },
                "powerLower":{
                    "type":"double"
                },
                "powerUpper":{
                    "type":"double"
                },
                "projectCode":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "proposalCase":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "purchasePriceLower":{
                    "type":"double"
                },
                "purchasePriceUpper":{
                    "type":"double"
                },
                "recommendCode":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "renewalYears":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "runMilesLower":{
                    "type":"double"
                },
                "runMilesUpper":{
                    "type":"double"
                },
                "scratchAmountLower":{
                    "type":"double"
                },
                "scratchAmountUpper":{
                    "type":"double"
                },
                "seatCountLower":{
                    "type":"double"
                },
                "seatCountUpper":{
                    "type":"double"
                },
                "seqId":{
                    "type":"double"
                },
                "sex":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "tonCountLower":{
                    "type":"double"
                },
                "tonCountUpper":{
                    "type":"double"
                },
                "useYearsLower":{
                    "type":"double"
                },
                "useYearsUpper":{
                    "type":"double"
                },
                "specialTreaTreason":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "unitType":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "useNatureCode":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "vehicleClass":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "vehicleMaker":{
                    "type":"string",
                    "index":"not_analyzed"
                }
            }
        }
    }
}

项目代码如下:
1.pom文件

核心的就是elasticsearch这个要注意它的版本,如果是2.x版本引入类似的:

<dependency>
         <groupId>org.elasticsearch</groupId>
         <artifactId>elasticsearch</artifactId>
         <version>2.2.0</version>
    </dependency> 

如果是5.x版本引入以下即可:

         <dependency>
          <groupId>org.elasticsearch.client</groupId>
          <artifactId>transport</artifactId>
          <version>5.3.0</version>
        </dependency>

其他选择即可:

   <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.9.1</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.8.6</version>
    </dependency>

 <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.6</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.8.6</version>
</dependency>

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.8</version>
</dependency>

2.内部大概就是用sql查询完再用线程分别开辟elasticsearch批量器插入数据

package com.sinosoft.doc.demo;

import java.io.IOException;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.sql.DataSource;

import org.elasticsearch.action.bulk.BulkProcessor;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;

import org.elasticsearch.client.Client;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;

import com.sinosoft.bean.PrpFriskPremrateTable;
import com.sinosoft.doc.data.InitialData;

/**
 *  测试表在elasticsearch中查询是不是会快一些
 *  已在项目的本地环境查出它的查询速度是3.36s
 * @author CC
 *
 */
public class TestForRiskPremrateTable extends Thread{

    private static Client client = null;

    static {
        InitialData init = new InitialData();
        //初始化连接
        try {
            if(client == null) {
                client = init.connect("bdrg", true, 100, "127.0.0.1", 9300);
            }


        } catch (UnknownHostException e) {
            System.out.println("連接客戶端錯誤!");
            e.printStackTrace();
        }
    }
    //获得数据
            private static List<PrpFriskPremrateTable> list = new ArrayList<PrpFriskPremrateTable>();
    public static void getList2() {
        if(list == null || list.size() == 0) {
            System.out.println("開始初始化數據...");
        //获得数据 
            try {
                    //JNDI方式连接数据库示例
                    Connection conn = null;
                    DataSource ds = null;
                    Statement stmt = null;
                    ResultSet resultSet = null;
                    try {
                        // Load the Informix JDBC Driver  
                        Class.forName("com.informix.jdbc.IfxDriver"); 
                        // Create and open a server/database connection  
                                    conn = DriverManager.getConnection("jdbc:informix-sqli://xxxxxxxx/xxxxxx:informixserver=test2;NEWCODESET=gb18030,8859-1,819,Big5;IFX_USE_STRENC=true;user=xxx;password=xxxxx");  
                        System.out.println("JDBC driver name: "  
                     + conn.getMetaData().getDriverName());  
                        // Queries that return more than one row  

                        stmt = conn.createStatement();
                        resultSet = stmt.executeQuery("select * from PrpFriskPremrateTable where comcode = '32000000' ");//and editionno = '6' 
                        while(resultSet.next()){
                            PrpFriskPremrateTable pp = new PrpFriskPremrateTable();
                            Date AbateTime = null;
                            if(resultSet.getDate("AbateTime") != null) {
                                AbateTime = new Date(resultSet.getDate("AbateTime").getTime());
                            }
                            pp.setAbateTime(AbateTime);
                            pp.setAccidentType(resultSet.getString("AccidentType"));
                            pp.setActualValueLower(resultSet.getBigDecimal("ActualValueLower"));
                            pp.setActualValueUpper(resultSet.getBigDecimal("ActualValueUpper"));
                            pp.setAgreementProportion(resultSet.getBigDecimal("AgreementProportion"));
                            pp.setAgeLower(resultSet.getBigDecimal("AgeLower"));
                            pp.setAgentCode(resultSet.getString("agentCode"));
                            pp.setAgeUpper(resultSet.getBigDecimal("ageUpper"));
                            pp.setAirBagsLower(resultSet.getInt("AirBagsLower"));
                            pp.setAirBagsUpper(resultSet.getInt("airBagsUpper"));
                            pp.setAmountLower(resultSet.getBigDecimal("amountLower"));
                            pp.setAmountUpper(resultSet.getBigDecimal("amountUpper"));
                            pp.setAppointRepairShop(resultSet.getString("appointRepairShop"));
                            pp.setBranchCode(resultSet.getString("branchCode"));
                            pp.setBrandCode(resultSet.getString("brandCode"));
                            pp.setBuisnessNo(resultSet.getString("BuisnessNo"));
                            pp.setBusinessNature(resultSet.getString("businessNature"));
                            pp.setCarKindCode(resultSet.getString("carKindCode"));
                            pp.setCarOwnerType(resultSet.getString("carOwnerType"));
                            pp.setClaimAdjustReason(resultSet.getString("claimAdjustReason"));
                            pp.setClauseType(resultSet.getString("clauseType"));
                            pp.setComCode(resultSet.getString("comCode"));
                            pp.setContractNo(resultSet.getString("contractNo"));
                            pp.setCountryNature(resultSet.getString("countryNature"));
                            pp.setCustomerTypeCode(resultSet.getString("customerTypeCode"));
                            pp.setDeductible(resultSet.getBigDecimal("deductible"));
                            pp.setDeductiblerate(resultSet.getBigDecimal("deductiblerate"));
                                pp.setDisplacementLower(resultSet.getBigDecimal("displacementLower"));
                            pp.setDisplacementUpper(resultSet.getBigDecimal("displacementUpper"));
                            pp.setDriveramountLower(resultSet.getBigDecimal("driveramountLower"));
                            pp.setDriveramountUpper(resultSet.getBigDecimal("driveramountUpper"));
                            pp.setDrivingYearsLower(resultSet.getBigDecimal("drivingYearsLower"));
                            pp.setDrivingYearsUpper(resultSet.getBigDecimal("drivingYearsUpper"));
                            pp.setEditionNo(resultSet.getString("editionNo"));
                            Date effectiveTime = null;
                            if(resultSet.getDate("effectiveTime") != null) {
                                    effectiveTime = new Date(resultSet.getDate("effectiveTime").getTime());
                            }
                            pp.setEffectiveTime(effectiveTime);
                            pp.setFactorCode(resultSet.getString("factorCode"));
                            pp.setFactorValue(resultSet.getBigDecimal("factorValue"));
                            pp.setFactorValue2(resultSet.getBigDecimal("factorValue2"));
                            pp.setFamilyCode(resultSet.getString("familyCode"));
                            pp.setGroupCode(resultSet.getString("groupCode"));
                            pp.setHandler1Code(resultSet.getString("handler1Code"));
                            pp.setHandlerCode(resultSet.getString("handlerCode"));
                            Date insertTimeForHis = null;
                            if(resultSet.getDate("insertTimeForHis") != null) {
                                insertTimeForHis = new Date(resultSet.getDate("insertTimeForHis").getTime());
                            }
                            pp.setInsertTimeForHis(insertTimeForHis);
                            pp.setInsuredCode(resultSet.getString("insuredCode"));
                            pp.setInsuredFlag051029(resultSet.getString("insuredFlag051029"));
                            pp.setInsuredType(resultSet.getString("insuredType"));
                            pp.setInsuredType_T(resultSet.getString("insuredType_T"));
                            pp.setIsNolocalCar(resultSet.getString("isNolocalCar"));
                            pp.setIsOwnerInsurant(resultSet.getString("isOwnerInsurant"));
                            pp.setKindCode(resultSet.getString("kindCode"));
                            pp.setLastDamagedLower(resultSet.getBigDecimal("lastDamagedLower"));
                            pp.setLastDamagedLower1(resultSet.getBigDecimal("lastDamagedLower1"));
                            pp.setLastDamagedLower2(resultSet.getBigDecimal("lastDamagedLower2"));
                            pp.setLastDamagedLower3(resultSet.getBigDecimal("lastDamagedLower3"));
                            pp.setLastDamagedUpper(resultSet.getBigDecimal("lastDamagedUpper"));
                            pp.setLastDamagedUpper1(resultSet.getBigDecimal("lastDamagedUpper1"));
                            pp.setLastDamagedUpper2(resultSet.getBigDecimal("lastDamagedUpper2"));
                            pp.setLastDamagedUpper3(resultSet.getBigDecimal("lastDamagedUpper3"));
                            pp.setLastPaidRateLower(resultSet.getBigDecimal("lastPaidRateLower"));
                            pp.setLastPaidRateUpper(resultSet.getBigDecimal("lastPaidRateUpper"));
                            pp.setLastPlatPaidRateUpper(resultSet.getBigDecimal("lastPlatPaidRateUpper"));
                            pp.setLastPlatPaidRateLower(resultSet.getBigDecimal("lastPlatPaidRateLower"));
                            pp.setModeCode(resultSet.getString("modeCode"));
                            pp.setModelCode(resultSet.getString("modelCode"));
                            pp.setNodamYearsLower(resultSet.getBigDecimal("nodamYearsLower"));
                            pp.setNodamYearsLower1(resultSet.getBigDecimal("nodamYearsLower1"));
                            pp.setNodamYearsLower2(resultSet.getBigDecimal("nodamYearsLower2"));
                            pp.setNodamYearsLower3(resultSet.getBigDecimal("nodamYearsLower3"));
                            pp.setNodamYearsUpper(resultSet.getBigDecimal("nodamYearsUpper"));
                            pp.setNodamYearsUpper1(resultSet.getBigDecimal("nodamYearsUpper1"));
                            pp.setNodamYearsUpper2(resultSet.getBigDecimal("nodamYearsUpper2"));
                            pp.setNodamYearsUpper3(resultSet.getBigDecimal("nodamYearsUpper3"));
                            pp.setNrrFlag(resultSet.getString("nrrFlag"));
                            Date operateTimeForHis = null;
                            if(resultSet.getDate("operateTimeForHis") != null) {
                                operateTimeForHis = new Date(resultSet.getDate("operateTimeForHis").getTime());
                            }
                            pp.setOperateTimeForHis(operateTimeForHis);
                            pp.setPassengeramountLower(resultSet.getBigDecimal("passengeramountLower"));
                            pp.setPassengeramountUpper(resultSet.getBigDecimal("passengeramountUpper"));
                            pp.setPowerLower(resultSet.getBigDecimal("powerLower"));
                            pp.setPowerUpper(resultSet.getBigDecimal("PowerUpper"));
                            pp.setProjectCode(resultSet.getString("projectCode"));
                            pp.setProposalCase(resultSet.getString("proposalCase"));
                            pp.setPurchasePriceLower(resultSet.getBigDecimal("purchasePriceLower"));
                            pp.setPurchasePriceUpper(resultSet.getBigDecimal("purchasePriceUpper"));
                            pp.setRecommendCode(resultSet.getString("recommendCode"));
                            pp.setRenewalYears(resultSet.getString("renewalYears"));
                            pp.setRunMilesLower(resultSet.getBigDecimal("runMilesLower"));
                            pp.setRunMilesUpper(resultSet.getBigDecimal("runMilesUpper"));
                            pp.setScratchAmountLower(resultSet.getBigDecimal("scratchAmountLower"));
                            pp.setScratchAmountUpper(resultSet.getBigDecimal("scratchAmountUpper"));
                            pp.setSeatCountLower(resultSet.getBigDecimal("seatCountLower"));
                            pp.setSeatCountUpper(resultSet.getBigDecimal("seatCountUpper"));
                            pp.setSeqId(resultSet.getBigDecimal("seqId"));
                            pp.setSex(resultSet.getString("sex"));
                            pp.setSpecialTreaTreason(resultSet.getString("specialTreaTreason"));
                            pp.setTonCountLower(resultSet.getBigDecimal("tonCountLower"));
                            pp.setTonCountUpper(resultSet.getBigDecimal("tonCountUpper"));
                            pp.setUnitType(resultSet.getString("unitType"));
                            pp.setUseNatureCode(resultSet.getString("useNatureCode"));
                            pp.setUseYearsLower(resultSet.getBigDecimal("useYearsLower"));
                            pp.setUseYearsUpper(resultSet.getBigDecimal("useYearsUpper"));
                            pp.setVehicleClass(resultSet.getString("vehicleClass"));
                            pp.setVehicleMaker(resultSet.getString("vehicleMaker"));
                            pp.setDtckFlag(resultSet.getString("dtckFlag"));

                            list.add(pp);

                        }
                    } catch (SQLException e) {
                        System.out.println("查詢失敗...");
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        System.out.println("查詢失敗...");
                        e.printStackTrace();
                    } finally {
                        try {
                            resultSet.close();
                        } catch (SQLException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                        try {
                            stmt.close();
                        } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        try {
                            conn.close();
                        } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
            }catch(Exception e) {}

            System.out.println("list数据初始化结束...");
        }
    }
    public TestForRiskPremrateTable(int start,int end) {
        this.start = start;
        this.end = end;
    }


    private  int start;
    private  int end;

    @Override
    public void run() {
        super.run();

        System.out.println("开始插入list-->"+start+"-"+end+"--->数据");
        insertDate(client);
    }

    public void insertDate(Client client) {
             //不开线程会慢死
             System.out.println(start+"-"+end+"进入insertDate");
             //批处理方式插入elasticsearch
             BulkProcessor bulkProcessor = BulkProcessor.builder(client, new BulkProcessor.Listener() {

                    public void beforeBulk(long executionId, BulkRequest request) {
                        System.out.println("待请求数量:"+request.numberOfActions());
                    }

                    public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {
                        if(!response.hasFailures()) {
                            System.out.println("执行成功!");
                        }else {
                            System.out.println("执行失败!");
                        }

                    }

                    public void afterBulk(long executionId, BulkRequest request, Throwable failure) {
                        System.out.println("失败的错误信息:"+failure.getMessage());

                    } 
                })
                     .setBulkActions(15000)   
                        .setBulkSize(new ByteSizeValue(1, ByteSizeUnit.GB))   
                        .setFlushInterval(TimeValue.timeValueSeconds(5))   
                        .build();  
                // 添加请求

             //添加新的信息
                for (int i = start;i<=end;i++) {
                    PrpFriskPremrateTable prpFriskPremrateTable = (PrpFriskPremrateTable) list.get(i);

                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    try {
                        XContentBuilder builder = XContentFactory.jsonBuilder()
                        .startObject()
                            .field("abateTime",prpFriskPremrateTable.getAbateTime() == null ? null :sdf.format(prpFriskPremrateTable.getAbateTime()))
                            .field("effectiveTime",prpFriskPremrateTable.getEffectiveTime() == null ? null :sdf.format(prpFriskPremrateTable.getEffectiveTime()))
                            .field("insertTimeForHis",prpFriskPremrateTable.getInsertTimeForHis() == null ?null:sdf.format(prpFriskPremrateTable.getInsertTimeForHis()))
                            .field("operateTimeForHis",prpFriskPremrateTable.getOperateTimeForHis() == null ? null : sdf.format(prpFriskPremrateTable.getOperateTimeForHis()))
                            .field("accidentType",prpFriskPremrateTable.getAccidentType())
                            .field("actualValueLower",prpFriskPremrateTable.getActualValueLower())
                            .field("actualValueUpper",prpFriskPremrateTable.getActualValueUpper())
                            .field("agreementProportion",prpFriskPremrateTable.getAgreementProportion())
                            .field("ageLower",prpFriskPremrateTable.getAgeLower())
                            .field("ageUpper",prpFriskPremrateTable.getAgeUpper())
                            .field("airBagsLower",prpFriskPremrateTable.getAirBagsLower())
                            .field("airBagsUpper",prpFriskPremrateTable.getAirBagsUpper())
                            .field("amountLower",prpFriskPremrateTable.getAmountLower())
                            .field("amountUpper",prpFriskPremrateTable.getAmountUpper())
                            .field("agentCode",prpFriskPremrateTable.getAgentCode())
                            .field("appointRepairShop",prpFriskPremrateTable.getAppointRepairShop())
                            .field("branchCode",prpFriskPremrateTable.getBranchCode())
                            .field("brandCode",prpFriskPremrateTable.getBrandCode())
                            .field("buisnessNo",prpFriskPremrateTable.getBuisnessNo())
                            .field("businessNature",prpFriskPremrateTable.getBusinessNature())
                            .field("carKindCode",prpFriskPremrateTable.getCarKindCode())
                            .field("carOwnerType",prpFriskPremrateTable.getCarOwnerType())
                            .field("claimAdjustReason",prpFriskPremrateTable.getClaimAdjustReason())
                            .field("clauseType",prpFriskPremrateTable.getClauseType())
                            .field("comCode",prpFriskPremrateTable.getComCode())
                            .field("contractNo",prpFriskPremrateTable.getContractNo())
                            .field("countryNature",prpFriskPremrateTable.getCountryNature())
                            .field("customerTypeCode",prpFriskPremrateTable.getCustomerTypeCode())
                            .field("deductible",prpFriskPremrateTable.getDeductible())
                            .field("deductiblerate",prpFriskPremrateTable.getDeductiblerate())
                            .field("dtckFlag",prpFriskPremrateTable.getDtckFlag())
                            .field("displacementLower",prpFriskPremrateTable.getDisplacementLower())
                            .field("displacementUpper",prpFriskPremrateTable.getDisplacementUpper())
                            .field("driveramountUpper",prpFriskPremrateTable.getDriveramountUpper())
                            .field("driveramountLower",prpFriskPremrateTable.getDriveramountLower())
                            .field("drivingYearsUpper",prpFriskPremrateTable.getDrivingYearsUpper())
                            .field("drivingYearsLower",prpFriskPremrateTable.getDrivingYearsLower())
                            .field("editionNo",prpFriskPremrateTable.getEditionNo())
                            .field("factorCode",prpFriskPremrateTable.getFactorCode())
                            .field("factorValue",prpFriskPremrateTable.getFactorValue())
                            .field("factorValue2",prpFriskPremrateTable.getFactorValue2())
                            .field("familyCode",prpFriskPremrateTable.getFamilyCode())
                            .field("groupCode",prpFriskPremrateTable.getGroupCode())
                            .field("handler1Code",prpFriskPremrateTable.getHandler1Code())
                            .field("handlerCode",prpFriskPremrateTable.getHandlerCode())
                            .field("insuredCode",prpFriskPremrateTable.getInsuredCode())
                            .field("insuredFlag051029",prpFriskPremrateTable.getInsuredFlag051029())
                            .field("insuredType",prpFriskPremrateTable.getInsuredType())
                            .field("insuredType_T",prpFriskPremrateTable.getInsuredType_T())
                            .field("isNolocalCar",prpFriskPremrateTable.getIsNolocalCar())
                            .field("isOwnerInsurant",prpFriskPremrateTable.getIsOwnerInsurant())
                            .field("kindCode",prpFriskPremrateTable.getKindCode())
                            .field("lastDamagedLower",prpFriskPremrateTable.getLastDamagedLower())
                            .field("lastDamagedLower1",prpFriskPremrateTable.getLastDamagedLower1())
                            .field("lastDamagedLower2",prpFriskPremrateTable.getLastDamagedLower2())
                            .field("lastDamagedLower3",prpFriskPremrateTable.getLastDamagedLower3())
                            .field("lastDamagedUpper",prpFriskPremrateTable.getLastDamagedUpper())
                            .field("lastDamagedUpper1",prpFriskPremrateTable.getLastDamagedUpper1())
                            .field("lastDamagedUpper2",prpFriskPremrateTable.getLastDamagedUpper2())
                            .field("lastDamagedUpper3",prpFriskPremrateTable.getLastDamagedUpper3())
                            .field("lastPaidRateLower",prpFriskPremrateTable.getLastPaidRateLower())
                            .field("lastPaidRateUpper",prpFriskPremrateTable.getLastPaidRateUpper())
                            .field("lastPlatPaidRateLower",prpFriskPremrateTable.getLastPlatPaidRateLower())
                            .field("lastPlatPaidRateUpper",prpFriskPremrateTable.getLastPlatPaidRateUpper())
                            .field("modeCode",prpFriskPremrateTable.getModeCode())
                            .field("modelCode",prpFriskPremrateTable.getModelCode())
                            .field("nodamYearsLower",prpFriskPremrateTable.getNodamYearsLower())
                            .field("nodamYearsLower1",prpFriskPremrateTable.getNodamYearsLower1())
                            .field("nodamYearsLower2",prpFriskPremrateTable.getNodamYearsLower2())
                            .field("nodamYearsLower3",prpFriskPremrateTable.getNodamYearsLower3())
                            .field("nodamYearsUpper",prpFriskPremrateTable.getNodamYearsUpper())
                            .field("nodamYearsUpper1",prpFriskPremrateTable.getNodamYearsUpper1())
                            .field("nodamYearsUpper2",prpFriskPremrateTable.getNodamYearsUpper2())
                            .field("nodamYearsUpper3",prpFriskPremrateTable.getNodamYearsUpper3())
                            .field("nrrFlag",prpFriskPremrateTable.getNrrFlag())
                            .field("passengeramountLower",prpFriskPremrateTable.getPassengeramountLower())
                            .field("passengeramountUpper",prpFriskPremrateTable.getPassengeramountUpper())
                            .field("powerLower",prpFriskPremrateTable.getPowerLower())
                            .field("powerUpper",prpFriskPremrateTable.getPowerUpper())
                            .field("projectCode",prpFriskPremrateTable.getProjectCode())
                            .field("proposalCase",prpFriskPremrateTable.getProposalCase())
                            .field("purchasePriceLower",prpFriskPremrateTable.getPurchasePriceLower())
                            .field("purchasePriceUpper",prpFriskPremrateTable.getPurchasePriceUpper())
                            .field("recommendCode",prpFriskPremrateTable.getRecommendCode())
                            .field("renewalYears",prpFriskPremrateTable.getRenewalYears())
                            .field("runMilesUpper",prpFriskPremrateTable.getRunMilesUpper())
                            .field("runMilesLower",prpFriskPremrateTable.getRunMilesLower())
                            .field("scratchAmountLower",prpFriskPremrateTable.getScratchAmountLower())
                            .field("scratchAmountUpper",prpFriskPremrateTable.getScratchAmountUpper())
                            .field("seatCountLower",prpFriskPremrateTable.getSeatCountLower())
                            .field("seatCountUpper",prpFriskPremrateTable.getSeatCountUpper())
                            .field("seqId",prpFriskPremrateTable.getSeqId())
                            .field("sex",prpFriskPremrateTable.getSex())
                            .field("tonCountLower",prpFriskPremrateTable.getTonCountLower())
                            .field("tonCountUpper",prpFriskPremrateTable.getTonCountUpper())
                            .field("useYearsLower",prpFriskPremrateTable.getUseYearsLower())
                            .field("useYearsUpper",prpFriskPremrateTable.getUseYearsUpper())
                            .field("specialTreaTreason",prpFriskPremrateTable.getSpecialTreaTreason())
                            .field("unitType",prpFriskPremrateTable.getUnitType())
                            .field("useNatureCode",prpFriskPremrateTable.getUseNatureCode())
                            .field("vehicleClass",prpFriskPremrateTable.getVehicleClass())
                            .field("vehicleMaker",prpFriskPremrateTable.getVehicleMaker())

                        .endObject();

                        bulkProcessor.add(client.prepareIndex("cqp","riskpremrate")
                                .setSource(builder).request());
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
        // 冲刷所有的请求
        bulkProcessor.flush();
        // 不需要批处理器可以关闭
        bulkProcessor.close();
        // 刷新索引
        client.admin().indices().prepareRefresh().get();
    }


    public static List<PrpFriskPremrateTable> getList() {
        return list;
    }
    public static void setList(List<PrpFriskPremrateTable> list) {
        TestForRiskPremrateTable.list = list;
    }
    public int getStart() {
        return start;
    }
    public void setStart(int start) {
        this.start = start;
    }
    public int getEnd() {
        return end;
    }
    public void setEnd(int end) {
        this.end = end;
    }
    public static Client getClient() {
        return client;
    }
    public static void setClient(Client client) {
        TestForRiskPremrateTable.client = client;
    }




}

3.测试类

package com.sinosoft.doc.demo;

import static org.elasticsearch.index.query.QueryBuilders.rangeQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;

import com.sinosoft.doc.data.InitialData;
/**
 * Servlet implementation class TestForRiskPreamrateServlet
 */
public class TestForRiskPreamrateMain { 
    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        //在测试查询速度前需插入数据 -- 此方法需单独调用,剩下的需注释,反之此方法注释
        //indexPrepareForQuery();

        //准备好了以后就可以测试查询速度啦
        long start = System.currentTimeMillis();

        InitialData init = new InitialData();
        // 获得客户端连接
        Client client = null;
        try {
            client = init.connect("bdrg", true, 100, "127.0.0.1", 9300);
            //查询是否插入正确量的数据  -- 数据的总量size
            /*SearchResponse response = client.prepareSearch("cqp")
                    .setTypes("riskpremrate")
                    .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
                    .get();
            SearchHits searchHits = response.getHits();
            System.out.println("elasticsearch已插入"+response.getHits().getTotalHits()+"条数据");*/
/*          
            from PrpFriskPremrateTable where id.comCode='32000000' 
                    and kindCode in ('050202') and id.editionNo='6' and
                    (branchCode in ('32010000','32000000')) and
                    (handler1Code='84326631' or handler1Code is null) and (businessNature='1' or businessNature is null) and
                    (clauseType='F42' or clauseType is null) and 
                    (useNatureCode='211' or useNatureCode is null) 
                    and (agentCode='000011000001' or agentCode is null) and (contractNo is null) 
                    and (nrrFlag='5' or nrrFlag is null) and
                    (carKindCode='A01' or carKindCode is null) and 
                    (claimAdjustReason='NULL' or claimAdjustReason is null) 
                    and ((driveramountLower<='0' and driveramountUpper>'0') or (driveramountLower is null and driveramountUpper is null)) 
                    and ((passengeramountLower<='0' and passengeramountUpper>'0') 
                            or (passengeramountLower is null and passengeramountUpper is null)) and
                    (specialTreaTreason='NULL' or specialTreaTreason is null) and 
                    ((purchasePriceLower<='199800.00' and purchasePriceUpper>'199800.00') or 
                    (purchasePriceLower is null and purchasePriceUpper is null)) and 
                    ((actualValueLower<='157842.00' and actualValueUpper>'157842.00') or (actualValueLower is null and actualValueUpper is null)) and 
                    ((displacementLower<='2.4880' and displacementUpper>'2.4880') or
                            (displacementLower is null and displacementUpper is null)) and
                    ((powerLower<='126' and powerUpper>'126') or (powerLower is null and powerUpper is null)) 
                    and ((seatCountLower<='5' and seatCountUpper>'5') or (seatCountLower is null and seatCountUpper is null))
                    and ((tonCountLower<='0' and tonCountUpper>'0') or (tonCountLower is null and tonCountUpper is null)) and 
                    ((useYearsLower<='3' and useYearsUpper>'3') or (useYearsLower is null and useYearsUpper is null)) and 
                    ((lastDamagedLower<='2' and lastDamagedUpper>='2') or (lastDamagedLower is null and lastDamagedUpper is null)) and 
                    (lastDamagedLower1 is null and lastDamagedUpper1 is null) and 
                    (lastDamagedLower2 is null and lastDamagedUpper2 is null) and 
                    (lastDamagedLower3 is null and lastDamagedUpper3 is null) and 
                    ((nodamYearsLower<='0' and nodamYearsUpper>='0') or (nodamYearsLower is null and nodamYearsUpper is null)) 
                    and (nodamYearsLower1 is null and nodamYearsUpper1 is null) and (nodamYearsLower2 is null and nodamYearsUpper2 is null) and (nodamYearsLower3 is null and nodamYearsUpper3 is null) 
                    and ((amountLower<='0' and amountUpper>'0') or (amountLower is null and amountUpper is null)) and 
                    ((scratchAmountLower<='0.00' and scratchAmountUpper>'0.00') or 
                            (scratchAmountLower is null and scratchAmountUpper is null)) and 
                    (appointRepairShop='1' or appointRepairShop is null) and (unitType is null) and 
                    (customerTypeCode='02') and (proposalCase='050202' or proposalCase is null) and 
                    (runMilesLower is null and runMilesUpper is null) and 
                    ((drivingYearsLower<='0' and drivingYearsUpper>'0') or (drivingYearsLower is null and drivingYearsUpper is null)) 
                    and (countryNature='02' or countryNature is null) and (modeCode is null) 
                    and (sex='1' or sex is null) and ((ageLower<='44' and ageUpper>'44') or (ageLower is null and ageUpper is null)) 
                    and ((lastPaidRateLower<='0.000000' and lastPaidRateUpper>'0.000000') or 
                            (lastPaidRateLower is null and lastPaidRateUpper is null)) and (
                                    (lastPlatPaidRateLower<='0.000000' and lastPlatPaidRateUpper>'0.000000') or 
                                    (lastPlatPaidRateLower is null and lastPlatPaidRateUpper is null)) and 
                    (carOwnerType='1' or carOwnerType is null) and (insuredType='1' or insuredType is null) and 
                    (insuredCode='4300100004488875' or insuredCode is null) and (buisnessNo is null) and 
                    (handlerCode='84326631' or handlerCode is null) and (projectCode is null) and 
                    (recommendCode='4301998000351' or recommendCode is null) and (accidentType is null) and 
                    (vehicleMaker='MK0182' or vehicleMaker is null or vehicleMaker='OTH') and 
                    (brandCode='LNA' or brandCode is null or brandCode='OTH') and 
                    (familyCode='LNAAM' or familyCode is null or familyCode='OTH') and 
                    (groupCode='LNA0AM01' or groupCode is null or groupCode='OTH') and 
                    (vehicleClass='瓒婇噹杞︾被' or vehicleClass is null) and 
                    (modelCode='LNAAMI0011' or modelCode is null or modelCode='OTH') and 
                    (agreementProportion='50' or agreementProportion is null) and 
                    (dtckflag='F' or dtckflag is null) and (renewalYears = 'D' or renewalYears is null) 
                    and (isNolocalCar = '0' or isNolocalCar is null) and 
                    ((airBagsLower <= '0' and airBagsUpper>'0') or (airBagsLower is null and airBagsUpper is null)) 
                    and (insuredType_T = '1' or insuredType_T is null) and (deductible = '500.00' or deductible is null) 
                    and (deductibleRate = '0' or deductibleRate is null) and (insuredFlag051029 = '0' or insuredFlag051029 is null)
    */

            //验证查询时间
            SearchRequestBuilder request = client.prepareSearch("cqp")
                    .setTypes("riskpremrate")
                    .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) 
                    .setQuery(QueryBuilders.boolQuery()
                            .must(termQuery("comCode","32000000"))
                            .must(termQuery("kindCode","050202"))
                            .must(termQuery("editionNo","6"))
                            .must(QueryBuilders.boolQuery().should(termQuery("branchCode","32010000")).should(termQuery("branchCode","32000000")))
                            .must(QueryBuilders.boolQuery().should(termQuery("handler1Code","84326631")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("handler1Code"))))
                            .must(QueryBuilders.boolQuery().should(termQuery("businessNature","1")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("businessNature"))))
                            .must(QueryBuilders.boolQuery().should(termQuery("clauseType","F42")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("clauseType"))))
                            .must(QueryBuilders.boolQuery().should(termQuery("useNatureCode","211")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("useNatureCode"))))
                            .must(QueryBuilders.boolQuery().should(termQuery("agentCode","000011000001")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("agentCode"))))
                            .must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("contractNo")))
                            .must(QueryBuilders.boolQuery().should(termQuery("nrrFlag","5")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("nrrFlag"))))
                            .must(QueryBuilders.boolQuery().should(termQuery("carKindCode","A01")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("carKindCode"))))
                            .must(QueryBuilders.boolQuery().should(termQuery("claimAdjustReason","NULL")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("claimAdjustReason"))))

                            .must(QueryBuilders.boolQuery().should(QueryBuilders.boolQuery().filter(rangeQuery("driveramountLower").lte(0)).filter(rangeQuery("driveramountUpper").gt(0))).should(QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("driveramountLower"))).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("driveramountUpper")))))
                            .must(QueryBuilders.boolQuery().should(termQuery("specialTreaTreason","NULL")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("specialTreaTreason"))))
                            .must(QueryBuilders.boolQuery().should(QueryBuilders.boolQuery().filter(rangeQuery("passengeramountLower").lte(0)).filter(rangeQuery("passengeramountUpper").gt(0))).should(QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("passengeramountLower"))).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("passengeramountLower")))))
                            .must(QueryBuilders.boolQuery().should(QueryBuilders.boolQuery().filter(rangeQuery("purchasePriceLower").lte(199800)).filter(rangeQuery("purchasePriceUpper").gt(199800))).should(QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("purchasePriceLower"))).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("purchasePriceUpper")))))
                            .must(QueryBuilders.boolQuery().should(QueryBuilders.boolQuery().filter(rangeQuery("actualValueLower").lte(157842)).filter(rangeQuery("actualValueUpper").gt(157842))).should(QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("actualValueLower"))).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("actualValueUpper")))))
                            .must(QueryBuilders.boolQuery().should(QueryBuilders.boolQuery().filter(rangeQuery("displacementLower").lte(2.488)).filter(rangeQuery("displacementUpper").gt(2.488))).should(QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("displacementLower"))).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("displacementUpper")))))
                            .must(QueryBuilders.boolQuery().should(QueryBuilders.boolQuery().filter(rangeQuery("powerLower").lte(126)).filter(rangeQuery("powerUpper").gt(126))).should(QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("powerLower"))).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("powerUpper")))))
                            .must(QueryBuilders.boolQuery().should(QueryBuilders.boolQuery().filter(rangeQuery("seatCountLower").lte(5)).filter(rangeQuery("seatCountUpper").gt(5))).should(QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("seatCountLower"))).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("seatCountUpper")))))
                            .must(QueryBuilders.boolQuery().should(QueryBuilders.boolQuery().filter(rangeQuery("tonCountLower").lte(0)).filter(rangeQuery("tonCountUpper").gt(0))).should(QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("tonCountLower"))).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("tonCountUpper")))))
                            .must(QueryBuilders.boolQuery().should(QueryBuilders.boolQuery().filter(rangeQuery("useYearsLower").lte(3)).filter(rangeQuery("useYearsUpper").gt(3))).should(QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("useYearsLower"))).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("useYearsUpper")))))
                            .must(QueryBuilders.boolQuery().should(QueryBuilders.boolQuery().filter(rangeQuery("lastDamagedLower").lte(2)).filter(rangeQuery("lastDamagedUpper").gt(2))).should(QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("lastDamagedLower"))).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("lastDamagedUpper")))))


                            .must(QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("lastDamagedLower1"))).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("lastDamagedUpper1"))))
                            .must(QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("lastDamagedLower2"))).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("lastDamagedUpper2"))))
                            .must(QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("lastDamagedLower3"))).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("lastDamagedUpper3"))))
                            .must(QueryBuilders.boolQuery().should(QueryBuilders.boolQuery().filter(rangeQuery("nodamYearsLower").lte(0)).filter(rangeQuery("nodamYearsUpper").gt(0))).should(QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("nodamYearsLower"))).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("nodamYearsUpper")))))

                            .must(QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("nodamYearsLower1"))).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("nodamYearsUpper1"))))
                            .must(QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("nodamYearsLower2"))).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("nodamYearsUpper2"))))
                            .must(QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("nodamYearsLower3"))).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("nodamYearsUpper3"))))

                            .must(QueryBuilders.boolQuery().should(QueryBuilders.boolQuery().filter(rangeQuery("amountLower").lte(0)).filter(rangeQuery("amountUpper").gt(0))).should(QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("amountLower"))).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("amountUpper")))))
                            .must(QueryBuilders.boolQuery().should(QueryBuilders.boolQuery().filter(rangeQuery("scratchAmountLower").lte(0)).filter(rangeQuery("scratchAmountUpper").gt(0))).should(QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("scratchAmountLower"))).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("scratchAmountUpper")))))

                            .must(QueryBuilders.boolQuery().should(termQuery("appointRepairShop","1")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("appointRepairShop"))))
                            .must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("unitType")))
                            .must(termQuery("customerTypeCode","02"))
                            .must(QueryBuilders.boolQuery().should(termQuery("proposalCase","050202")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("proposalCase"))))
                            .must(QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("runMilesLower"))).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("runMilesUpper"))))

                            .must(QueryBuilders.boolQuery().should(QueryBuilders.boolQuery().filter(rangeQuery("drivingYearsLower").lte(0)).filter(rangeQuery("drivingYearsUpper").gt(0))).should(QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("drivingYearsLower"))).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("drivingYearsUpper")))))

                            .must(QueryBuilders.boolQuery().should(termQuery("countryNature","02")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("countryNature"))))
                            .must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("modeCode")))
                            .must(QueryBuilders.boolQuery().should(termQuery("sex","1")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("sex"))))

                            .must(QueryBuilders.boolQuery().should(QueryBuilders.boolQuery().filter(rangeQuery("ageLower").lte(44)).filter(rangeQuery("ageUpper").gt(44))).should(QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("ageLower")).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("ageUpper"))))))
                            .must(QueryBuilders.boolQuery().should(QueryBuilders.boolQuery().filter(rangeQuery("lastPaidRateLower").lte(0)).filter(rangeQuery("lastPaidRateUpper").gt(0))).should(QueryBuilders.boolQuery().mustNot((QueryBuilders.existsQuery("lastPaidRateLower"))).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("lastPaidRateUpper")))))
                            .must(QueryBuilders.boolQuery().should(QueryBuilders.boolQuery().filter(rangeQuery("lastPlatPaidRateLower").lte(0)).filter(rangeQuery("lastPlatPaidRateUpper").gt(0))).should(QueryBuilders.boolQuery().mustNot((QueryBuilders.existsQuery("lastPlatPaidRateLower"))).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("lastPlatPaidRateUpper")))))


                            .must(QueryBuilders.boolQuery().should(termQuery("carOwnerType","1")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("carOwnerType"))))
                            .must(QueryBuilders.boolQuery().should(termQuery("insuredType","1")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("insuredType"))))
                            .must(QueryBuilders.boolQuery().should(termQuery("insuredCode","4300100004488875")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("insuredCode"))))
                            .must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("buisnessNo")))
                            .must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("projectCode")))
                            .must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("accidentType")))
                            .must(QueryBuilders.boolQuery().should(termQuery("handlerCode","84326631")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("handlerCode"))))
                            .must(QueryBuilders.boolQuery().should(termQuery("recommendCode","4301998000351")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("recommendCode"))))
                            .must(QueryBuilders.boolQuery().should(termQuery("vehicleMaker","MK0182")).should(termQuery("vehicleMaker","OTH")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("insuredType"))))
                            .must(QueryBuilders.boolQuery().should(termQuery("brandCode","LNA")).should(termQuery("brandCode","OTH")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("brandCode"))))
                            .must(QueryBuilders.boolQuery().should(termQuery("familyCode","LNAAM")).should(termQuery("familyCode","OTH")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("familyCode"))))
                            .must(QueryBuilders.boolQuery().should(termQuery("groupCode","LNA0AM01")).should(termQuery("groupCode","OTH")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("groupCode"))))
                            .must(QueryBuilders.boolQuery().should(termQuery("modelCode","LNAAMI0011")).should(termQuery("modelCode","OTH")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("modelCode"))))
                            .must(QueryBuilders.boolQuery().should(termQuery("vehicleClass","A")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("vehicleClass"))))
//                          
                            .must(QueryBuilders.boolQuery().should(termQuery("dtckFlag","F")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("dtckFlag"))))
                            .must(QueryBuilders.boolQuery().should(termQuery("renewalYears","D")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("renewalYears"))))
                            .must(QueryBuilders.boolQuery().should(termQuery("isNolocalCar","0")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("isNolocalCar"))))
                            .must(QueryBuilders.boolQuery().should(termQuery("agreementProportion","50")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("agreementProportion"))))




                            /*gt :: 大于 
//                          gte:: 大于等于 
//                          lt :: 小于 
//                          lte:: 小于等于*/

                            //数值默认会存0而不是null
                            //.must(QueryBuilders.boolQuery().should(QueryBuilders.boolQuery().filter(rangeQuery("airBagsLower").lte(0)).filter(rangeQuery("airBagsUpper").gt(0))).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("airBagsLower")).must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("airBagsUpper")))))
                            .must(QueryBuilders.boolQuery().should(QueryBuilders.boolQuery().filter(rangeQuery("airBagsLower").lte(0)).filter(rangeQuery("airBagsUpper").gt(0))).should(QueryBuilders.boolQuery().must(termQuery("airBagsLower",0)).must(QueryBuilders.boolQuery().must(termQuery("airBagsUpper",0)))))

                            .must(QueryBuilders.boolQuery().should(termQuery("insuredType_T","1")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("insuredType_T"))))
                            .must(QueryBuilders.boolQuery().should(termQuery("deductible","500")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("deductible"))))
                            .must(QueryBuilders.boolQuery().should(termQuery("deductibleRate","0")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("deductibleRate"))))
                            .must(QueryBuilders.boolQuery().should(termQuery("insuredFlag051029","0")).should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("insuredFlag051029"))))

                            );
            //将请求串保存在文件里-json 可以在riskpremrate.json中观看此查询json
            //writeJsonInFile(request.toString());

            //我们可以用json方式查询此结果
            String result = requestToElasticSearch("http://localhost:9200/cqp/_search",request.toString());

            //打印结果
            System.out.println("******code request******");
            printResponseHits(request.get());
            System.out.println("******json request******");
            System.out.println(result);

        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }

        long queryEnd = System.currentTimeMillis();
        System.out.println("查询数据共用了("+(queryEnd-start)/1000+")s");
    }

        private static String requestToElasticSearch(String urlString, String param){
            String result = "";
            try {
                URL url = new URL(urlString);// 创建连接
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setUseCaches(false);
                connection.setInstanceFollowRedirects(true);
                connection.setRequestMethod("POST"); // 设置请求方式
                connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
                connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
                connection.connect();
                OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码
                out.append(param);
                out.flush();
                out.close();
                // 读取响应
                InputStream is = connection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                String readLine = null;
                StringBuffer sbf = new StringBuffer();
                while ((readLine = reader.readLine()) != null) {
                    sbf.append(readLine);
                }
                reader.close();
                result = sbf.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return result;
        }

    //将请求串保存在文件里
   private static void writeJsonInFile(String json)  {
      // System.out.println(json);
       StringReader sr = null;
       OutputStream out = null;
        try {
            sr = new StringReader(json);
            out = new FileOutputStream("riskpremrate.json");
             int tempbyte;  
              while ((tempbyte = sr.read()) != -1) {  
                  out.write(tempbyte);
              }  
             out.flush(); 
        } catch (FileNotFoundException e) {
            System.out.println("找不到文件!");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("文件读写发生错误!");
            e.printStackTrace();
        } finally {
            sr.close();  
            try {
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }



    }

   //查看结果
    private static void printResponseHits(SearchResponse response) {
        SearchHits searchHits = response.getHits();
        System.out.println("查到了"+searchHits.getTotalHits()+"条数据");
        Iterator<SearchHit> iterator = searchHits.iterator();
        while(iterator.hasNext()) {
            SearchHit hit = iterator.next();
            String index = hit.getIndex();
            String type = hit.getType();
            String id = hit.getId();
            float score = hit.getScore();
            System.out.println("index="+index+" type="+type+" id="+id+" score="+score+" source-->"+hit.getSourceAsString());
        }
    }

   // 在测试查询速度前需插入数据
    public static void indexPrepareForQuery() {
        System.out.println("Hello , i have entered the test ...");

        long start = System.currentTimeMillis();
        //初始化list
        TestForRiskPremrateTable tt = new TestForRiskPremrateTable(1,1);
        tt.getList2();
        long queryEnd = System.currentTimeMillis();
        System.out.println("初始化数据共用了("+(queryEnd-start)/1000+")s");

        System.out.println("开始转入线程插入数据...");
        // 将获得的list插入到elasticsearch中
        int len = tt.getList().size();
        int line = 10;
        int threadNum = len/line;
        int temp = 0;
        if(threadNum == 0) {
            TestForRiskPremrateTable thread = new TestForRiskPremrateTable(0,1);
            thread.start();
        }else {
            for (int i = 1;i<=line;i++) {
                int endTemp = temp+threadNum;
                if(i == line) {
                    endTemp = len-1;
                }
                TestForRiskPremrateTable thread = new TestForRiskPremrateTable(temp,endTemp);
                temp = endTemp+1;
                thread.start();

            }

        }
    }


}

4.结果
连接成功…
*code request*
查到了11条数据
index=cqp type=riskpremrate id=AWD9XAzH3xu8toz6yvf3 score=102.18167 source–>{“abateTime”:”2018-12-31”,”effectiveTime”:”2016-08-27”,”insertTimeForHis”:null,”operateTimeForHis”:null,”accidentType”:null,”actualValueLower”:null,”actualValueUpper”:null,”agreementProp…
*json request*
{“took”:4,”timed_out”:false,”_sha…
查询数据共用了(1)s
查了两次一共才用了1s哦