elastic / elasticsearch-java

Official Elasticsearch Java Client
Apache License 2.0
6 stars 242 forks source link

According to the documentation getting-started-java adjustment Indexi… #740

Open qq943260285 opened 9 months ago

qq943260285 commented 9 months ago

…ngTest.java update doc

According to the documentation https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/getting-started-java.html,Update exception (Validation Failed: 1: script or doc is missing;)

wordscode commented 8 months ago

+1 @Test public void updateDoc() throws Exception { //tag::single-doc-update Product product = new Product("bk-1", "City bike", 666);

    esClient.update(u -> u
                    .index("products")
                    .id("bk-1")
                    .upsert(product),
            Product.class
    );
    //end::single-doc-update
}

报错: co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/update] failed: [action_request_validation_exception] Validation Failed: 1: script or doc is missing;

at co.elastic.clients.transport.ElasticsearchTransportBase.getApiResponse(ElasticsearchTransportBase.java:338)
at co.elastic.clients.transport.ElasticsearchTransportBase.performRequest(ElasticsearchTransportBase.java:140)
at co.elastic.clients.elasticsearch.ElasticsearchClient.update(ElasticsearchClient.java:2201)
at co.elastic.clients.elasticsearch.ElasticsearchClient.update(ElasticsearchClient.java:2218)
at com.example.gp.EsTest.updateDoc(EsTest.java:93)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

我执行下列代码都是成功的: @Test public void singleDocumentDSL() throws Exception {

    //tag::single-doc-dsl
    Product product = new Product("bk-1", "City bike", 123.0);

    IndexResponse response = esClient.index(i -> i
            .index("products")
            .id(product.getSku())
            .document(product)
    );

    log.info("Indexed with version " + response.version()); //每当对文档执行更新操作时,版本号会增加,以便跟踪文档的变化历史。
    //end::single-doc-dsl
}

 @Test
public void deleteDocument() throws Exception {
    esClient.delete(d -> d.index("products").id("bk-1"));
}  
qq943260285 commented 8 months ago
    此回复表示我收到你的邮件了哦。   

    --

            小宇