如何使用Java客户端更新弹性搜索的条目
我正在尝试使用弹性搜索客户端更新我的es模型信息如何使用Java客户端更新弹性搜索的条目,java,
elasticsearch,Java,
elasticsearch,我正在尝试使用弹性搜索客户端更新我的es模型信息 org.elasticsearch.client.Client 我真的很难找到正确的方法来做这件事,因为我不知道索引和匹配器,对不起,我对这个问题非常感兴趣 { "_index": "my_index_20", "_type": "student", "_id": "a80ae58", "_source": { "model": { "id": "a80ae58748e",
org.elasticsearch.client.Client
我真的很难找到正确的方法来做这件事,因为我不知道索引和匹配器,对不起,我对这个问题非常感兴趣
{
"_index": "my_index_20",
"_type": "student",
"_id": "a80ae58",
"_source": {
"model": {
"id": "a80ae58748e",
"name": "John Doe"
....
到目前为止我的代码
response = esClient.prepareUpdate("student", "name", "John Doe")
.setDoc(jsonBuilder()
.startObject()
.field("name", "Joe Doe")
.endObject())
.get();
我是否使用了正确的索引?或者我可以在这里改变什么
我没有得到任何错误,但“文件丢失”的结果。。。意味着我可能没有使用正确的索引
想法
根据反馈和更多信息更新
我把它移到了
response = esClient.prepareUpdate("my_index_20", "student", "a80ae58")
.setDoc(jsonBuilder()
.startObject()
.field("name", "Joe Doe")
.endObject())
.get();
这是可行的,但由于我不知道索引ID,我无法执行此操作,是否有任何方法可以通过查询生成器或其他功能执行此操作 这是准备更新方法的签名:
UpdateRequestBuilder prepareUpdate(String index, String type, String id);
因此,正确的语法可能是
esClient.prepareUpdate("my_index_20", "student", "a80ae58").setDoc(...)...
如果要通过匹配其他字段来执行此操作,请使用update by查询
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();
嗨,是的,但是我没有ID本身,我是否可以通过匹配其他字段来实现它?当然可以。使用“按查询更新”。查看此链接,了解如何处理此案例吗?:它仍然说我需要索引如果你不知道确切的索引,但知道全局格式(例如索引名称的开头),你可以这样做:
String indexName=“my_index”*;BoolQueryBuilder boolQuery=QueryBuilders.boolQuery();boolQuery.filter(QueryBuilders.termQuery(“name”,“johndoe”));UpdateByQueryRequestBuilder updateByQuery=UpdateByQueryAction.INSTANCE.newRequestBuilder(esClient);updateByQuery.source(indexName);updateByQuery.filter(boolQuery);BulkByScrollResponse updateResponse=updateByQuery.get()代码>@user6441481你应该用这个代码更新你的答案,它会更清晰