High-Level-REST-Client 操作ES(boot2.4.4、esdata4.1x、es7.9.3)

一、IndexRequest

1.索引、请求的文档id、以字符串形式提供的文档源
IndexRequest request = new IndexRequest("posts"); 
request.id("1"); 
String jsonString = "{" +
        "\"user\":\"kimchy\"," +
        "\"postDate\":\"2013-01-30\"," +
        "\"message\":\"trying out Elasticsearch\"" +
        "}";
request.source(jsonString, XContentType.JSON); 
2.作为映射提供的文档源,该映射将自动转换为JSON格式
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("user", "kimchy");
jsonMap.put("postDate", new Date());
jsonMap.put("message", "trying out Elasticsearch");
IndexRequest indexRequest = new IndexRequest("posts")
    .id("1").source(jsonMap); 
3.文档源以对象密钥对的形式提供,并转换为JSON格式
IndexRequest indexRequest = new IndexRequest("posts")
    .id("1")
    .source("user", "kimchy",
        "postDate", new Date(),
        "message", "trying out Elasticsearch"); 
Optional argumentsedit-可以选择提供以下参数:

路由值

request.routing("routing"); 

路由值

#Timeout to wait for primary shard to become available as a TimeValue(等待主碎片作为时间值可用的超时)
request.timeout(TimeValue.timeValueSeconds(1)); 
#Timeout to wait for primary shard to become available as a String(等待主碎片作为字符串可用的超时)
request.timeout("1s"); 
#将策略刷新为WriteRequest.RefreshPolicy文件实例
request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL); 
#将策略刷新为字符串
request.setRefreshPolicy("wait_for");                 
#版本
request.version(2); 
#版本类型
request.versionType(VersionType.EXTERNAL); 
#操作类型作为DocWriteRequest.OpType文件价值
request.opType(DocWriteRequest.OpType.CREATE); 
#作为字符串提供的操作类型:可以创建或索引(默认)
request.opType("create"); 
#索引文档之前要执行的摄取管道的名称
request.setPipeline("pipeline"); 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容