注意下ES的版本

pom.xml

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

代码

public static Settings settings;
public static TransportClient client;

public static void main(String[] args) throws Exception {
    settings = Settings.builder()
            .put("cluster.name", "mmba").put("client.transport.sniff", false).build();

    client = new PreBuiltTransportClient(settings)
            .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.94"), 9300));

}

private static void test1() throws Exception {
    for(int i=1;i<=2;i++) {
        IndexResponse response = client.prepareIndex("app_use_log", "app_use_log")
                .setSource(XContentFactory.jsonBuilder()
                        .startObject()
                            .field("orgName", user.getOrgName())
                            .field("appName", apk.getName())
                            .field("className", word.getClassName())
                            .field("deviceId", user.getDeviceID())
                            .field("deviceName", "11_HUAWEI PRA-AL00")
                            .field("orgId", user.getOrgID())
                            .field("duration", ESUtil.getRandomInteger(20))
                            .field("classId", word.getClassID())
                            .field("tenancyName", "auto")
                            .field("appId", apk.getId())
                            .field("staffName", user.getUserName())
                            .field("beginTime", ESUtil.getDate())
                            .field("endTime", ESUtil.getDate())
                            .field("esId", ESUtil.getID())
                            .field("staffId", user.getStaffID())
                            .field("id", ESUtil.getID())
                            .field("beginHour", 4)
                            .field("endHour", 6)
                        .endObject())
                .get();
        System.out.println(response.getResult()); 
    }
}

private static void test2() {
    Map<String, Object> json = new HashMap<String, Object>();  
    json.put("user","kimchy");  
    json.put("postDate","2013-01-30");  
    json.put("message","trying out Elasticsearch");  

    IndexResponse response = client.prepareIndex("fendo", "fendodate")  
            .setSource(json)  
            .get();  
}

private static void delete1(String index,String type,String id) {
    DeleteResponse response = client.prepareDelete(index, type, id).get();
    System.out.println(response.getResult());
}

private static void delete2(String index) {
    BulkByScrollResponse response =
            DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
                .filter(QueryBuilders.matchQuery("gender", "male")) //查询条件
                .source(index) //index(索引名)
                .get();  //执行

    long deleted = response.getDeleted(); //删除文档的数量
}

详情参考: https://juejin.im/entry/5a0c436cf265da43133cb1df

发表评论

电子邮件地址不会被公开。 必填项已用*标注