简体   繁体   English

如何使用Java客户端更新Elastic Search上的条目

[英]How to update an entry on Elastic Search using Java client

I am trying to make an update on my es model information using an elastic search client 我正在尝试使用弹性搜索客户端更新我的es模型信息

org.elasticsearch.client.Client

https://www.programcreek.com/java-api-examples/?api=org.elasticsearch.client.Client https://www.programcreek.com/java-api-examples/?api=org.elasticsearch.client.Client

It is really hard for me to find the correct way to do it since I dont know the index and the matcher, Im sorry, I am very beginer at this subject. 我真的很难找到正确的方法,因为我不知道索引和匹配器,对不起,我非常喜欢这个主题。

  {
    "_index": "my_index_20",
    "_type": "student",
    "_id": "a80ae58",
    "_source": {
      "model": {
        "id": "a80ae58748e",
        "name": "John Doe"
        ....

My code so far 我的代码到目前为止

 response = esClient.prepareUpdate("student", "name", "John Doe")
                    .setDoc(jsonBuilder()               
                    .startObject()
                    .field("name", "Joe Doe")
                    .endObject())
                    .get();

Am I using the right index? 我使用正确的索引吗? or what could I change here? 或者我可以在这里改变什么?

I am not getting any error but a "document missing" result... means I might not be using the right indexes. 我没有收到任何错误,但“文档丢失”结果...意味着我可能没有使用正确的索引。

Ideas? 想法?

Updated according feedback and more info... 根据反馈和更多信息更新...

I moved it to 我把它移到了

response = esClient.prepareUpdate("my_index_20", "student", "a80ae58")
                    .setDoc(jsonBuilder()               
                    .startObject()
                    .field("name", "Joe Doe")
                    .endObject())
                    .get();

This works but since I dont know the index ID I cannot perform this, is there any way to do it by a query builder or other functionality? 这有效,但由于我不知道索引ID我无法执行此操作,是否有任何方法可以通过查询生成器或其他功能执行此操作?

This is the signature of the prepareUpdate method: 这是prepareUpdate方法的签名:

UpdateRequestBuilder prepareUpdate(String index, String type, String id);

So the correct syntax may be 所以正确的语法可能是

esClient.prepareUpdate("my_index_20", "student", "a80ae58").setDoc(...)...

If you want to do it by matching other field, use the update by query. 如果要通过匹配其他字段来执行此操作,请按查询使用更新。

String indexName = "my_index_*"; //Assuming that you don't know the exact index, but know the global format (for example the beginning of the name of the index)
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.filter(QueryBuilders.termQuery("name", "John Doe"));
boolQuery.filter(QueryBuilders.termQuery("otherField", "otherFieldValue"));
UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(esClient);
updateByQuery.source(indexName); 
updateByQuery.filter(boolQuery); 
BulkByScrollResponse updateResponse = updateByQuery.get();

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

相关问题 如何在 Java 中使用 Redisson 客户端更新条目的生存时间? - How to update time-to-live of an entry using Redisson client in Java? 如何通过 Java 高级 rest 客户端在 Elastic Search 中使用多个字段进行搜索 - How to search using multiple fields in Elastic Search through Java high level rest client 使用弹性搜索Java客户端时,如何上传pdf到elasticsearch? - How do I upload a pdf to elasticsearch when using the elastic search java client? 如何使用 JEST 客户端在弹性搜索中添加自定义名称? - How to add custom name in elastic search using JEST client? 如何使用ElasticsearchRepository通过Java搜索弹性搜索? - How to search elastic search via Java using ElasticsearchRepository? 如何使用通配符弹性搜索 java 搜索不区分大小写 - How to search case insensitive using wildcard elastic search java 弹性搜索 - Java api 客户端范围查询 - elastic search - Java api client range query Bool在弹性搜索java客户端中查询模糊性 - Bool query with fuzziness in elastic search java client 弹性搜索客户端使用R包 - elastic search client using R packages 如何使用弹性搜索RestHighLevelClient在java中编写json映射? - How to write json mapping in java using elastic search RestHighLevelClient?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM