通过学习Java High Level REST Client官方文档,进行一些总结。
本文为关于Index API的总结:
IndexRequest request = new IndexRequest("索引", "type","id");
request.source("转换为json的对象", XContentType.JSON);
同样也支持使用Map的形式放入对象
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("字段1", "值");
jsonMap.put("字段2", new Date());
jsonMap.put("字段3", "值");
IndexRequest indexRequest = new IndexRequest("索引", "tyep", "id")
.source(jsonMap);
也可以直接在source中对需要的对象进行赋值
IndexRequest indexRequest = new IndexRequest("index", "type", "id")
.source("字段1", "值",
"字段2", new Date(),
"字段3", "值");
设置分片超时时间,以下两种方式均可
request.timeout(TimeValue.timeValueSeconds(1));
request.timeout("1s");
设置刷新策略,若未设置最常见的情况就是执行es中数据更新或插入,同步去查询时数据发现没有更新。
request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
request.setRefreshPolicy("wait_for");
通过设置poType为“update”or“create”设置更新或者修改
request.opType(DocWriteRequest.OpType.CREATE);
request.opType("create");