ElasticSearch High Level REST API(1)文档基本操作

获取ES客户端

ES的提供了四种Java客户端,分别为节点客户端(node client)、传输客户端(Transport Client)、低级REST客户端、高级REST客户端。

节点客户端作为集群节点的一部分,在集群节点较多的情况下会影响集群的反应速度。

传输客户端客户端虽然实现了和集群的解耦,相对节点更“轻”,但是会在ES7.0或8.0之后被移除,被“高级Rest客户端”所取代。

如下为ES 客户端的创建:

@Configuration
public class ElasticClient {
    private String host = "192.168.165.239";
    private int port = 9200;

    /**
     * 获取Rest高级客户端
     * @return
     */
    @Bean
    public RestHighLevelClient getRestHighLevelClient(){
        RestClientBuilder builder = RestClient.builder(new HttpHost(host, port));
        return new RestHighLevelClient(builder);
    }

    /**
     * 获取Rest低级客户端
     * @return
     */
    @Bean
    public RestClient getRestClient(){
        RestClientBuilder builder = RestClient.builder(new HttpHost(host, port));
        return builder.build();
    }
}

下面介绍ES在Java中基本的文档增删改查操作

索引操作

索引即我们的新增操作,ES提供了三种形式的索引,分别为通过Json字符串、Map集合、XContentBuilder实现索引操作

@Resource
private RestHighLevelClient client;

@GetMapping("/index/create")
public String createIndex() {
    IndexRequest indexRequest = new IndexRequest("test");
    /*
    //Json字符串作为数据源
    String jsonString = "{" +
            "\"name\": \"Joe.Ye\"," +
            "\"email\": \"yezhou@yezhou.org\"," +
            "\"homepage\": \"http://www.appblog.cn\"" +
            "}";
    indexRequest.source(jsonString, XContentType.JSON);
    */
    //Map集合作为数据源
    Map<String, Object> map = new HashMap<>();
    map.put("name", "Joe.Ye");
    map.put("age", 28);
    map.put("homepage", "http://www.appblog.cn");
    indexRequest.source(map);
    /*
    //XContentBuilder作为数据源
    XContentBuilder builder = null;
    try {
        builder = XContentFactory.jsonBuilder();
        builder.startObject();
        builder.field("name", "Joe.Ye");
        builder.timeField("date", new Date());
        builder.field("homepage", "http://www.appblog.cn");
        builder.endObject();
    } catch (IOException e) {
        e.printStackTrace();
    }
    indexRequest.source(builder);
    */
    try {
        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
        log.info("Status: {}", indexResponse.status().getStatus());
        //client.close();
        return JSON.toJSONString(indexResponse);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}
Status: 201
{
    "fragment":false,
    "id":"zGMq_WoBzwYnh4XWhOrH",
    "index":"test",
    "primaryTerm":1,
    "result":"CREATED",
    "seqNo":0,
    "shardId":{
        "fragment":true,
        "id":-1,
        "index":{
            "fragment":false,
            "name":"test",
            "uUID":"_na_"
        },
        "indexName":"test"
    },
    "shardInfo":{
        "failed":0,
        "failures":[

        ],
        "fragment":false,
        "successful":1,
        "total":2
    },
    "type":"_doc",
    "version":1
}

查询文档

根据索引、ID查询文档

@GetMapping("/index/get")
public String getIndex() {
    GetRequest getRequest = new GetRequest(
            "test",
            "zGMq_WoBzwYnh4XWhOrH");
    try {
        GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
        Map<String, Object> source = getResponse.getSource();
        if (source != null && !source.isEmpty()) {
            for (Map.Entry<String, Object> entry : source.entrySet()) {
                log.info("Key: {}, Value: {}", entry.getKey(), entry.getValue());
            }
        }
        //client.close();
        return JSON.toJSONString(getResponse);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}
Key: name, Value: Joe.Ye
Key: email, Value: yezhou@yezhou.org
Key: homepage, Value: http://www.appblog.cn
{
    "exists":true,
    "fields":{

    },
    "fragment":false,
    "id":"zGMq_WoBzwYnh4XWhOrH",
    "index":"test",
    "primaryTerm":1,
    "seqNo":0,
    "source":{
        "name":"Joe.Ye",
        "email":"yezhou@yezhou.org",
        "homepage":"http://www.appblog.cn"
    },
    "sourceAsBytes":"eyJuYW1lIjoiSm9lLlllIiwiZW1haWwiOiJ5ZXpob3VAeWV6aG91Lm9yZyIsImhvbWVwYWdlIjoiaHR0cDovL3d3dy5hcHBibG9nLmNuIn0=",
    "sourceAsBytesRef":{
        "fragment":true
    },
    "sourceAsMap":{
        "$ref":"$.source"
    },
    "sourceAsString":"{"name":"Joe.Ye","email":"yezhou@yezhou.org","homepage":"http://www.appblog.cn"}",
    "sourceEmpty":false,
    "sourceInternal":{
        "$ref":"$.sourceAsBytesRef"
    },
    "type":"_doc",
    "version":1
}

删除文档

@GetMapping("/index/delete")
public String deleteIndex() {
    DeleteRequest deleteRequest = new DeleteRequest("test", "DWMz_WoBzwYnh4XWA-s8");
    deleteRequest.timeout(TimeValue.timeValueMinutes(10));
    deleteRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
    try {
        DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
        log.info("Status: {}", deleteResponse.status().getStatus());
        //client.close();
        return JSON.toJSONString(deleteResponse);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}
Status: 200
{
    "fragment":false,
    "id":"DWMz_WoBzwYnh4XWA-s8",
    "index":"test",
    "primaryTerm":1,
    "result":"DELETED",
    "seqNo":6,
    "shardId":{
        "fragment":true,
        "id":-1,
        "index":{
            "fragment":false,
            "name":"test",
            "uUID":"_na_"
        },
        "indexName":"test"
    },
    "shardInfo":{
        "failed":0,
        "failures":[

        ],
        "fragment":false,
        "successful":1,
        "total":2
    },
    "type":"_doc",
    "version":2
}

再次查询

{
    "exists":false,
    "fields":{

    },
    "fragment":false,
    "id":"DWMz_WoBzwYnh4XWA-s8",
    "index":"test",
    "primaryTerm":0,
    "seqNo":-2,
    "sourceEmpty":true,
    "type":"_doc",
    "version":-1
}

更新文档

@GetMapping("/index/update")
public String updateIndex() {
    UpdateRequest updateRequest = new UpdateRequest("test", "nWNC_WoBzwYnh4XWTeun");
    Map<String, Object> map = new HashMap<>();
    map.put("name", "Joe.Ye@AppBlog.CN");
    map.put("age", 18);
    updateRequest.doc(map, XContentType.JSON);
    try {
        UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
        log.info("Status: {}", updateResponse.status().getStatus());
        //client.close();
        return JSON.toJSONString(updateResponse);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}
Status: 200
{
    "fragment":false,
    "id":"nWNC_WoBzwYnh4XWTeun",
    "index":"test",
    "primaryTerm":1,
    "result":"UPDATED",
    "seqNo":7,
    "shardId":{
        "fragment":true,
        "id":-1,
        "index":{
            "fragment":false,
            "name":"test",
            "uUID":"_na_"
        },
        "indexName":"test"
    },
    "shardInfo":{
        "failed":0,
        "failures":[

        ],
        "fragment":false,
        "successful":1,
        "total":2
    },
    "type":"_doc",
    "version":2
}

查询文档是否存在

@GetMapping("/index/exists")
public String existsIndex() {
    GetRequest getRequest = new GetRequest("test","zGMq_WoBzwYnh4XWhOrH");
    try {
        getRequest.fetchSourceContext(new FetchSourceContext(false)); //禁用获取 _source字段
        getRequest.storedFields("_none_"); //禁用获取存储字段
        boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
        //client.close();
        return String.valueOf(exists);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}
true

Bulk批量操作

bulk可以批量执行多条操作语句,bulk支持批量操作不同的命令,例如批量更新和删除

@GetMapping("/bulk")
public String bulk() {
    Person person1 = new Person("Joe.Ye", 28, 175.2);
    Person person2 = new Person("Xiong", 26, 165.5);
    BulkRequest bulkRequest = new BulkRequest();
    IndexRequest indexRequest1 = new IndexRequest("person").source(JSON.toJSONString(person1), XContentType.JSON);
    IndexRequest indexRequest2 = new IndexRequest("person").source(JSON.toJSONString(person2), XContentType.JSON);
    DeleteRequest deleteRequest = new DeleteRequest("test", "AGMx_WoBzwYnh4XWx-tL");
    bulkRequest.add(indexRequest1);
    bulkRequest.add(indexRequest2);
    bulkRequest.add(deleteRequest);
    try {
        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        log.info("Status: {}", bulkResponse.status().getStatus());
        //client.close();
        return JSON.toJSONString(bulkResponse);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}
Status: 200
{
    "fragment":false,
    "ingestTook":{
        "days":0,
        "daysFrac":-1.1574074074074074e-8,
        "hours":0,
        "hoursFrac":-2.7777777777777776e-7,
        "micros":-1000,
        "microsFrac":-1000,
        "millis":-1,
        "millisFrac":-1,
        "minutes":0,
        "minutesFrac":-0.000016666666666666667,
        "nanos":-1000000,
        "seconds":0,
        "secondsFrac":-0.001,
        "stringRep":"-1"
    },
    "ingestTookInMillis":-1,
    "items":[
        {
            "failed":false,
            "fragment":false,
            "id":"n2Nh_WoBzwYnh4XWSuxi",
            "index":"person",
            "itemId":0,
            "opType":"INDEX",
            "response":{
                "fragment":false,
                "id":"n2Nh_WoBzwYnh4XWSuxi",
                "index":"person",
                "primaryTerm":1,
                "result":"CREATED",
                "seqNo":0,
                "shardId":{
                    "fragment":true,
                    "id":-1,
                    "index":{
                        "fragment":false,
                        "name":"person",
                        "uUID":"_na_"
                    },
                    "indexName":"person"
                },
                "shardInfo":{
                    "failed":0,
                    "failures":[

                    ],
                    "fragment":false,
                    "successful":1,
                    "total":2
                },
                "type":"_doc",
                "version":1
            },
            "type":"_doc",
            "version":1
        },
        {
            "failed":false,
            "fragment":false,
            "id":"oGNh_WoBzwYnh4XWSuxi",
            "index":"person",
            "itemId":1,
            "opType":"INDEX",
            "response":{
                "fragment":false,
                "id":"oGNh_WoBzwYnh4XWSuxi",
                "index":"person",
                "primaryTerm":1,
                "result":"CREATED",
                "seqNo":1,
                "shardId":{
                    "fragment":true,
                    "id":-1,
                    "index":{
                        "fragment":false,
                        "name":"person",
                        "uUID":"_na_"
                    },
                    "indexName":"person"
                },
                "shardInfo":{
                    "failed":0,
                    "failures":[

                    ],
                    "fragment":false,
                    "successful":1,
                    "total":2
                },
                "type":"_doc",
                "version":1
            },
            "type":"_doc",
            "version":1
        },
        {
            "failed":false,
            "fragment":false,
            "id":"AGMx_WoBzwYnh4XWx-tL",
            "index":"test",
            "itemId":2,
            "opType":"DELETE",
            "response":{
                "fragment":false,
                "id":"AGMx_WoBzwYnh4XWx-tL",
                "index":"test",
                "primaryTerm":1,
                "result":"DELETED",
                "seqNo":8,
                "shardId":{
                    "fragment":true,
                    "id":-1,
                    "index":{
                        "fragment":false,
                        "name":"test",
                        "uUID":"_na_"
                    },
                    "indexName":"test"
                },
                "shardInfo":{
                    "failed":0,
                    "failures":[

                    ],
                    "fragment":false,
                    "successful":1,
                    "total":2
                },
                "type":"_doc",
                "version":2
            },
            "type":"_doc",
            "version":2
        }
    ],
    "took":{
        "days":0,
        "daysFrac":0.000004780092592592592,
        "hours":0,
        "hoursFrac":0.00011472222222222222,
        "micros":413000,
        "microsFrac":413000,
        "millis":413,
        "millisFrac":413,
        "minutes":0,
        "minutesFrac":0.006883333333333333,
        "nanos":413000000,
        "seconds":0,
        "secondsFrac":0.413,
        "stringRep":"413ms"
    }
}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/19/elasticsearch-high-level-rest-api-document-basic-operations/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
ElasticSearch High Level REST API(1)文档基本操作
获取ES客户端 ES的提供了四种Java客户端,分别为节点客户端(node client)、传输客户端(Transport Client)、低级REST客户端、高级REST客户端。 节点客户端作为……
<<上一篇
下一篇>>
文章目录
关闭
目 录