elasticsearch的java api基本操作

1.添加依赖

默认elasticsearch的配置已经没有问题了,本文使用elasticsearch版本为6.4.2,依赖如下:

        
            org.elasticsearch
            elasticsearch
            6.4.2
        
        
            org.elasticsearch.client
            transport
            6.4.2
        

2.使用transport连接elasticsearch集群

①、公用方法
    private static TransportClient getClient() throws Exception {
        // client.transport.sniff=true :使客户端嗅探整个集群状态,把集群中其他机器的ip地址添加到客户端中
        Settings settings = Settings.builder().put("cluster.name", "my-es-cluster")
                .put("client.transport.sniff", true).build();
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("10.49.2.18"), 9300));
        return client;
    }

    private static XContentBuilder getJsonBuilder() throws Exception {
        XContentBuilder jsonBuilder = XContentFactory.jsonBuilder();
        return jsonBuilder;
    }
②、创建索引
    private static void createDoc() throws Exception {
        TransportClient client = TestES2.getClient();
        XContentBuilder source = new XContentFactory().jsonBuilder()
                .startObject()
                .startObject("settings") //设置文档属性
                .field("number_of_shards", 1)
                .field("number_of_replicas", 0)
                .endObject()
                .endObject()
                .startObject()
                .startObject("testlog") // 文档类型
                .startObject("properties")
                .startObject("type")
                .field("type", "string")
                .field("store", "yes")
                .endObject()
                .startObject("eventCount")
                .field("type", "long")
                .field("store", "yes")
                .endObject()
                .startObject("eventDate")
                .field("type", "date")
                .field("format", "dateOptionalTime")
                .field("store", "yes")
                .endObject()
                .startObject("message")
                .field("type", "string")
                .field("index", "not_analyzed")
                .field("store", "yes")
                .endObject()
                .endObject()
                .endObject()
                .endObject();

        CreateIndexRequestBuilder cirb = client.admin().indices().prepareCreate("testhello")
                .setSource(source);
        CreateIndexResponse response = cirb.execute().actionGet();
        if (response.isAcknowledged()) {
            System.out.println("index create success!");
        } else {
            System.out.println("index create failed!");
        }
    }
③、添加文档
    private static void addDoc() throws Exception {
        TransportClient client = TestES2.getClient();
        XContentBuilder jsonBuilder = TestES2.getJsonBuilder();
        IndexResponse response = client.prepareIndex("testhello", "testlog", "1")
                .setSource(jsonBuilder.startObject()
                        .field("type", "log")
                        .field("eventCount", 1)
                        .field("eventDate", new Date())
                        .field("message", "testlog insert doc test")
                        .endObject()
                ).get();
        System.out.println("index:" + response.getIndex() + " insert doc id:" + response.getId() + " result:" + response.getResult());
    }
④、更新文档
    private static void updateDoc() throws Exception {
        UpdateRequest request = new UpdateRequest();
        request.index("testhello");
        request.type("testlog");
        request.id("1");
        XContentBuilder jsonBuilder = TestES2.getJsonBuilder();
        request.doc(jsonBuilder.startObject().field("type", "file").endObject());
        UpdateResponse response = TestES2.getClient().update(request).get();
        System.out.println("result:" + response.getResult()); // result:UPDATED
    }
⑤、查询文档
    private static void searchDoc() throws Exception {
        TransportClient client = TestES2.getClient();
        // 索引 类型 文档id
        GetResponse response = client.prepareGet("testhello", "testlog", "1").get();
        System.out.println(response);
    }
⑥、删除文档
    private static void deleteDoc() throws Exception {
        TransportClient client = TestES2.getClient();
        DeleteResponse deleteResponse = client.prepareDelete("testhello", "testlog", "1").get();
        // 判断是否能够删除
        System.out.println(deleteResponse.status());
        // testhello为索引名称
        DeleteIndexRequest request = new DeleteIndexRequest("testhello");
        ActionFuture actionFuture = client.admin().indices().delete(request);
        DeleteIndexResponse response = actionFuture.actionGet();
        // response.isAcknowledged()若为true则删除成功
        if (response.isAcknowledged()) {
            System.out.println("删除成功!");
        } else {
            System.out.println("删除失败!");
        }
    }

你可能感兴趣的:(elasticsearch)