新增和修改数据 插入单条数据: HttpHost : url地址封装 RestClientBuilder: rest客户端构建器 RestHighLevelClient: rest高级客户端 IndexRequest: 新增或修改请求 IndexResponse:新增或修改的响应结果
//1.连接rest接口HttpHost http=new HttpHost("127.0.0.1",9200,"http");RestClientBuilder builder= RestClient.builder(http);//rest构建器RestHighLevelClient restHighLevelClient=new RestHighLevelClient(builder);//高级客户端对象//2.封装请求对象IndexRequest indexRequest=new IndexRequest("sku","doc","3");Map skuMap =new HashMap();skuMap.put("name","华为p30pro");skuMap.put("brandName","华为");skuMap.put("categoryName","手机");skuMap.put("price",1010221);skuMap.put("createTime","2019-05-01");skuMap.put("saleNum",101021);skuMap.put("commentNum",10102321);Map spec=new HashMap();spec.put("网络制式","移动4G");spec.put("屏幕尺寸","5");skuMap.put("spec",spec);indexRequest.source(skuMap);//3.获取响应结果IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);int status = response.status().getStatus();System.out.println(status);restHighLevelClient.close();
|