package com.one;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import java.util.HashMap;
import java.util.Map;
public class Test01 {
public static void main(String[] args) throws Exception {
// 1.连接接口 第一个ip地址,第二个端口,第三个协议
HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
RestClientBuilder builder = RestClient.builder(http);
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
// 2.封装请求对象
IndexRequest indexRequest = new IndexRequest("sku", "doc", "3");
Map skuMap = new HashMap();
skuMap.put("name", "魅族");
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("网络制式", "全网通");
spec.put("屏幕尺寸", "5");
skuMap.put("spec", spec);
indexRequest.source(skuMap);
// 3.获取执行结果
IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
int status = index.status().getStatus();
System.out.println(status);
restHighLevelClient.close();
}
}
|
|