简体   繁体   English

在Java中检查ElasticSearch中updateRequest的状态

[英]Check the status of updateRequest in ElasticSearch in Java

Indexing using elastic search allows to check the status of the response: 使用弹性搜索建立索引可以检查响应状态:

import static org.elasticsearch.common.xcontent.XContentFactory.*;

IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
        .setSource(jsonBuilder()
                    .startObject()
                        .field("user", "kimchy")
                        .field("postDate", new Date())
                        .field("message", "trying out Elasticsearch")
                    .endObject()
                  )
        .get();

// status has stored current instance statement.
RestStatus status = response.status();

When I need to upsert and using updateRequest instead of the response, is there an equivalent of a status method? 当我需要upsert并使用updateRequest而不是响应时,是否有等效的状态方法?

IndexRequest indexRequest = new IndexRequest("index", "type", "1")
        .source(jsonBuilder()
            .startObject()
                .field("name", "Joe Smith")
                .field("gender", "male")
            .endObject());
UpdateRequest updateRequest = new UpdateRequest("index", "type", "1")
        .doc(jsonBuilder()
            .startObject()
                .field("gender", "male")
            .endObject())
        .upsert(indexRequest);              
client.update(updateRequest).get();

Thanks. 谢谢。

The simplest way is to check for the number of successful, failed, or total operations. 最简单的方法是检查成功,失败或全部操作的数量。

For example, if the method to use is getTotal(), 例如,如果要使用的方法是getTotal(),

rather than using get(), use actionGet() to get an UpdateResponse that contains the number of operations, including the total ones. 而不是使用get(),而是使用actionGet()来获取一个UpdateResponse,其中包含操作数,包括总数。

Putting them all together: 将它们放在一起:

client.update(updateRequest).actionGet().getShardInfo().getTotal();

For others operations just change the getTotal() to the one you want. 对于其他操作,只需将getTotal()更改为所需的值即可。

The result is the number of operations in int. 结果是int中的操作数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM